exec-wrapper: deal with absolute path names in is_installed exec-wrapper [rebase...
[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 usage() {
10   printf "Usage: $0 <program> [<arguments>]
11
12 Available options:
13
14   -h | --help   display this usage information
15   -p <program>  check specified argument instead of default args, like:
16                 $(basename $0) -p aumix xterm -T aumix -e aumix
17 "
18 }
19
20 is_installed() {
21     prog="$1"
22     [ -z "$prog" ] && return 1
23
24     case "${prog}" in
25         /*) # deal with absolute-path programs
26             [ -x "${prog}" ] && return 0
27             return 1 ;;
28     esac
29
30     ret=1
31
32     ret=1
33     oifs="$IFS"
34     IFS=:
35     for dir in $PATH; do
36         [ -z "$dir" ] && continue
37         [ -x "$dir/$prog" ] && ret=0 && break
38     done
39
40     IFS="$oifs"
41     unset oifs
42     return "$ret"
43 }
44
45 # use Xdisplay only if present and running under X:
46 display_info() {
47 if is_installed xterm && test -n "$DISPLAY" ; then
48     xterm -T 'Sorry :(' -e "dialog --msgbox \"$1\" 0 0"
49 else
50     printf '%s\n' "$1">&2
51 fi
52 }
53
54 if [ -z "$1" -o "$1" = '-h' -o "$1" = '--help' ] ; then
55     usage
56     exit 1
57 fi
58
59 if [ "$1" = '-p' ] ; then
60     if [ -z "$2" ] ; then
61          usage
62          exit 1
63     else
64          PROG="$2"
65          shift ; shift
66     fi
67 else
68     PROG="$1"
69     # make sure to support 'grml-exec-wrapper sudo wireshark' as well:
70     case $PROG in
71       *sudo*) PROG="$2" ;;
72     esac
73 fi
74
75 if is_installed "$PROG" ; then
76     exec "$@"
77 fi
78
79 display_info "Sorry: ${PROG} not available.
80
81 Looks like the grml flavour you are using does not ship ${PROG}. :(
82
83 You can search for ${PROG} executing:
84
85 apt-get update && apt-cache search $(basename ${PROG})
86     "
87
88 exit 1
89
90 ## END OF FILE #################################################################