#!/bin/sh
# ----------------------------------------------------------------------------
# - afnix-aexec                                                              -
# - afnix als execution script                                               -
# ----------------------------------------------------------------------------
# - This program is  free software;  you can  redistribute it and/or  modify -
# - it provided that this copyright notice is kept intact.                   -
# -                                                                          -
# - This  program  is  distributed in the hope  that it  will be useful, but -
# - without  any   warranty;  without  even   the   implied    warranty   of -
# - merchantability  or fitness for a particular purpose. In not event shall -
# - the copyright holder be  liable for  any direct, indirect, incidental or -
# - special damages arising in any way out of the use of this software.      -
# ----------------------------------------------------------------------------
# - copyright (c) 1999-2007 amaury darsch                                    -
# ----------------------------------------------------------------------------

# ----------------------------------------------------------------------------
# - set default variables                                                    -
# ----------------------------------------------------------------------------

# the root prefix
prefix=
# the bin directory
bindir=
# the lib directory
libdir=
# the binary program to execute
binexe='afnix'
# the binary program options
binopt=
# the programs to run
runpgm=
# whether or not we are verbose
verbose=no
# whether or not we loop with the arguments
noloop=no

# ----------------------------------------------------------------------------
# - local function always make life easier                                   -
# ----------------------------------------------------------------------------

# print a usage message
usage () {
    echo "usage: afnix-aexec [options] file..."
    echo "       -h | --help        print this help message"
    echo "       -v | --verbose     set verbose mode"
    echo "       -n | --noloop      do not loop with arguments"
    echo
    echo "       --prefix=dir       set top directory"
    echo "       --bindir=dir       set bin directory"
    echo "       --libdir=dir       set lib directory"
    echo "       --binexe=file      set binary program"
    echo "       --binopt=options   set binary program options"
    exit 0
}

# print an error message
error () {
    echo "afnix-aexec: $1"
    exit 1
}

# compute the directories
getdir () {
    if test -z "$bindir" ; then
	bindir=$prefix/bin
    fi
    if test ! -d "$bindir" ; then
	error "cannot find bin directory"
    fi
    if test -z "$libdir" ; then
	libdir=$prefix/lib
    fi
}

# run the program
dorun () {
    # check if the executable exists
    pexe=$bindir/$binexe
    if test ! -x "$pexe" ; then
	error "cannot find binary executable"
    fi
    # check for verbose mode
    if test "$verbose" = "yes"; then
	echo "running: $1"
    fi
    # execute the program
    ld_lib_path=$libdir:$LD_LIBRARY_PATH
    LD_LIBRARY_PATH=$ld_lib_path DYLD_LIBRARY_PATH=$ld_lib_path \
                    $pexe $binopt $1
    if [ "$?" != "0" ]; then
	error "failure $1"
    fi
}

# ----------------------------------------------------------------------------
# - parse options - this is where we really start                            -
# ----------------------------------------------------------------------------

# parse the options
lastopt=
for nextopt
do
    # assign the previous option argument
    if test -n "$lastopt"; then
	eval "$lastopt=\$nextopt"
	lastopt=
	continue
    fi

    # extract options
    case "$nextopt" in
    -*=*) argsopt=`echo "$nextopt" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
    *) argsopt= ;;
    esac

    # process options now
    case "$nextopt" in
    -h | --help)             usage ;;
    -v | --verbose)          verbose=yes ;;
    -n | --noloop)           noloop=yes ;;

    --prefix)                lastopt=prefix ;;
    --prefix=*)              prefix="$argsopt" ;;

    --bindir)                lastopt=bindir ;;
    --bindir=*)              bindir="$argsopt" ;;

    --libdir)                lastopt=libdir ;;
    --libdir=*)              libdir="$argsopt:$libdir" ;;

    --binexe)                lastopt=binexe ;;
    --binexe=*)              binexe="$argsopt" ;;
    
    --binopt)                lastopt=binopt ;;
    --binopt=*)              binopt="$argsopt" ;;

    -*)                      error "illegal option $nextopt" ;;
     *)                      runpgm="$runpgm $nextopt" ;;
    esac
done

# process the directories
getdir

# loop or not in the files and run
if test "$noloop" = "yes"; then
    dorun "$runpgm"
else
    for f in $runpgm ; do
	dorun $f
    done
fi

# that's all folks
exit 0
