| 1 |
#!/bin/sh |
| 2 |
# ---------------------------------------------------------------------------- |
| 3 |
# - afnix-bexec - |
| 4 |
# - afnix binary 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 lib directory |
| 25 |
libdir= |
| 26 |
# the library path |
| 27 |
ldpath= |
| 28 |
dlpath= |
| 29 |
# the programs to run |
| 30 |
runpgm= |
| 31 |
# show the options |
| 32 |
shwopt="no" |
| 33 |
# arguments loop option |
| 34 |
noloop="no" |
| 35 |
|
| 36 |
# ---------------------------------------------------------------------------- |
| 37 |
# - local function always make life easier - |
| 38 |
# ---------------------------------------------------------------------------- |
| 39 |
|
| 40 |
# print a usage message |
| 41 |
usage () { |
| 42 |
echo "usage: afnix-bexec [options] file..." |
| 43 |
echo " -h print this help message" |
| 44 |
echo " -v set verbose mode" |
| 45 |
echo " -n do not loop with arguments" |
| 46 |
echo |
| 47 |
echo " --noloop do not loop with arguments" |
| 48 |
echo " --prefix set top directory" |
| 49 |
echo " --libdir set lib directory" |
| 50 |
exit 0 |
| 51 |
} |
| 52 |
|
| 53 |
# print an error message |
| 54 |
error () { |
| 55 |
echo "afnix-bexec: $1" |
| 56 |
exit 1 |
| 57 |
} |
| 58 |
|
| 59 |
# compute the directories |
| 60 |
getdir () { |
| 61 |
# check library path |
| 62 |
if test -z "$libdir" ; then |
| 63 |
libdir="$prefix/lib" |
| 64 |
fi |
| 65 |
# set the library path |
| 66 |
if test -z "$LD_LIBRARY_PATH"; then |
| 67 |
ldpath="$libdir" |
| 68 |
else |
| 69 |
ldpath="$libdir:$LD_LIBRARY_PATH" |
| 70 |
fi |
| 71 |
if test -z "$DYLD_LIBRARY_PATH"; then |
| 72 |
dlpath="$libdir" |
| 73 |
else |
| 74 |
dlpath="$libdir:$DYLD_LIBRARY_PATH" |
| 75 |
fi |
| 76 |
} |
| 77 |
|
| 78 |
# run the program |
| 79 |
dorun () { |
| 80 |
# check for verbose mode |
| 81 |
if test "$shwopt" = "yes"; then |
| 82 |
echo "running: $1" |
| 83 |
fi |
| 84 |
# execute the program |
| 85 |
LD_LIBRARY_PATH=$ldpath DYLD_LIBRARY_PATH=$dlpath ./$1 |
| 86 |
if [ "$?" != "0" ]; then |
| 87 |
error "failure $1" |
| 88 |
fi |
| 89 |
} |
| 90 |
|
| 91 |
# ---------------------------------------------------------------------------- |
| 92 |
# - parse options - this is where we really start - |
| 93 |
# ---------------------------------------------------------------------------- |
| 94 |
|
| 95 |
# parse the options |
| 96 |
preopt= |
| 97 |
for nxtopt |
| 98 |
do |
| 99 |
# assign the previous option argument |
| 100 |
if test -n "$preopt"; then |
| 101 |
eval "$preopt=\$nxtopt" |
| 102 |
preopt= |
| 103 |
continue |
| 104 |
fi |
| 105 |
|
| 106 |
# extract options |
| 107 |
case "$nxtopt" in |
| 108 |
-*=*) argopt=`echo "$nxtopt" | sed 's/[-_a-zA-Z0-9]*=//'`;; |
| 109 |
*) argopt=;; |
| 110 |
esac |
| 111 |
|
| 112 |
# process options now |
| 113 |
case "$nxtopt" in |
| 114 |
-h) usage;; |
| 115 |
-v) shwopt="yes";; |
| 116 |
-n) noloop="yes";; |
| 117 |
|
| 118 |
--noloop) noloop="yes";; |
| 119 |
--prefix) preopt=prefix;; |
| 120 |
--prefix=*) prefix="$argopt";; |
| 121 |
--libdir) preopt=libdir;; |
| 122 |
--libdir=*) libdir="$argopt:$libdir";; |
| 123 |
|
| 124 |
-*) error "illegal option $nxtopt";; |
| 125 |
*) runpgm="$runpgm $nxtopt";; |
| 126 |
esac |
| 127 |
done |
| 128 |
|
| 129 |
# process the directories |
| 130 |
getdir |
| 131 |
|
| 132 |
# loop or not in the files and run |
| 133 |
if test "$noloop" = "yes"; then |
| 134 |
dorun "$runpgm" |
| 135 |
else |
| 136 |
for f in $runpgm ; do |
| 137 |
dorun $f |
| 138 |
done |
| 139 |
fi |
| 140 |
|
| 141 |
# that's all folks |
| 142 |
exit 0 |