#!/bin/sh
# $Id: limaltool 2266 2007-07-18 09:43:22Z mt $
#
# limaltool - Start a LiMaL tool from the LiMaL tool path
#
# Author: Stefan Hundhammer <sh@suse.de>
#         Stefan Schubert   <schubi@suse.de>
# (c) SuSE GmbH 2001
#

self=`basename $0`

quiet="no"
prefix=""
for opt in $@ ; do
    case "$opt" in
      -p|--prefix)
	shift
	prefix="$1"
	shift
	if test -z "$prefix" ; then
		test "$quiet" != "yes" && \
		echo "Option '$opt' requires argument" >&2
		exit 1
	fi
      ;;
      -q|--quiet)
	quiet="yes"
	shift
      ;;
      *) break ;;
    esac
done

# run the tools that are in the same prefix as we are
limaltoolpath=.
if [ -z "$prefix" ]; then
    if [ `dirname $0` != "." ]; then
	prefix=${0%/bin/$self}
	limaltoolpath=${prefix}/share/limal/devtools/bin
    fi
else
    limaltoolpath=$prefix
fi
if [ ! -d "$limaltoolpath" ] ; then
    test "$quiet" != "yes" && \
    echo "limal tool prefix '$limaltoolpath' does not exists" >&2
    exit 1
fi

show_usage()
{
    echo ""
    echo "Usage: ${self} [options] [<command> [<arg> ...]]"
    echo ""
}
show_help()
{
    show_usage
    echo "Options:"
    echo "  --help   | -h           Show this help message"
    echo "  --list   | -l           Show list of available commands"
    echo "  --quiet  | -q           Be quiet, report via exit code"
    echo "  --prefix | -p <prefix>  Use <prefix> to locate commands"
    echo "  --exists | -x <command> Check whether the command exists"
    echo ""
}

opt=$1
shift
case "$opt" in
    --exists|-x)
	tool=$1
	shift
	if test -n "$tool" && test -x "$limaltoolpath/$tool" ; then
	    test "$quiet" != "yes" && \
	    echo "$limaltoolpath/$tool"
	    exit 0
	elif test -n "$tool" ; then
	    test "$quiet" != "yes" && \
	    echo "$limaltoolpath/$tool not found" >&2
	    exit 1
	else
	    test "$quiet" != "yes" && \
	    echo "Option '$opt' requires argument" >&2
	    exit 1
	fi
    ;;
    --list|-l)
	cd $limaltoolpath && ls -C
	exit 0
    ;;
    --help|-h|-?)
	show_help
	echo "Starts specified LiMaL command from '$limaltoolpath'."
	echo ""
	echo "Avaliable commands: "
	echo ""
	cd $limaltoolpath && ls -C
	echo ""
	exit 0
    ;;
    --*)
	echo "Unknown option '$opt'" >&2
	exit 1
    ;;
    "")
	show_usage >&2
	exit 1
    ;;
    *)
	if [ -x $limaltoolpath/$opt ]; then
		$limaltoolpath/$opt "$@"
	else
	    echo "${self}: Can't find \"$opt\" in $limaltoolpath" >&2
	    exit 1
	fi
    ;;
esac

