#!/bin/sh

VERSION="2.1"

print_help()
{
    printf "%s\n" "
Usage:
any COMMAND [options]

Format of options depends on used COMMAND.

Description of each of them you may find in:
any man <command>
any help <command>
"
#List of available commands:
#$(subcom --list)

}


case "$1" in
    --help)
            print_help
            exit 0
        ;;
    --version)
            printf "%s\n" "${VERSION}"
            exit 0
        ;;
esac

work_dir="$(rootrdd)"

if test -z "${work_dir}" ; then
    printf "%s\n" "
    Root of working directory has not been set.
    Executable \"rootrdd\" is unavailable,
    or there are troubles with current directory (does it exist?)"
    exit 1
fi

# local commands are checked by default
local_commands="${work_dir}/any/libexec"
command="$( subcom -p "${local_commands}" "$0" "$@" 2> /dev/null )"
if test -n "${command}" ; then
    args_command="$(subcom -p "${local_commands}" -s "$0" "$@")"
else
    # after local ones check the commands, installed globally
    command="$(subcom "$0" "$@")"
    if test -n "${command}" ; then
        args_command="$(subcom -s "$0" "$@")"
    else
        printf "%s\n" "No proper command for any was found."
        print_help
        exit 1
    fi
fi

for arg in ${args_command} ; do
    shift
done
${command} "$@"
