Release new version 2.13.0
[grml-scripts.git] / usr_bin / grml-exec-wrapper
index a826be1..4e5a3cc 100755 (executable)
@@ -1,4 +1,4 @@
-#!/bin/bash
+#!/bin/sh
 # Filename:      grml-exec-wrapper
 # Purpose:       simple but smart program execution wrapper
 # Authors:       grml-team (grml.org), (c) Michael Prokop <mika@grml.org>
@@ -6,42 +6,84 @@
 # License:       This file is licensed under the GPL v2 or any later version.
 ################################################################################
 
+PN=$(basename "$0")
+
+usage() {
+  printf "Usage: %s <program> [<arguments>]
+
+Available options:
+
+  -h | --help   display this usage information
+  -p <program>  check specified argument instead of default args, like:
+                %s -p aumix xterm -T aumix -e aumix\n" "${PN}" "${PN}"
+}
+
+is_installed() {
+    prog="$1"
+    [ -z "$prog" ] && return 1
+
+    case "${prog}" in
+        /*) # deal with absolute-path programs
+            [ -x "${prog}" ] && return 0
+            return 1 ;;
+    esac
+
+    ret=1
+    oifs="$IFS"
+    IFS=:
+    for dir in $PATH; do
+        [ -z "$dir" ] && continue
+        [ -x "$dir/$prog" ] && ret=0 && break
+    done
+
+    IFS="$oifs"
+    unset oifs
+    return "$ret"
+}
+
 # use Xdisplay only if present and running under X:
 display_info() {
-if type -a Xdialog 1>/dev/null 2>&1 && test -n "$DISPLAY" ; then
-    Xdialog --title "grml-exec-wrapper" --msgbox "$1" 0 0 0
+if is_installed xterm && test -n "$DISPLAY" ; then
+    xterm -T 'Sorry :(' -e "dialog --msgbox \"$1\" 0 0"
 else
-    print "$1">&2
+    printf '%s\n' "$1">&2
 fi
 }
 
-if [ -z "$1" ] ; then
-    display_info "Usage: $0 <program> [<arguments>]"
+if [ -z "$1" ] || [ "$1" = '-h' ] || [ "$1" = '--help' ] ; then
+    usage
     exit 1
 fi
 
-RC='0'
-PROG="$1"
+if [ "$1" = '-p' ] ; then
+    if [ -z "$2" ] ; then
+         usage
+         exit 1
+    else
+         PROG="$2"
+         shift ; shift
+    fi
+else
+    PROG="$1"
+    # make sure to support 'grml-exec-wrapper sudo wireshark' as well:
+    case $PROG in
+      *sudo*) PROG="$2" ;;
+    esac
+fi
 
-# make sure to support 'grml-exec-wrapper sudo wireshark' as well:
-case $PROG in
-       *sudo*) PROG="$2" ;;
-esac
+if is_installed "$PROG" ; then
+    exec "$@"
+fi
 
-if type -a "$PROG" 1>/dev/null 2>&1 ; then
-    exec $@
-else
-    RC=1
-    display_info "Sorry: ${PROG} not available.
+display_info "Sorry: ${PROG} not available.
 
 Looks like the grml flavour you are using does not ship ${PROG}. :(
 
 You can search for ${PROG} executing:
 
-apt-get update && apt-cache search ${PROG}
+apt-get update && apt-cache search $(basename "${PROG}")
     "
-fi
 
-exit $RC
+exit 1
 
 ## END OF FILE #################################################################