| 1 |
#!/bin/sh |
| 2 |
# ---------------------------------------------------------------------------- |
| 3 |
# - afnix-aexec - |
| 4 |
# - afnix als execution script - |
| 5 |
# ---------------------------------------------------------------------------- |
| 6 |
# - This program is free software; you can redistribute it and/or modify - |
| 7 |
# - it provided that this copyright notice is kept intact. - |
| 8 |
# - - |
| 9 |
# - This program is distributed in the hope that it will be useful, but - |
| 10 |
# - without any warranty; without even the implied warranty of - |
| 11 |
# - merchantability or fitness for a particular purpose. In not event shall - |
| 12 |
# - the copyright holder be liable for any direct, indirect, incidental or - |
| 13 |
# - special damages arising in any way out of the use of this software. - |
| 14 |
# ---------------------------------------------------------------------------- |
| 15 |
# - copyright (c) 1999-2023 amaury darsch - |
| 16 |
# ---------------------------------------------------------------------------- |
| 17 |
|
| 18 |
# ---------------------------------------------------------------------------- |
| 19 |
# - set default variables - |
| 20 |
# ---------------------------------------------------------------------------- |
| 21 |
|
| 22 |
# the root prefix |
| 23 |
prefix= |
| 24 |
# the bin directory |
| 25 |
bindir= |
| 26 |
# the lib directory |
| 27 |
libdir= |
| 28 |
# the library path |
| 29 |
ldpath= |
| 30 |
dlpath= |
| 31 |
# the binary program to execute |
| 32 |
binexe="afnix" |
| 33 |
# the binary program options |
| 34 |
binopt= |
| 35 |
# the programs to run |
| 36 |
runpgm= |
| 37 |
# show the options |
| 38 |
shwopt="no" |
| 39 |
# arguments loop option |
| 40 |
noloop="no" |
| 41 |
|
| 42 |
# ---------------------------------------------------------------------------- |
| 43 |
# - local function always make life easier - |
| 44 |
# ---------------------------------------------------------------------------- |
| 45 |
|
| 46 |
# print a usage message |
| 47 |
usage () { |
| 48 |
echo "usage: afnix-aexec [options] file..." |
| 49 |
echo " -h print this help message" |
| 50 |
echo " -v set verbose mode" |
| 51 |
echo " -n do not loop with arguments" |
| 52 |
echo |
| 53 |
echo " --noloop do not loop with arguments" |
| 54 |
echo " --prefix set top directory" |
| 55 |
echo " --bindir set bin directory" |
| 56 |
echo " --libdir set lib directory" |
| 57 |
echo " --binexe set binary program" |
| 58 |
echo " --binopt set binary program options" |
| 59 |
exit 0 |
| 60 |
} |
| 61 |
|
| 62 |
# print an error message |
| 63 |
error () { |
| 64 |
echo "afnix-aexec: $1" |
| 65 |
exit 1 |
| 66 |
} |
| 67 |
|
| 68 |
# compute the directories |
| 69 |
getdir () { |
| 70 |
# set and check directories |
| 71 |
if test -z "$bindir"; then |
| 72 |
bindir="$prefix/bin" |
| 73 |
fi |
| 74 |
if test ! -d "$bindir"; then |
| 75 |
error "cannot find bin directory $bindir" |
| 76 |
fi |
| 77 |
if test -z "$libdir"; then |
| 78 |
libdir="$prefix/lib" |
| 79 |
fi |
| 80 |
# set the library path |
| 81 |
if test -z "$LD_LIBRARY_PATH"; then |
| 82 |
ldpath="$libdir" |
| 83 |
else |
| 84 |
ldpath="$libdir:$LD_LIBRARY_PATH" |
| 85 |
fi |
| 86 |
if test -z "$DYLD_LIBRARY_PATH"; then |
| 87 |
dlpath="$libdir" |
| 88 |
else |
| 89 |
dlpath="$libdir:$DYLD_LIBRARY_PATH" |
| 90 |
fi |
| 91 |
} |
| 92 |
|
| 93 |
# run the program |
| 94 |
dorun () { |
| 95 |
# check if the executable exists |
| 96 |
pthexe="$bindir/$binexe" |
| 97 |
if test ! -x "$pthexe" ; then |
| 98 |
error "cannot find binary executable $pthexe" |
| 99 |
fi |
| 100 |
# check for verbose mode |
| 101 |
if test "$shwopt" = "yes"; then |
| 102 |
echo "running: $1" |
| 103 |
fi |
| 104 |
# execute the program |
| 105 |
LD_LIBRARY_PATH=$ldpath DYLD_LIBRARY_PATH=$dlpath $pthexe $binopt $1 |
| 106 |
if [ "$?" != "0" ]; then |
| 107 |
error "failure $1" |
| 108 |
fi |
| 109 |
} |
| 110 |
|
| 111 |
# ---------------------------------------------------------------------------- |
| 112 |
# - parse options - this is where we really start - |
| 113 |
# ---------------------------------------------------------------------------- |
| 114 |
|
| 115 |
# parse the options |
| 116 |
preopt= |
| 117 |
for nxtopt |
| 118 |
do |
| 119 |
# assign the previous option argument |
| 120 |
if test -n "$preopt"; then |
| 121 |
eval "$preopt=\$nxtopt" |
| 122 |
preopt= |
| 123 |
continue |
| 124 |
fi |
| 125 |
|
| 126 |
# extract options |
| 127 |
case "$nxtopt" in |
| 128 |
-*=*) argopt=`echo "$nxtopt" | sed 's/[-_a-zA-Z0-9]*=//'`;; |
| 129 |
*) argopt=;; |
| 130 |
esac |
| 131 |
|
| 132 |
# process options now |
| 133 |
case "$nxtopt" in |
| 134 |
-h) usage;; |
| 135 |
-v) shwopt="yes";; |
| 136 |
-n) noloop="yes";; |
| 137 |
|
| 138 |
--noloop) noloop="yes";; |
| 139 |
--prefix) preopt=prefix;; |
| 140 |
--prefix=*) prefix="$argopt";; |
| 141 |
--bindir) preopt=bindir;; |
| 142 |
--bindir=*) bindir="$argopt";; |
| 143 |
--libdir) preopt=libdir;; |
| 144 |
--libdir=*) libdir="$argopt:$libdir";; |
| 145 |
--binexe) preopt=binexe;; |
| 146 |
--binexe=*) binexe="$argopt";; |
| 147 |
--binopt) preopt=binopt;; |
| 148 |
--binopt=*) binopt="$argopt";; |
| 149 |
|
| 150 |
-*) error "illegal option $nxtopt";; |
| 151 |
*) runpgm="$runpgm $nxtopt";; |
| 152 |
esac |
| 153 |
done |
| 154 |
|
| 155 |
# process the directories |
| 156 |
getdir |
| 157 |
|
| 158 |
# loop or not in the files and run |
| 159 |
if test "$noloop" = "yes"; then |
| 160 |
dorun "$runpgm" |
| 161 |
else |
| 162 |
for f in $runpgm; do |
| 163 |
dorun $f |
| 164 |
done |
| 165 |
fi |
| 166 |
|
| 167 |
# that's all folks |
| 168 |
exit 0 |