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