]> git.donarmstrong.com Git - neurodebian.git/blob - tools/nd-autoinstall
Also for stats report which repo and which job number use our setup
[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 safe
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 ai_file=
22
23 print_verbose()
24 {
25         [ -z "$ai_verbose" ] || echo "I: $*"
26 }
27
28 print_version()
29 {
30 cat << EOT
31 nd-autoinstall $nd_autoinstall_version
32
33 Copyright (C) 2010 Yaroslav Halchenko <debian@onerussian.com>
34
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.
38
39 Written by Yaroslav Halchenko for the NeuroDebian project.
40
41 EOT
42 }
43
44
45 print_help()
46 {
47 cat << EOT
48
49 Usage:  nd-autoinstall [options] COMMAND [command_options]
50
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
54 available).
55
56 Options:
57
58   -p, --package=PACKAGE
59     Name of the package to be installed if named differently than
60     COMMAND.
61
62   -e, --environment-file=FILE
63     File to be sourced before invocation of the COMMAND.  If not found,
64     PACKAGE gets installed first.
65
66   -f, --force
67     Skip all checks and install package(s) provided via --package.
68
69   -F, --file=FILE
70     If specified file is not present, install the PACKAGE and run
71     the COMMAND.
72
73   -v, --verbose
74     Enable additional progress messages.
75
76   -t, --title
77     Optinal title of the installation dialog.
78
79   -h, --help
80     Print short description, usage summary and option list.
81
82   --version
83     Print version information and exit.
84
85 Exit status:
86
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
91 EOT
92 }
93
94 ################################
95 # Commandline options handling #
96 ################################
97
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' -- "$@"`
103
104 if [ $? != 0 ] ; then
105   echo "Terminating..." >&2
106   exit 2
107 fi
108
109 # Note the quotes around `$CLOPTS': they are essential!
110 eval set -- "$CLOPTS"
111
112 while true ; do
113   case "$1" in
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;;
122           --) shift ; break ;;
123           *) echo "Internal error! ($1)"; exit 1;;
124   esac
125 done
126
127
128 if [ $# -lt 1 ] ; then
129     print_help >&2
130     exit 2
131 fi
132
133 ai_command="$1"; shift
134 [ -z "$ai_package" ] && ai_package=$(basename "$ai_command")
135
136 [ -z "$ai_dialog_title" ] && ai_dialog_title="Package installation"
137
138 ai_envfile_failed=
139 if [ ! -z "$ai_envfile" ]; then
140         source "$ai_envfile" || ai_envfile_failed=1
141 fi
142
143 #
144 # Decide either installation is necessary
145 #
146 do_install=0
147 if [ ! -z "$ai_envfile_failed" ]; then
148     print_verbose "Environment file $ai_envfile failed to source. Install $ai_package"
149         do_install=1
150 fi
151 if [ ! -z "$ai_file" ] && [ ! -e "$ai_file" ]; then
152     print_verbose "File $ai_file absent. Install $ai_package"
153     do_install=1
154 fi
155 if ! which "$ai_command" > /dev/null && [ ! -e "$ai_command" ]; then
156     print_verbose "Command $ai_command is not available. Install $ai_package"
157         do_install=1
158 fi
159 if [ $ai_force -eq 1 ]; then
160     print_verbose "Force installation of $ai_package"
161         do_install=1
162 fi
163
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.
171
172 $space_info
173
174 Do you want to proceed?"; then
175                         exit 2
176                 fi
177         fi
178
179     print_verbose "Need to install $ai_package to run $ai_command"
180
181     logfile=$(mktemp -u /tmp/nd-autoinstall-XXXXXX.log)
182
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; } \
186         | tee $logfile \
187         | zenity --title="$ai_dialog_title" \
188                          --text="Installing $ai_package
189 $space_info" \
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
196         exit 3
197     fi
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
201                 exit 1
202         }
203 fi
204
205 if which $ai_command > /dev/null 2>&1; then
206         # only run if executable
207         print_verbose "Running $ai_command"
208         $ai_command "$@"
209 fi