]> git.donarmstrong.com Git - neurodebian.git/blob - tools/nd-autoinstall
BF: use SUDO_ASKPASS + sudo (do not use gksu for a variety of reasons)
[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 if [ ! -z "$ai_envfile_failed" ] || ! which $ai_command >/dev/null; then
126     if ! zenity --question \
127         --text="To run '$ai_command', $ai_package package needs to be installed.  Do you want to proceed?"; then
128         exit 2
129     fi
130
131     print_verbose "Need to install $ai_package to run $ai_command"
132
133     logfile=$(mktemp -u /tmp/nd-autoinstall-XXXXXX.log)
134
135     { SUDO_ASKPASS="/usr/bin/ssh-askpass" /usr/bin/sudo -A \
136                 DEBIAN_FRONTEND=gnome /usr/bin/apt-get install -y $ai_package 2>&1 \
137         && rm -f $logfile; } \
138         | tee $logfile \
139         | zenity --title="nd-autoinstall" \
140                          --text="Installing $ai_package" \
141                  --progress --pulsate --auto-close --auto-kill
142     if [ -e $logfile ] ; then
143         zenity --title="Installation of $ai_package has failed: see $logfile" \
144             --window-icon=error \
145             --width=800 --height=600 \
146             --text-info --filename=$logfile
147         exit 3
148     fi
149         [ -z "$ai_envfile_failed" ] || source "$ai_envfile" || {
150                 zenity --text="Failed to source $ai_envfile even after installing $ai_package" \
151                         --window-icon=error --error
152                 exit 1
153         }
154 fi
155
156 print_verbose "Running $ai_command"
157 $ai_command "$@"