Bug 8215: Followup FIX QA issues
[koha.git] / install_misc / ubuntu-packages.sh
1 #!/bin/bash
2
3 # Copyright 2012 Universidad Nacional de Cordoba
4 # Written by Tomas Cohen Arazi
5 #            Mark Tompsett
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
12 # version.
13 #
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License along
19 # with Koha; if not, write to the Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
22 # Output simple help
23 usage() {
24     local scriptname=$(basename $0)
25     cat <<EOF
26 $scriptname
27
28 Query for missing dependencies. Print the install command for them if specified.
29
30 Usage:
31 $scriptname -r
32 $scriptname -ic
33 $scriptname -h
34
35     -r  | --report             Report the status of Koha's dependencies
36     -ic | --install-command    Display the install command for the missing dependencies
37     -h  | --help               Display this help message
38 EOF
39 }
40
41 # Check if the package is installed
42 packageInstalled() {
43     local package=$1
44     local package_status=`dpkg-query --showformat='${Status}' \
45                           -W $package 2> /dev/null`
46
47     if [ "$package_status" == "install ok installed" ] ; then
48         echo "yes"
49     else
50         echo "no"
51     fi
52 }
53
54 # Get the installed package version
55 getPackageVersion() {
56     local package=$1
57     dpkg-query --showformat='${Version}' -W $package
58 }
59
60 # A parameter is required.
61 if [ "$#" -eq "0" ]; then
62     usage
63     exit 1
64 fi
65
66 # Initialize variables
67 CHECK=no
68 INSTALLCMD=no
69 HELP=no
70
71 # Loop over parameters
72 while (( "$#" )); do
73     case $1 in
74         -r | --report )
75             CHECK=yes
76             ;;
77         -ic | --install-command )
78             INSTALLCMD=yes
79             ;;
80         -h | --help)
81             HELP=yes
82             ;;
83         * )
84             usage
85             exit 1
86     esac
87     shift
88 done
89
90 if [ "$HELP" = "yes" ]; then
91     usage
92     exit 0
93 fi
94
95 # Determine what directory this script is in, the packages files
96 # should be in the same path.
97 DIR=`dirname $0`
98
99 # Determine the Ubuntu release
100 UBUNTU_RELEASE=`lsb_release -r | cut -f2 -d'    '`
101 UBUNTU_PACKAGES_FILE=$DIR/ubuntu.$UBUNTU_RELEASE.packages
102
103 # Check for the release-specific packages file. Default to the general one
104 # but warn the user about LTS releases recommended, if they are attempting
105 # to do an install command option.
106 if [ ! -e $UBUNTU_PACKAGES_FILE ]; then
107     UBUNTU_PACKAGES_FILE=$DIR/ubuntu.packages
108     if [ "$INSTALLCMD" == "yes" ]; then
109         echo "# There's no packages file for your distro/release"
110         echo "# WARNING! We strongly recommend an LTS release."
111     fi
112 fi
113
114 # We where asked to print the packages list and current versions (if any)
115 UBUNTU_PACKAGES=`awk '{print $1}' $UBUNTU_PACKAGES_FILE | grep -v '^\s*#' | grep -v '^\s*$'`
116
117 # Only output this on an install command option in order to maintain
118 # output equivalence to the former script, in the case of reporting
119 # only.
120 if [ "$INSTALLCMD" == "yes" ]; then
121
122     # Tell them which file being used to determine the output.
123     echo "# Using the $UBUNTU_PACKAGES_FILE file as source"
124
125     # Comment for skiping the dots if needed ....
126     if [ "$CHECK" == "no" ]; then
127         echo -n "#"
128     fi
129 fi
130
131 # Initialize variable to accumulate missing packages in.
132 MISSING_PACKAGES=""
133
134 # Loop used to accumulate the missing packages and display package information if requested to report.
135 for PACKAGE in $UBUNTU_PACKAGES; do
136
137     # If an install command option is running, but not a report option,
138     # There is no need to determine the version number. If it was
139     # This would run even slower!
140
141     # Test if the package is installed
142     PACKAGE_INSTALLED=`packageInstalled $PACKAGE`
143
144     # Determine the package version if it is installed.
145     if [ "$PACKAGE_INSTALLED" == "yes" ]; then
146         PACKAGE_VERSION=`getPackageVersion $PACKAGE`
147
148     # otherwise default to 'none'.
149     else
150         PACKAGE_VERSION="none"
151         MISSING_PACKAGES="$PACKAGE $MISSING_PACKAGES"
152     fi
153
154     # If we are supposed to report...
155     if [ "$CHECK" == "yes" ]; then
156
157
158         # report the package name and version number.
159         echo "$PACKAGE = $PACKAGE_VERSION"
160
161     # Otherwise, we'll just echo a dot for the impatient.
162     else
163         echo -n "."
164     fi
165
166 done
167
168 # If we aren't reporting, then the last echo didn't have a newline.
169 if [ ! "$CHECK" == "yes" ]; then
170     echo
171 fi
172
173 # If the install command was requested...
174 if [ "$INSTALLCMD" == "yes" ]; then
175
176     # Give them a nicely indented command to copy, if dependencies are missing.
177     if [ "${#MISSING_PACKAGES}" -gt "0" ]; then
178         cat <<EOF
179 # Copy and paste the following command to install all Koha's dependencies on your system:
180 # Note: this command will run with admin privileges. Make sure your user has sudo rights
181 EOF
182
183         echo -e "\tsudo apt-get install $MISSING_PACKAGES"
184
185     # Otherwise tell them all is well.
186     else
187         echo -e "# All dependencies installed!"
188         echo -e "# Please confirm the version numbers are sufficient"
189         echo -e "# By running koha_perl_deps.pl -m -u."
190     fi
191
192 fi
193
194 exit 0