]> git.donarmstrong.com Git - neurodebian.git/blob - tools/nd-autoinstall
to avoid using duplication whenever command has the same name as package -- added...
[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 # Not used atm
16 #ai_install_cmd="/usr/bin/gksudo '/usr/bin/aptitude install -y @PACKAGE@'"
17 #ai_install_cmd_terminal=no
18
19 # To be set by cmdline args
20 ai_envfile=
21 ai_package=
22 ai_verbose=
23
24 print_verbose()
25 {
26         [ -z "$ai_verbose" ] || echo "I: $*"
27 }
28
29 print_version()
30 {
31 cat << EOT
32 nd-autoinstall $nd_autoinstall_version
33
34 Copyright (C) 2010 Yaroslav Halchenko <debian@onerussian.com>
35
36 Licensed under GNU Public License version 3 or later.
37 This is free software; see the source for copying conditions.  There is NO
38 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
39
40 Written by Yaroslav Halchenko for the NeuroDebian project.
41
42 EOT
43 }
44
45
46 print_help()
47 {
48 cat << EOT
49
50 Usage:  nd-autoinstall [options] COMMAND [command_options]
51
52 Runs the COMMAND if it is available, otherwise installs PACKAGE first,
53 and then runs the command.  If an environment file is specified, it
54 gets sourced first (PACKAGE gets installed if environment file is not
55 available).
56
57 Options:
58
59   -p, --package=PACKAGE
60     Name of the package to be installed if named differently than
61     COMMAND.
62
63   -e, --environment-file=FILE
64     File to be sourced before invocation of the COMMAND.  If not found,
65     PACKAGE gets installed first.
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:,v --long help,version,environment-file:,package:,verbose, -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           -p|--package) shift; ai_package=$1; shift;;
107           -v|--verbose) ai_verbose=1; shift;;
108           -h|--help) print_help; exit 0;;
109           --version) print_version; exit 0;;
110           --) shift ; break ;;
111           *) echo "Internal error! ($1)"; exit 1;;
112   esac
113 done
114
115
116 if [ $# -lt 1 ] ; then
117     print_help >&2
118     exit 2
119 fi
120
121 ai_command=$1; shift
122 [ -z "$ai_package" ] && ai_package=$ai_command
123
124 ai_envfile_failed=
125 if [ ! -z "$ai_envfile" ]; then
126         source "$ai_envfile" || ai_envfile_failed=1
127 fi
128
129 if [ ! -z "$ai_envfile_failed" ] || ! which $ai_command >/dev/null; then
130     if ! zenity --question \
131         --text="To run '$ai_command', $ai_package package needs to be installed.  Do you want to proceed?"; then
132         exit 2
133     fi
134
135     print_verbose "Need to install $ai_package to run $ai_command"
136     #cmd=$ai_install_cmd
137     #[ "x$ai_install_cmd_terminal" = xyes ] \
138 #       && cmd="/usr/bin/xterm -e /bin/bash -c '$cmd'"
139     cmd="${ai_install_cmd//@PACKAGE@/$ai_package}"
140     #/usr/bin/gksudo "/usr/bin/aptitude install -y $ai_package" > /tmp/nd-autoinstall.log 2>&1
141     # debconf has a nice debconf-apt-progress but it would require
142     # root access to each of commands for little benefit
143     #
144     # . /usr/share/debconf/confmodule
145     # debconf-apt-progress --start
146     # debconf-apt-progress --from 0 --to 100 -- apt-get -y install $ai_package
147     # debconf-apt-progress --from 45 --to 90 -- apt-get -y install kde
148     # debconf-apt-progress --from 90 --to 100 -- apt-get -y install xfce4
149     # debconf-apt-progress --stop
150
151     logfile=$(mktemp -u /tmp/nd-autoinstall-XXXXXX.log)
152     # * aptitude for some reason does not return fail exit code
153     # * bloody gksudo swallows all stderr output (#599233) :-/
154     # So we just initiate it first to get the password
155     { /usr/bin/gksudo ls > /dev/null; }
156     # and then revert to sudo
157     # and enforce a GNOME front-end since otherwise things might go wrong
158     # and there is no way to enforce just some GUI frontend
159     { /usr/bin/sudo DEBIAN_FRONTEND=gnome /usr/bin/apt-get install -y $ai_package 2>&1 \
160         && rm -f $logfile; } \
161         | tee $logfile \
162         | zenity --title="nd-autoinstall" \
163                          --text="Installing $ai_package" \
164                  --progress --pulsate --auto-close --auto-kill
165     if [ -e $logfile ] ; then
166         zenity --title="Installation of $ai_package has failed: see $logfile" \
167             --window-icon=error \
168             --width=800 --height=600 \
169             --text-info --filename=$logfile
170         exit 3
171     fi
172         [ -z "$ai_envfile_failed" ] || source "$ai_envfile" || {
173                 zenity --text="Failed to source $ai_envfile even after installing $ai_package" \
174                         --window-icon=error --error
175                 exit 1
176         }
177 fi
178
179 print_verbose "Running $ai_command"
180 $ai_command "$@"