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
25 [ -z "$ai_verbose" ] || echo "I: $*"
31 nd-autoinstall $nd_autoinstall_version
33 Copyright (C) 2010 Yaroslav Halchenko <debian@onerussian.com>
35 Licensed under GNU Public License version 3 or later.
36 This is free software; see the source for copying conditions. There is NO
37 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
39 Written by Yaroslav Halchenko for the NeuroDebian project.
49 Usage: nd-autoinstall [options] COMMAND [command_options]
51 Runs the COMMAND if it is available, otherwise installs PACKAGE first,
52 and then runs the command. If an environment file is specified, it
53 gets sourced first (PACKAGE gets installed if environment file is not
59 Name of the package to be installed if named differently than
62 -e, --environment-file=FILE
63 File to be sourced before invocation of the COMMAND. If not found,
64 PACKAGE gets installed first.
67 Skip all checks and install package(s) provided via --package.
70 If specified file is not present, install the PACKAGE and run
74 Enable additional progress messages.
77 Optinal title of the installation dialog.
80 Print short description, usage summary and option list.
83 Print version information and exit.
87 exit status of the COMMAND -
88 if COMMAND is available (or got sucesfully installed)
89 2 - incorrect invocation of nd-autoinstall
90 3 - PACKAGE installation failure
94 ################################
95 # Commandline options handling #
96 ################################
98 # Parse commandline options (taken from the getopt examples from the Debian util-linux package)
99 # Note that we use `"$@"' to let each command-line parameter expand to a
100 # separate word. The quotes around `$@' are essential!
101 # We need CLOPTS as the `eval set --' would nuke the return value of getopt.
102 CLOPTS=`getopt -o h,e:,p:,t:,f,F:,v --long help,version,environment-file:,package:,title:,verbose,force,file: -n 'nd-autoinstall' -- "$@"`
104 if [ $? != 0 ] ; then
105 echo "Terminating..." >&2
109 # Note the quotes around `$CLOPTS': they are essential!
110 eval set -- "$CLOPTS"
114 -e|--environment-file) shift; ai_envfile="$1"; shift;;
115 -f|--force) ai_force=1; shift;;
116 -F|--file) shift; ai_file="$1"; shift;;
117 -p|--package) shift; ai_package="$1"; shift;;
118 -t|--title) shift; ai_dialog_title="$1"; shift;;
119 -v|--verbose) ai_verbose=1; shift;;
120 -h|--help) print_help; exit 0;;
121 --version) print_version; exit 0;;
123 *) echo "Internal error! ($1)"; exit 1;;
128 if [ $# -lt 1 ] ; then
133 ai_command="$1"; shift
134 [ -z "$ai_package" ] && ai_package=$(basename "$ai_command")
136 [ -z "$ai_dialog_title" ] && ai_dialog_title="Package installation"
139 if [ ! -z "$ai_envfile" ]; then
140 source "$ai_envfile" || ai_envfile_failed=1
144 # Decide either installation is necessary
147 if [ ! -z "$ai_envfile_failed" ]; then
148 print_verbose "Environment file $ai_envfile failed to source. Install $ai_package"
151 if [ ! -z "$ai_file" ] && [ ! -e "$ai_file" ]; then
152 print_verbose "File $ai_file absent. Install $ai_package"
155 if ! which "$ai_command" > /dev/null && [ ! -e "$ai_command" ]; then
156 print_verbose "Command $ai_command is not available. Install $ai_package"
159 if [ $ai_force -eq 1 ]; then
160 print_verbose "Force installation of $ai_package"
164 if [ $do_install -eq 1 ]; then
165 # Figure out amount of space to download/occupy
166 space_info="$(LC_ALL=C apt-get --trivial-only --print-uris install $ai_package 2>/dev/null \
167 | grep -e '^\(Need to get \|After this\)' || : )"
168 if [ $ai_force -eq 0 ]; then
169 if ! zenity --question \
170 --text="To run '$ai_command', $ai_package package needs to be installed.
174 Do you want to proceed?"; then
179 print_verbose "Need to install $ai_package to run $ai_command"
181 logfile=$(mktemp -u /tmp/nd-autoinstall-XXXXXX.log)
183 { SUDO_ASKPASS="/usr/bin/ssh-askpass" /usr/bin/sudo -A \
184 DEBIAN_FRONTEND=gnome /usr/bin/apt-get install -y $ai_package 2>&1 \
185 && rm -f $logfile; } \
187 | zenity --title="$ai_dialog_title" \
188 --text="Installing $ai_package
190 --progress --pulsate --auto-close --auto-kill
191 if [ -e $logfile ] ; then
192 zenity --title="Installation of $ai_package has failed: see $logfile" \
193 --window-icon=error \
194 --width=800 --height=600 \
195 --text-info --filename=$logfile
198 [ -z "$ai_envfile_failed" ] || source "$ai_envfile" || {
199 zenity --text="Failed to source $ai_envfile even after installing $ai_package" \
200 --window-icon=error --error
205 if which $ai_command > /dev/null 2>&1; then
206 # only run if executable
207 print_verbose "Running $ai_command"