]> git.donarmstrong.com Git - neurodebian.git/blob - tools/nd-autoinstall
Allow nd-autoinstall to test just file existance not only command.
[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
20 print_verbose()
21 {
22         [ -z "$ai_verbose" ] || echo "I: $*"
23 }
24
25 print_version()
26 {
27 cat << EOT
28 nd-autoinstall $nd_autoinstall_version
29
30 Copyright (C) 2010 Yaroslav Halchenko <debian@onerussian.com>
31
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.
35
36 Written by Yaroslav Halchenko for the NeuroDebian project.
37
38 EOT
39 }
40
41
42 print_help()
43 {
44 cat << EOT
45
46 Usage:  nd-autoinstall [options] COMMAND [command_options]
47
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
51 available).
52
53 Options:
54
55   -p, --package=PACKAGE
56     Name of the package to be installed if named differently than
57     COMMAND.
58
59   -e, --environment-file=FILE
60     File to be sourced before invocation of the COMMAND.  If not found,
61     PACKAGE gets installed first.
62
63   -v, --verbose
64     Enable additional progress messages.
65
66   -h, --help
67     Print short description, usage summary and option list.
68
69   --version
70     Print version information and exit.
71
72 Exit status:
73
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
78 EOT
79 }
80
81 ################################
82 # Commandline options handling #
83 ################################
84
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' -- "$@"`
90
91 if [ $? != 0 ] ; then
92   echo "Terminating..." >&2
93   exit 2
94 fi
95
96 # Note the quotes around `$CLOPTS': they are essential!
97 eval set -- "$CLOPTS"
98
99 while true ; do
100   case "$1" in
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;;
106           --) shift ; break ;;
107           *) echo "Internal error! ($1)"; exit 1;;
108   esac
109 done
110
111
112 if [ $# -lt 1 ] ; then
113     print_help >&2
114     exit 2
115 fi
116
117 ai_command=$1; shift
118 [ -z "$ai_package" ] && ai_package=$ai_command
119
120 ai_envfile_failed=
121 if [ ! -z "$ai_envfile" ]; then
122         source "$ai_envfile" || ai_envfile_failed=1
123 fi
124
125 do_install=0
126 if [ ! -z "$ai_envfile_failed" ]; then
127         do_install=1
128 fi
129 if which $ai_command > /dev/null || [ -e $ai_command ]; then
130         do_install=0
131 else
132         do_install=1
133 fi
134
135 if [ $do_install -eq 1 ]; then
136     if ! zenity --question \
137         --text="To run '$ai_command', $ai_package package needs to be installed.  Do you want to proceed?"; then
138         exit 2
139     fi
140
141     print_verbose "Need to install $ai_package to run $ai_command"
142
143     logfile=$(mktemp -u /tmp/nd-autoinstall-XXXXXX.log)
144
145     { SUDO_ASKPASS="/usr/bin/ssh-askpass" /usr/bin/sudo -A \
146                 DEBIAN_FRONTEND=gnome /usr/bin/apt-get install -y $ai_package 2>&1 \
147         && rm -f $logfile; } \
148         | tee $logfile \
149         | zenity --title="Package installation" \
150                          --text="Installing $ai_package" \
151                  --progress --pulsate --auto-close --auto-kill
152     if [ -e $logfile ] ; then
153         zenity --title="Installation of $ai_package has failed: see $logfile" \
154             --window-icon=error \
155             --width=800 --height=600 \
156             --text-info --filename=$logfile
157         exit 3
158     fi
159         [ -z "$ai_envfile_failed" ] || source "$ai_envfile" || {
160                 zenity --text="Failed to source $ai_envfile even after installing $ai_package" \
161                         --window-icon=error --error
162                 exit 1
163         }
164 fi
165
166 if [ -x $ai_command ]; then
167         # only run if executable
168         print_verbose "Running $ai_command"
169         $ai_command "$@"
170 fi