]> git.donarmstrong.com Git - neurodebian.git/blob - tools/nd-autoinstall
Allow for custom title; charge "tasklist"
[neurodebian.git] / tools / nd-autoinstall
1 #!/bin/bash
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:
4
5 # play save
6 set -e
7 set -u
8
9 ############
10 # Defaults #
11 ############
12
13 nd_autoinstall_version=0.1
14
15 # To be set by cmdline args
16 ai_envfile=
17 ai_package=
18 ai_verbose=
19 ai_dialog_title=
20 ai_force=0
21
22 print_verbose()
23 {
24         [ -z "$ai_verbose" ] || echo "I: $*"
25 }
26
27 print_version()
28 {
29 cat << EOT
30 nd-autoinstall $nd_autoinstall_version
31
32 Copyright (C) 2010 Yaroslav Halchenko <debian@onerussian.com>
33
34 Licensed under GNU Public License version 3 or later.
35 This is free software; see the source for copying conditions.  There is NO
36 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
37
38 Written by Yaroslav Halchenko for the NeuroDebian project.
39
40 EOT
41 }
42
43
44 print_help()
45 {
46 cat << EOT
47
48 Usage:  nd-autoinstall [options] COMMAND [command_options]
49
50 Runs the COMMAND if it is available, otherwise installs PACKAGE first,
51 and then runs the command.  If an environment file is specified, it
52 gets sourced first (PACKAGE gets installed if environment file is not
53 available).
54
55 Options:
56
57   -p, --package=PACKAGE
58     Name of the package to be installed if named differently than
59     COMMAND.
60
61   -e, --environment-file=FILE
62     File to be sourced before invocation of the COMMAND.  If not found,
63     PACKAGE gets installed first.
64
65   -f, --force
66     Skip all checks and install package(s) provided via --package.
67
68   -v, --verbose
69     Enable additional progress messages.
70
71   -t, --title
72     Optinal title of the installation dialog.
73
74   -h, --help
75     Print short description, usage summary and option list.
76
77   --version
78     Print version information and exit.
79
80 Exit status:
81
82   exit status of the COMMAND -
83       if COMMAND is available (or got sucesfully installed)
84   2 - incorrect invocation of nd-autoinstall
85   3 - PACKAGE installation failure
86 EOT
87 }
88
89 ################################
90 # Commandline options handling #
91 ################################
92
93 # Parse commandline options (taken from the getopt examples from the Debian util-linux package)
94 # Note that we use `"$@"' to let each command-line parameter expand to a
95 # separate word. The quotes around `$@' are essential!
96 # We need CLOPTS as the `eval set --' would nuke the return value of getopt.
97 CLOPTS=`getopt -o h,e:,p:,t:,f,v --long help,version,environment-file:,package:,title:,verbose,force -n 'nd-autoinstall' -- "$@"`
98
99 if [ $? != 0 ] ; then
100   echo "Terminating..." >&2
101   exit 2
102 fi
103
104 # Note the quotes around `$CLOPTS': they are essential!
105 eval set -- "$CLOPTS"
106
107 while true ; do
108   case "$1" in
109           -e|--environment-file) shift; ai_envfile=$1; shift;;
110           -f|--force) ai_force=1; shift;;
111           -p|--package) shift; ai_package=$1; shift;;
112           -t|--title) shift; ai_dialog_title=$1; shift;;
113           -v|--verbose) ai_verbose=1; shift;;
114           -h|--help) print_help; exit 0;;
115           --version) print_version; exit 0;;
116           --) shift ; break ;;
117           *) echo "Internal error! ($1)"; exit 1;;
118   esac
119 done
120
121
122 if [ $# -lt 1 ] ; then
123     print_help >&2
124     exit 2
125 fi
126
127 ai_command=$1; shift
128 [ -z "$ai_package" ] && ai_package=$ai_command
129
130 [ -z "$ai_dialog_title" ] && ai_dialog_title="Package installation"
131
132 ai_envfile_failed=
133 if [ ! -z "$ai_envfile" ]; then
134         source "$ai_envfile" || ai_envfile_failed=1
135 fi
136
137 do_install=0
138 if [ ! -z "$ai_envfile_failed" ]; then
139         do_install=1
140 fi
141 if which $ai_command > /dev/null || [ -e $ai_command ]; then
142         do_install=0
143 else
144         do_install=1
145 fi
146 if [ $ai_force -eq 1 ]; then
147         do_install=1
148 fi
149
150 if [ $do_install -eq 1 ]; then
151     if [ $ai_force -eq 0 ]; then
152                 if ! zenity --question \
153                         --text="To run '$ai_command', $ai_package package needs to be installed.  Do you want to proceed?"; then
154                         exit 2
155                 fi
156         fi
157
158     print_verbose "Need to install $ai_package to run $ai_command"
159
160     logfile=$(mktemp -u /tmp/nd-autoinstall-XXXXXX.log)
161
162     { SUDO_ASKPASS="/usr/bin/ssh-askpass" /usr/bin/sudo -A \
163                 DEBIAN_FRONTEND=gnome /usr/bin/apt-get install -y $ai_package 2>&1 \
164         && rm -f $logfile; } \
165         | tee $logfile \
166         | zenity --title="$ai_dialog_title" \
167                          --text="Installing $ai_package" \
168                  --progress --pulsate --auto-close --auto-kill
169     if [ -e $logfile ] ; then
170         zenity --title="Installation of $ai_package has failed: see $logfile" \
171             --window-icon=error \
172             --width=800 --height=600 \
173             --text-info --filename=$logfile
174         exit 3
175     fi
176         [ -z "$ai_envfile_failed" ] || source "$ai_envfile" || {
177                 zenity --text="Failed to source $ai_envfile even after installing $ai_package" \
178                         --window-icon=error --error
179                 exit 1
180         }
181 fi
182
183 if [ -x $ai_command ]; then
184         # only run if executable
185         print_verbose "Running $ai_command"
186         $ai_command "$@"
187 fi