#!/bin/sh

# This software is released according to the Expat license below.
#
# Copyright: 2012-2013, Giulio Paci <giuliopaci@gmail.com>
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use, copy,
# modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.



COMMAND="$1"

SCTK_PATH=/usr/lib/sctk/bin
PATH="$SCTK_PATH":"$PATH"

get_command()
{
    local cmnd="$1"
    if [ -x "$SCTK_PATH"/"$cmnd" ]
    then
	echo "$SCTK_PATH"/"$cmnd"
    elif [ -x "$SCTK_PATH"/"$cmnd".sh ]
    then
	echo "$SCTK_PATH"/"$cmnd".sh
    elif [ -x "$SCTK_PATH"/"$cmnd".pl ]
    then
	echo "$SCTK_PATH"/"$cmnd".pl
    else
	return 1
    fi
}

get_commands()
{
    ls "$SCTK_PATH" | while read cmnd;
    do
	# "$SCTK_PATH"/"$cmnd" -h 2>&1 \
	#     | head -n 3 \
	#     | grep  . \
	#     ;
	get_command "$cmnd" | sed -e 's#.*/##g' -e 's/[.]\(sh\|pl\)//g'
    done
    echo "path - print base SCTK binaries path"
    echo "help - print help about commands"
}

get_help_usage()
{
    local cmnd="$1"
    if test x"$cmnd" = x"" || test x"$cmnd" = x"help"
    then
    cat<<EOF

help - print help about commands

USAGE:
       help [ command ]

COMMANDS:
EOF
	get_commands | sed -e 's/^/       /' | sed -e 's/[.]\(sh\|pl\) -/ -/g'
    cat<<EOF

EOF
    else
	local CMND=`get_command "$cmnd"`
	if [ -x "$CMND" ]
	then
	    "$CMND" -h 2>&1
	else
	    cat<<EOF
"$cmnd" command unsupported.
Try "$0 help" for a list of supported commands.
EOF
	fi
    fi
}

get_path_usage()
{
    cat<<EOF

path - print base SCTK binaries path

 usage:
      path

SPTK: frontend
EOF
}

get_usage()
{
    local cmnd="$1"
    if test x"$cmnd" = x""
    then
	cat<<EOF

sctk - frontend to the SCTK toolkit

USAGE:
       sctk [ command ] [ command_options ]
       sctk help [ command ]

EXAMPLES:
       sctk help
          Print a brief descriprion of the available
          commands.
       sctk help <command>
          Print help for <command>.
       sctk <command> [ command_options ]
          Executes <command> with the given options. 

EOF
    fi
}

if test x"$1" = x""
then
    get_usage
    exit
fi
shift
if test x"$COMMAND" = x"help"
then
    get_help_usage "$1"
    exit 0;
fi
if test x"$COMMAND" = x"path"
then
    echo "$SCTK_PATH"
    exit 0;
fi

CMND=`get_command "$COMMAND"`
if [ -x "$CMND" ]
then
    "$CMND" "$@"
else
    get_help_usage "$COMMAND"
fi
