Add ist_installed(), thanks ft!
[grml-scripts.git] / usr_bin / grml-exec-wrapper
1 #!/bin/sh
2 # Filename:      grml-exec-wrapper
3 # Purpose:       simple but smart program execution wrapper
4 # Authors:       grml-team (grml.org), (c) Michael Prokop <mika@grml.org>
5 # Bug-Reports:   see http://grml.org/bugs/
6 # License:       This file is licensed under the GPL v2 or any later version.
7 ################################################################################
8
9 # use Xdisplay only if present and running under X:
10 display_info() {
11 if type -a Xdialog 1>/dev/null 2>&1 && test -n "$DISPLAY" ; then
12     Xdialog --title "grml-exec-wrapper" --msgbox "$1" 0 0 0
13 else
14     print "$1">&2
15 fi
16 }
17
18 if [ -z "$1" ] ; then
19     display_info "Usage: $0 <program> [<arguments>]"
20     exit 1
21 fi
22
23 RC='0'
24 PROG="$1"
25
26 # make sure to support 'grml-exec-wrapper sudo wireshark' as well:
27 case $PROG in
28         *sudo*) PROG="$2" ;;
29 esac
30
31 is_installed() {
32     prog="$1"
33     [ -z "$prog" ] && return 1
34
35     ret=1
36     oifs="$IFS"
37     IFS=:
38     for dir in $PATH; do
39         [ -z "$dir" ] && continue
40         [ -x "$dir/$prog" ] && ret=0 && break
41     done
42
43     IFS="$oifs"
44     unset oifs
45     return "$ret"
46 }
47
48 if is_installed "$PROG" 1>/dev/null 2>&1 ; then
49     exec $@
50 else
51     RC=1
52     display_info "Sorry: ${PROG} not available.
53
54 Looks like the grml flavour you are using does not ship ${PROG}. :(
55
56 You can search for ${PROG} executing:
57
58 apt-get update && apt-cache search ${PROG}
59     "
60 fi
61
62 exit $RC
63
64 ## END OF FILE #################################################################