#!/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"

export IRSTLM=/usr/lib/irstlm
IRSTLM_PATH=/usr/lib/irstlm/bin
PATH=$IRSTLM_PATH:$PATH

get_commands()
{
    ls "$IRSTLM_PATH" | while read cmnd;
    do
	"$IRSTLM_PATH"/"$cmnd" -h 2>&1 \
	    | head -n 3 \
	    | grep  . \
	    ;
    done
    echo "path - print IRSTLM 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/^/       /' | sed -e 's/\([[:space:]]plsa[.]sh\) -/\1.sh -/g' -e 's/[.]\(sh\|pl\) -/ -/g'
    cat<<EOF

EOF
    elif test x"$cmnd" = x"path"
    then
        get_path_usage
    else
	if [ -x "$IRSTLM_PATH"/"$cmnd" ]
	then
	    "$IRSTLM_PATH"/"$cmnd" -h 2>&1
	elif [ -x "$IRSTLM_PATH"/"$cmnd".sh ]
	then
	    "$IRSTLM_PATH"/"$cmnd".sh -h 2>&1 | sed -e 's/\('"$cmnd"'\)[.]sh/\1/g'
	elif [ -x "$IRSTLM_PATH"/"$cmnd".pl ]
	then
	    "$IRSTLM_PATH"/"$cmnd".pl -h 2>&1 | sed -e 's/\('"$cmnd"'\)[.]pl/\1/g'
	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 IRSTLM binaries path

USAGE:
      path

EOF
}

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

irstlm - frontend to the IRST Language Modeling toolkit

DESCRIPTION:
       irstlm is a software for statistical language modeling. It
       generates n-gram models that can be used on any system
       supporting ARPA language model format.

USAGE:
       irstlm [ command ] [ command_options ]
       irstlm help [ command ]

EXAMPLES:
       irstlm help
          Print a brief descriprion of the available
          commands.
       irstlm help <command>
          Print help for <command>.
       irstlm <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 "$IRSTLM_PATH"
    exit 0;
fi

if [ -x "$IRSTLM_PATH"/"$COMMAND" ]
then
    "$IRSTLM_PATH"/"$COMMAND" "$@"
elif [ -x "$IRSTLM_PATH"/"$COMMAND".sh ]
then
    "$IRSTLM_PATH"/"$COMMAND".sh "$@"
elif [ -x "$IRSTLM_PATH"/"$COMMAND".pl ]
then
    "$IRSTLM_PATH"/"$COMMAND".pl "$@"
else
    get_help_usage "$COMMAND"
fi
