Merge branch 't/default_gsu_dir'
[gsu.git] / subcommand
index 6449d6e..95e0ba9 100644 (file)
@@ -1,9 +1,10 @@
 #!/bin/bash
 # (C) 2006-2011 Andre Noll
 
-if [[ $(type -t gsu_is_a_number) != "function" ]]; then
-       GSU_DIR=${GSU_DIR:=${HOME-}/.gsu}
-       . $GSU_DIR/common || exit 1
+if [[ "$(type -t _gsu_setup)" != "function" ]]; then
+       gsu_dir=${gsu_dir:-${BASH_SOURCE[0]%/*}}
+       . $gsu_dir/common || exit 1
+       _gsu_setup
 fi
 
 _gsu_usage()
@@ -110,11 +111,11 @@ _com_prefs()
                ret=-$E_GSU_MKDIR
                result="${conf%/*}"
                mkdir -p "$result"
-               [[ $? -ne 0 ]] && return
+               (($? != 0)) && return
                ret=-$E_GSU_EDITOR
                result="${EDITOR:-vi}"
                "$result" "$conf"
-               [[ $? -ne 0 ]] && return
+               (($? != 0)) && return
                ret=$GSU_SUCCESS
                return
        fi
@@ -180,9 +181,7 @@ _com_man()
        _gsu_available_commands
         for com in $result; do
                num=${#com}
-               if test $num -lt 4; then
-                       num=4
-               fi
+               (($num < 4)) && num=4
                 echo "${minus_signs:0:$num}"
                 echo "$com"
                 echo "${minus_signs:0:$num}"
@@ -332,7 +331,7 @@ gsu_getopts()
 '
 
        gsu_check_arg_count $# 1 1
-       if [[ $ret -lt 0 ]]; then
+       if (($ret < 0)); then
                gsu_err_msg
                exit 1
        fi
@@ -526,10 +525,9 @@ gsu_get_unnamed_arg_num()
 gsu()
 {
        local i
-       _gsu_setup
        _gsu_available_commands
        gsu_cmds="$result"
-       if test $# -eq 0; then
+       if (($# == 0)); then
                _gsu_usage
                _gsu_print_available_commands
                exit 1
@@ -539,7 +537,7 @@ gsu()
        # check internal commands
        if [[ "$arg" = "help" || "$arg" = "man" || "$arg" = "prefs" || "$arg" = "complete" ]]; then
                _com_$arg "$@"
-               if [[ "$ret" -lt 0 ]]; then
+               if (("$ret" < 0)); then
                        gsu_err_msg
                        exit 1
                fi
@@ -550,7 +548,7 @@ gsu()
        for i in $gsu_cmds; do
                if test "$arg" = "$i"; then
                        com_$arg "$@"
-                       if [[ "$ret" -lt 0 ]]; then
+                       if (("$ret" < 0)); then
                                gsu_err_msg
                                exit 1
                        fi
@@ -581,16 +579,16 @@ gsu()
 gsu_check_arg_count()
 {
        ret=-$E_GSU_BAD_ARG_COUNT
-       if [[ $# -eq 2 ]]; then # only num1 is given
+       if (($# == 2)); then # only num1 is given
                result="at least $2 args required, $1 given"
-               [[ $1 -lt $2 ]] && return
+               (($1 < $2)) && return
                ret=$GSU_SUCCESS
                return
        fi
        # num1 and num2 given
        result="need at least $2 args, $1 given"
-       [[ $1 -lt $2 ]] && return
+       (($1 < $2)) && return
        result="need at most $3 args, $1 given"
-       [[ $1 -gt $3 ]] && return
+       (($1 > $3)) && return
        ret=$GSU_SUCCESS
 }