2 #emacs: -*- mode: shell-script; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil -*-
3 #ex: set sts=4 ts=4 sw=4 et:
13 nd_autoinstall_version=0.1
15 # To be set by cmdline args
22 [ -z "$ai_verbose" ] || echo "I: $*"
28 nd-autoinstall $nd_autoinstall_version
30 Copyright (C) 2010 Yaroslav Halchenko <debian@onerussian.com>
32 Licensed under GNU Public License version 3 or later.
33 This is free software; see the source for copying conditions. There is NO
34 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
36 Written by Yaroslav Halchenko for the NeuroDebian project.
46 Usage: nd-autoinstall [options] COMMAND [command_options]
48 Runs the COMMAND if it is available, otherwise installs PACKAGE first,
49 and then runs the command. If an environment file is specified, it
50 gets sourced first (PACKAGE gets installed if environment file is not
56 Name of the package to be installed if named differently than
59 -e, --environment-file=FILE
60 File to be sourced before invocation of the COMMAND. If not found,
61 PACKAGE gets installed first.
64 Enable additional progress messages.
67 Print short description, usage summary and option list.
70 Print version information and exit.
74 exit status of the COMMAND -
75 if COMMAND is available (or got sucesfully installed)
76 2 - incorrect invocation of nd-autoinstall
77 3 - PACKAGE installation failure
81 ################################
82 # Commandline options handling #
83 ################################
85 # Parse commandline options (taken from the getopt examples from the Debian util-linux package)
86 # Note that we use `"$@"' to let each command-line parameter expand to a
87 # separate word. The quotes around `$@' are essential!
88 # We need CLOPTS as the `eval set --' would nuke the return value of getopt.
89 CLOPTS=`getopt -o h,e:,p:,v --long help,version,environment-file:,package:,verbose, -n 'nd-autoinstall' -- "$@"`
92 echo "Terminating..." >&2
96 # Note the quotes around `$CLOPTS': they are essential!
101 -e|--environment-file) shift; ai_envfile=$1; shift;;
102 -p|--package) shift; ai_package=$1; shift;;
103 -v|--verbose) ai_verbose=1; shift;;
104 -h|--help) print_help; exit 0;;
105 --version) print_version; exit 0;;
107 *) echo "Internal error! ($1)"; exit 1;;
112 if [ $# -lt 1 ] ; then
118 [ -z "$ai_package" ] && ai_package=$ai_command
121 if [ ! -z "$ai_envfile" ]; then
122 source "$ai_envfile" || ai_envfile_failed=1
125 if [ ! -z "$ai_envfile_failed" ] || ! which $ai_command >/dev/null; then
126 if ! zenity --question \
127 --text="To run '$ai_command', $ai_package package needs to be installed. Do you want to proceed?"; then
131 print_verbose "Need to install $ai_package to run $ai_command"
133 logfile=$(mktemp -u /tmp/nd-autoinstall-XXXXXX.log)
135 { SUDO_ASKPASS="/usr/bin/ssh-askpass" /usr/bin/sudo -A \
136 DEBIAN_FRONTEND=gnome /usr/bin/apt-get install -y $ai_package 2>&1 \
137 && rm -f $logfile; } \
139 | zenity --title="nd-autoinstall" \
140 --text="Installing $ai_package" \
141 --progress --pulsate --auto-close --auto-kill
142 if [ -e $logfile ] ; then
143 zenity --title="Installation of $ai_package has failed: see $logfile" \
144 --window-icon=error \
145 --width=800 --height=600 \
146 --text-info --filename=$logfile
149 [ -z "$ai_envfile_failed" ] || source "$ai_envfile" || {
150 zenity --text="Failed to source $ai_envfile even after installing $ai_package" \
151 --window-icon=error --error
156 print_verbose "Running $ai_command"