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