check4progs: Change testing from ‘which’ to iterating over $PATH [Closes: issue1284]
[grml-etc-core.git] / etc / grml / script-functions
1 # Filename:      /etc/grml/script-functions
2 # Purpose:       some often used functions for use in shellscripts
3 # Authors:       grml-team (grml.org), (c) Michael Prokop <mika@grml.org>
4 # Bug-Reports:   see http://grml.org/bugs/
5 # License:       This file is licensed under the GPL v2.
6 ################################################################################
7
8 # {{{ set default PATH
9 setpath(){
10   export PATH=${PATH:-'/bin:/sbin:/usr/bin:/usr/sbin:/usr/X11R6/bin:/usr/local/bin'}
11 }
12 # }}}
13
14 # {{{ check for root-permissions
15 check4root(){
16   if [ "$(id -u 2>/dev/null)" != 0 ] ; then
17     echo 1>&2 "Error: please run this script with uid 0 (root)." ; return 1
18   fi
19 }
20 # }}}
21
22 # {{{ check for user permissions
23 check4user(){
24   if [ "$(id -u 2>/dev/null)" = 0 ] ; then
25     echo 1>&2 "Error: please do not run this script with uid 0 (root)." ; return 1
26   fi
27 }
28 # }}}
29
30 # {{{ check for running zsh
31 iszsh(){
32   if ! [ -z "$ZSH_VERSION" ] ; then
33     return 0
34   else
35     return 1
36   fi
37 }
38 # }}}
39
40 # {{{ check for (X)dialog
41 setdialog(){
42   if [ -n "$DISPLAY" ] ; then
43      [ -x /usr/bin/Xdialog ] && DIALOG="Xdialog" && export XDIALOG_HIGH_DIALOG_COMPAT=1
44   else
45      [ -x /usr/bin/dialog ] && DIALOG='dialog' || ( echo 1>&2 "dialog not available" ; return 1 )
46   fi
47 }
48 # }}}
49
50 # {{{ check for availability of program(s)
51 # usage example:
52 #       check4progs [-s,-q,--quiet,--silent] arg [arg .... argn]
53 #
54 # with option given either of:
55 #       -s,-q,--quiet,--silent
56 #
57 # check for available progs but produce no output
58 check4progs() {
59     [ -n "${ZSH_VERSION}" ] && emulate -L sh
60     local RTN=0
61     local oldifs="${IFS}"
62     local ARG d found
63     local VERBOSE=1
64
65     case ${1} in
66         -q | -s | --quiet | --silent)
67             VERBOSE=0
68             shift 1
69             ;;
70
71         *)
72             ;;
73     esac
74
75     while [ $# -gt 0 ]
76     do
77         ARG="$1"
78         shift
79
80         found=0
81         IFS=:
82         for d in $PATH
83         do
84             if [ -x "${d}/${ARG}" ]
85             then
86                 found=1
87                 break
88             fi
89         done
90         IFS="${oldifs}"
91
92         # check for availability
93         if [ ${found} -eq 0 ]
94         then
95             if [ ${VERBOSE} -eq 1 ]
96             then
97                 printf "%s: binary not found\n" "${ARG}" >&2
98             fi
99             RTN=1
100         fi
101     done
102
103     # return non zero, if at least one prog is missing!
104     return $RTN
105 }
106 # }}}
107
108 # {{{ simple shell grep
109 stringinfile(){
110   case "$(cat $2)" in *$1*) return 0;; esac
111   return 1
112 }
113 # }}}
114
115 # {{{ simple shell grep for strings
116 stringinstring(){
117   case "$2" in *$1*) return 0;; esac
118   return 1
119 }
120 # }}}
121
122 # {{{ reread boot command line; echo last parameter's argument or return false.
123 getbootparam(){
124   CMDLINE=$(cat /proc/cmdline)
125   stringinstring " $1=" "$CMDLINE" || return 1
126   result="${CMDLINE##*$1=}"
127   result="${result%%[   ]*}"
128   echo "$result"
129   return 0
130 }
131 # }}}
132
133 # {{{ check boot commandline for specified option
134 checkbootparam(){
135   stringinfile " $1" /proc/cmdline
136   return "$?"
137 }
138 # }}}
139
140 # {{{ check whether $1 is yes
141 checkvalue(){
142   if [ "$1" = "yes" -o "$1" = "YES" ] ; then
143     return 0
144   else
145     return 1
146   fi
147 }
148 # }}}
149
150 # {{{ grml specific checks
151 isgrml(){
152   [ -f /etc/grml_version ] && return 0 || return 1
153 }
154
155 grmlversion(){
156  cat /etc/grml_version
157 }
158
159 isgrmlcd(){
160   [ -f /etc/grml_cd ] && return 0 || return 1
161 }
162
163 isgrmlhd(){
164   [ -f /etc/grml_cd ] && return 1 || return 0
165 }
166
167 checkgrmlsmall(){
168   grep -q small /etc/grml_version 2>/dev/null && return 0 || return 1
169 }
170 # }}}
171
172 # {{{ filesystems (proc, pts, sys)
173 mount_proc(){
174   check4root || return 1
175   [ -f /proc/version ] || mount -t proc /proc /proc 2>/dev/null
176 }
177
178 mount_pts(){
179   check4root || return 1
180   stringinfile "/dev/pts" /proc/mounts || mount -t devpts /dev/pts /dev/pts 2>/dev/null
181 }
182
183 mount_sys(){
184   check4root || return 1
185   [ -d /sys/devices ] || mount -t sysfs /sys /sys 2>/dev/null
186 }
187 # }}}
188
189 # char *reverse_list(list) {{{
190 #
191 #   Returns the reversed order of list
192 #
193 reverse_list() {
194   local ret
195   ret=''
196   while [ "$#" -gt 0 ] ; do
197     if [ -z "${ret}" ] ; then
198       ret="$1"
199     else
200       ret="$1 ${ret}"
201     fi
202     shift
203   done
204   printf '%s' "${ret}"
205 }
206 #}}}
207
208 # bool is_older_than(reference, files/dirs to check) {{{
209 #
210 #   return 0 if any of the files/dirs are newer than
211 #   the reference file
212 #
213 #   EXAMPLE: if is_older_than a.out *.o ; then ...
214 is_older_than() {
215   local x
216   local ref="$1"
217   shift
218
219   for x in "$@" ; do
220     [ "${x}" -nt "${ref}" ] && return 0
221
222     if [ -d "${x}" ] ; then
223       is_older_than "${ref}" "${x}"/* && return 0
224     fi
225   done
226
227   return 1
228 }
229 #}}}
230
231 ## END OF FILE #################################################################
232 # vim:foldmethod=marker tw=80 ai expandtab shiftwidth=2 tabstop=8 ft=sh