2 # gsu -- the global subcommand utility
3 # (C) 2006-2009 Andre Noll
9 E_GSU_BAD_COMMAND invalid command
10 E_GSU_NOT_A_NUMBER not a number
11 E_GSU_SOURCE error in config file
12 E_GSU_CONFIG bad/missing config file option
28 export -f _gsu_init_errors
30 # check if $1 is a number
34 if test "$1" -eq "$1" &> /dev/null; then
37 ret=-$E_GSU_NOT_A_NUMBER
40 export -f gsu_is_a_number
46 export -f gsu_short_msg
50 gsu_short_msg "$_gsu_self: $1"
56 gsu_short_msg "$_gsu_self $(date): $1"
58 export -f gsu_date_msg
62 local txt="*** $_gsu_self --"
63 if test -z "$gsu_banner_txt"; then
64 txt="$txt set \$gsu_banner_txt to customize this message"
66 txt="$txt $gsu_banner_txt"
68 gsu_short_msg "$txt ***"
70 export -f _gsu_banner_msg
74 local txt="$result" err
76 gsu_is_a_number "$ret"
77 if test $ret -lt 0; then
78 gsu_msg "unknown error ($ret:$txt)"
81 if test $result -ge 0; then
82 gsu_msg "unknown error ($result:$txt)"
86 if test -n "$txt"; then
87 txt="$txt: ${gsu_error_txt[$err]}"
89 txt="${gsu_error_txt[$err]}"
97 gsu_short_msg "Usage: $_gsu_self command [options]"
101 _gsu_available_commands()
103 result="$( (printf "help\nman\n"; grep "^com_[a-z_]\+()" $0) \
104 | sed -e 's/^com_//' -e 's/()//' \
109 export -f _gsu_available_commands
111 _gsu_print_available_commands()
114 gsu_short_msg "Available commands:"
115 for i in $gsu_cmds; do
117 count=$(($count + 1))
118 if test $(($count % 4)) -eq 0; then
122 if test ${#i} -lt 8; then
130 export -f _gsu_print_available_commands
139 local equal_signs="=================================================="
140 local minus_signs="--------------------------------------------------"
143 echo "$_gsu_self (_${gsu_banner_txt}_) manual"
144 echo "${equal_signs:0:${#_gsu_self} + ${#gsu_banner_txt} + 16}"
147 sed -e '1,/^#\{70,\}/d' -e '/^#\{70,\}/,$d' $0 -e 's/^# *//'
150 echo "$_gsu_self usage"
151 echo "${minus_signs:0:${#_gsu_self} + 6}"
154 echo "Each command has its own set of options as described below."
158 echo "Available commands:"
160 _gsu_available_commands
161 for com in $result; do
163 if test $num -lt 4; then
166 echo "${minus_signs:0:$num}"
168 echo "${minus_signs:0:$num}"
176 export gsu_help_txt="
179 Usage: help [command]
181 Without arguments, print the list of available commands. Otherwise,
182 print the help text for the given command."
187 if test -z "$1"; then
191 (printf "com_help()\n$gsu_help_txt" | head -n 4; echo "--"
192 printf "com_man()\n$gsu_man_txt" | head -n 4; echo "--"
194 grep -A 2 "^com_\([a-zA-Z_0-9]\+\)()" $0) \
196 | sed -e '/^com_\([a-zA-Z_0-9]\+\)()/bs' \
197 -e 'H;$!d;x;s/\n//g;b' \
199 -e 'x;s/\n//g;${p;x;}' \
200 | sed -e 's/^com_\([a-zA-Z_0-9]\+\)()#*/\1\t/' \
204 if test ${#a} -lt 8; then
210 echo "Try $_gsu_self help <command> for info on <command>."
214 if test "$1" = "help"; then
219 if test "$1" = "man"; then
225 if grep -q "^com_$1()" $0; then
226 sed -e "1,/com_$1()/d" -e '/^{/,$d' -e 's/^## *//' $0
229 _gsu_print_available_commands
231 ret=-$E_GSU_BAD_COMMAND
237 local name val default_val required ty comment
240 while read name default_val required ty comment; do
241 if test -z "$name"; then
244 eval ${gsu_self}_$name="$default_val"
248 result="$HOME/.${gsu_self}rc"
249 # overwrite by custom configuration
250 if [ -r "$result" ]; then
252 if ! . "$result"; then
257 while read name default_val required ty comment; do
258 [ -z "$name" ] && continue
260 # abort if any required config var remains unset
262 if [ "$val" = "-" -a "$required" = "required" ]; then
267 if [ $ty == "number" ]; then
268 gsu_is_a_number "$val"
269 if [ $ret -lt 0]; then
274 eval export ${gsu_self}_$name
279 export -f _gsu_init_config
285 _gsu_self="$(basename $0)"
288 _gsu_available_commands
290 if test $# -eq 0; then
292 _gsu_print_available_commands
297 for i in $gsu_cmds; do
298 if test "$arg" = "$i"; then
300 if test $ret -lt 0; then
307 ret=-$E_GSU_BAD_COMMAND
310 _gsu_print_available_commands