#!/bin/sh
# ----------------------------------------------------------------------------
# - afnix-bexec                                                              -
# - afnix binary 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 lib directory
libdir=
# 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-bexec [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 "       --libdir=dir       set lib directory"
    exit 0
}

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

# compute the directories
getdir () {
    if test -z "$libdir" ; then
	libdir=$prefix/lib
    fi
}

# run the program
dorun () {
    # 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 ./$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" ;;

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

    -*)                      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
