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 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     oifs="$IFS"
32     IFS=:
33     for dir in $PATH; do
34         [ -z "$dir" ] && continue
35         [ -x "$dir/$prog" ] && ret=0 && break
36     done
37
38     IFS="$oifs"
39     unset oifs
40     return "$ret"
41 }
42
43 # use Xdisplay only if present and running under X:
44 display_info() {
45 if is_installed xterm && test -n "$DISPLAY" ; then
46     xterm -T 'Sorry :(' -e "dialog --msgbox \"$1\" 0 0"
47 else
48     printf '%s\n' "$1">&2
49 fi
50 }
51
52 if [ -z "$1" -o "$1" = '-h' -o "$1" = '--help' ] ; then
53     usage
54     exit 1
55 fi
56
57 if [ "$1" = '-p' ] ; then
58     if [ -z "$2" ] ; then
59          usage
60          exit 1
61     else
62          PROG="$2"
63          shift ; shift
64     fi
65 else
66     PROG="$1"
67     # make sure to support 'grml-exec-wrapper sudo wireshark' as well:
68     case $PROG in
69       *sudo*) PROG="$2" ;;
70     esac
71 fi
72
73 if is_installed "$PROG" ; then
74     exec "$@"
75 fi
76
77 display_info "Sorry: ${PROG} not available.
78
79 Looks like the grml flavour you are using does not ship ${PROG}. :(
80
81 You can search for ${PROG} executing:
82
83 apt-get update && apt-cache search $(basename ${PROG})
84     "
85
86 exit 1
87
88 ## END OF FILE #################################################################