#!/bin/sh

# ----------------------------------------------------------------- #
#             Frontend to the Speech Signal Processing Toolkit      #
#             developed by Giulio Paci                              #
# ----------------------------------------------------------------- #
#  Copyright (c) 2012-2013  Giulio Paci <giuliopaci@gmail.com>      #
#                                                                   #
# All rights reserved.                                              #
#                                                                   #
# Redistribution and use in source and binary forms, with or        #
# without modification, are permitted provided that the following   #
# conditions are met:                                               #
#                                                                   #
# - Redistributions of source code must retain the above copyright  #
#   notice, this list of conditions and the following disclaimer.   #
# - Redistributions in binary form must reproduce the above         #
#   copyright notice, this list of conditions and the following     #
#   disclaimer in the documentation and/or other materials provided #
#   with the distribution.                                          #
# - Neither the name of the SPTK working group nor the names of its #
#   contributors may be used to endorse or promote products derived #
#   from this software without specific prior written permission.   #
#                                                                   #
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND            #
# CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,       #
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF          #
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE          #
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS #
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,          #
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED   #
# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,     #
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON #
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,   #
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY    #
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE           #
# POSSIBILITY OF SUCH DAMAGE.                                       #
# ----------------------------------------------------------------- #


COMMAND="$1"

SPTK_PATH=/usr/lib/sptk/bin
PATH=$SPTK_PATH:$PATH

get_commands()
{
    ls "$SPTK_PATH" | while read cmnd;
    do
	"$SPTK_PATH"/"$cmnd" -h 2>&1 \
	    | head -n 3 \
	    | grep -v -- "-h" \
	    | grep  . \
	    ;
    done
    echo " path - print base SPTK binaries path"
    echo " help - print help about commands"
}

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

help - print help about commands

 usage:
      help [ command ]
 commands:
EOF
	get_commands | sed -e 's/^/     /'
    cat<<EOF

SPTK: frontend
EOF
    else
	if [ -x "$SPTK_PATH"/"$cmnd" ]
	then
	    "$SPTK_PATH"/"$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 SPTK binaries path

 usage:
      path

SPTK: frontend
EOF
}

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

sptk - frontend to the SPTK toolkit

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

SPTK: frontend
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 "$SPTK_PATH"
    exit 0;
fi

if [ -x "$SPTK_PATH"/"$COMMAND" ]
then
    "$SPTK_PATH"/"$COMMAND" "$@"
else
    get_help_usage "$COMMAND"
fi
