-
-
Notifications
You must be signed in to change notification settings - Fork 86
/
o-saft
executable file
·246 lines (234 loc) · 9.31 KB
/
o-saft
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#!/bin/sh
#?
#? NAME
#? $0 - simple wrapper to call o-saft.pl or o-saft.tcl
#?
#? SYNOPSIS
#? $0 [options] target
#?
#? DESCRIPTION
#? Calls o-saft.pl or o-saft.tcl with all specified arguments.
#? o-saft.pl's output is piped to the command specified with the -post=
#? option. The program given there must be located in usr/ directory, or
#? or must be found via PATH environment variable. The default is cat,
#? which means that all output is on STDOUT.
#?
#? OPTIONS
#? -h got it
# -help got it
#? -n just show command to be executed
#? -help=* grep for * in 'o-saft.pl --help'
# TODO: should also grep in --help=commads and some other programs
#? -list list available programs in ./usr/ directory
#? -cli start o-saft.pl with remaining arguments; this is the default
#? -cgi start o-saft.cgi with remaining arguments
#? -gui start o-saft.tcl with remaining arguments
# -tcl alias for -gui
#? -log start o-saft.pl with remaining arguments and store result in
#? file, then start start o-saft.tcl loading this file
#? (option intended when system cannot exec other programs)
#? -docker same as -podman but uses docker instead of podman
#? -podman start o-saft-docker with remaining arguments
#? -id=* passed to o-saft-docker
#? -tag=* passed to o-saft-docker
# -prg="program"
# use program instead of o-saft.pl; intended for debugging only
#? -post="program [options]"
#? program in ./usr to pipe output of o-saft.pl to
#? -colour alias for -post=bunt.pl
#? -blind alias for -post='bunt.pl --blind'
#? -line alias for -post='bunt.pl --line'
#? -less pipe output of o-saft.pl to less
#? -more pipe output of o-saft.pl to more
#? -- pass all remaining arguments to o-saft.pl or o-saft.tcl
#?
#? LIMITATIONS
#? All option listed above must preceed arguments to be passed through.
#? $0 must be installed in the same directory as the tools it will call
#? $0.pl $0.cgi $0.tcl
#?
#? If -log option is used, the options -help=* -docker* and -post=*
#? are silently ignored.
#?
#? Option +VERSION prints version of this program and not the version
#? of o-saft.pl .
#?
#? Automatic detection of GUI mode fails, if the starter in the desktop's
#? panel (or whatever functionality is used) uses a terminal.
#?
#? EXAMPLES
#? $0 +cmd --option target
#? $0 -post='bunt.pl' ' +cmd --option target
#? $0 -post='bunt.pl -blind' +cmd --option target
#? $0 -colour +cmd --option target # same as above
#? $0 -gui target
#? $0 -gui +cmd target
#? $0 -log +cmd target
# $0 -prg=yeast.pl +cmd target
#?
#? $0 -docker +cmd --option target
#? $0 -docker -post='bunt.pl' ' +cmd --option target
#? $0 -docker -post='bunt.pl -blind' +cmd --option target
#? $0 -docker -gui target
#?
#? Specify program to execute script (if shebang in script file fails):
#? $0 -docker -post='perl usr/bunt.pl' ' +cmd --option target
#?
#
# Hacker's INFO
# Uses options -h and -post= and not --h or --help or --post to
# avoid conflicts with same option for o-saft.pl .
#
#? VERSION
#? @(#) o-saft 3.6 24/09/24 00:29:42
#?
#? AUTHOR
#? 17-dec-17 Achim Hoffmann
#?
# -----------------------------------------------------------------------------
ich=${0##*/}
dir=${0%/*}; [ "$dir" = "$0" ] && dir="." # $0 found via $PATH in .
log=$$.$USER
[ -n "$OSAFT_MAKE" ] && log=OSAFT_MAKE
# avoid random name, which confuses diff of generated output (i.e. with make)
log=/tmp/$ich.$log.log;
usr=$dir/usr
prg=$dir/$ich.pl # most likely ich=o-saft ..
gui=$dir/$ich.tcl
cgi=$dir/$ich.cgi
dok=$dir/$ich-docker
pod=$dir/$ich-podman # not used, just for documentation (see $podman below also)
bunt=$usr/bunt.pl
# all tools are prefix with the path $dir to avoid incomplete $PATH settings
# then check (below) if tools exists, if not, remove path and rely on $PATH
[ ! -x "$prg" ] && prg=$ich.pl
[ ! -x "$gui" ] && gui=$ich.tcl
[ ! -x "$cgi" ] && cgi=$ich.cgi
[ ! -x "$dok" ] && dok=$ich-docker
[ ! -x "$pod" ] && dok=$ich-docker # fallback
try=
args= # argumets passed through (mainly --help)
mode=cli # cli, cgi, gui, log
post="cat" # default, avoids special handling if -post= missing
post_args= # aguments passed to post command, mainly used for -help=
suchen= # text to be searched for passed to post command, mainly used for -help=
podman= # set to -podman : use podman instead of docker
docker=0 # cli or gui mode; cannot use $mode because it is needed for docker too
docker_id=
docker_tag=
# podman is similar to docker, it just differs herein with the -podman option
# all variables are named docker* (for historical reason) even if podman used
#dbx# echo "#################" >> /tmp/t.o-saft.log
#dbx# [ -t 0 ] &&echo 0 STDIN >> /tmp/t.o-saft.log
#dbx# [ -t 1 ] &&echo 1 STDOUT >> /tmp/t.o-saft.log
#dbx# [ -t 2 ] &&echo 2 STDERR >> /tmp/t.o-saft.log
[ -t 0 ] || mode=gui # no STDIN, assuming start from desktop, hence GUI
while [ $# -gt 0 ]; do
case "$1" in
-help | -h | '-?')
\sed -ne "s/\$0/$ich/g" -e '/^#?/s/#?//p' $0
\cat <<EoT
NOTE
To get help for o-saft.pl, please use:
$0 --help
or
o-saft.pl --help
EoT
exit 0
;;
+VERSION) echo "3.6"; exit 0; ;;
-help=*)
suchen="`expr "$1" ':' '-help=\(.*\)'`"
args="--help"
post=egrep
post_args="--color -i -C 3 "
;;
-list)
for exe in $usr/* ; do
[ -d "$exe" ] && continue
[ -x "$exe" ] && echo "$exe"
done
exit 0
;;
-n | --n) try=echo ; ;;
-v | --v) ;;
-x | --x) set -x ; ;;
-cgi | --cgi) mode=cgi ; ;;
-cli | --cli) mode=cli ; ;;
-gui | --gui) mode=gui ; ;;
-tcl | --tcl) mode=gui ; ;;
-log | --log) mode=log ; ;;
-docker | --docker) docker=1 ; ;;
-podman | --podman) podman="-podman" ; ;; # will be passed to o-saft-docker
-id*) docker_id="$1" ; ;;
-tag*) docker_tag="$1" ; ;;
-colour | --colour) post="$bunt" ; ;;
-color | --color) post="$bunt" ; ;;
-blind | --blind) post="$bunt --blind" ; ;;
-line | --line) post="$bunt --line" ; ;;
-less | --less) post="less" ; ;;
-more | --more) post="more" ; ;;
-prg=*) prg="`expr "$1" ':' '-prg=\(.*\)'`" ; ;; # for debugging only
-post=*)
post="`expr "$1" ':' '-post=\(.*\)'`" ;
cmd=`echo "$post" | awk '{print $1}'` # remove additional trailing arguments
[ -x "$usr/$cmd" ] && post="$usr/$post"
shift
break
;;
--post=*)
post="`expr "$1" ':' '--post=\(.*\)'`" ;
cmd=`echo "$post" | awk '{print $1}'` # remove additional trailing arguments
[ -x "$usr/$cmd" ] && post="$usr/$post"
shift
break
;;
--) shift; break; ;;
*) break; ;;
esac
shift
done
# from here on 'podman' is the same as 'docker'
if [ -n "$podman" ]; then
docker=1
# dok=$pod # not set because $pod is a symlink to $dok and my be missing
fi
# Note: using $* (instead of $@) does not contain parsed options
[ -n "$try" ] && echo "# docker=$docker, podman=$podman, dok=$dok, mode=$mode, post=$post, $*#"
# docker mode is special
if [ $docker -eq 1 ]; then
if [ -z "$osaft_vm_build" ]; then
echo $dok $docker_id $docker_tag $podman -$mode -post=$post $*
[ -n "$try" ] && exit 0
$dok $docker_id $docker_tag $podman -$mode -post=$post $*
exit $?
fi
#else
echo "**WARNING: option -docker|-podman ignored inside VM"
fi
# all other modes
case "$mode" in
cgi) $try $cgi --cgi $* | $post ; ;;
gui) $try $gui --post="$post" $* ; ;;
log)
[ -e $log -a -z "$OSAFT_MAKE" ] && echo "**ERROR: '$log' exists; aborted" && exit 2
echo "$prg $* $args > $log" ; echo "$gui $* --load=$log"
$try $prg $* $args > $log && $try $gui $* --load=$log && rm $log
# dirty hack: passing $* to $gui as it assumes they behave the
# same way there as for $prg
;;
*) echo "$prg $docker_id $docker_tag $* $args | $post $post_args $suchen"
[ -n "$try" ] && exit 0
# some shell behave strange when quotes are needed (for $suchen here)
# hence different command lines are needed wether $suchen is empty or not
if [ -n "$suchen" ]; then
$prg $docker_id $docker_tag $* $args | $post $post_args "$suchen"
else
$prg $docker_id $docker_tag $* $args | $post $post_args
fi
;;
esac
exit $?
# NOTE: $gui
# all parameters are passed to $gui, even they are not yet (since 2018)
# used properly, but most likely treated as hostname