Correcting a comment
[OSXPackageBuilder.git] / pkg resources / single-binary scripts / preupgrade
1 #!/bin/sh
2
3 # File        :  preinstall
4 #
5 # Purpose     :  execute all tasks necessary prior to installation of Privoxy's files
6 #
7 # Copyright   :  Written by and Copyright (C) 2001-2012 the
8 #                Privoxy team. http://www.privoxy.org/
9 #
10 #                This program is free software; you can redistribute it
11 #                and/or modify it under the terms of the GNU General
12 #                Public License as published by the Free Software
13 #                Foundation; either version 2 of the License, or (at
14 #                your option) any later version.
15 #
16 #                This program is distributed in the hope that it will
17 #                be useful, but WITHOUT ANY WARRANTY; without even the
18 #                implied warranty of MERCHANTABILITY or FITNESS FOR A
19 #                PARTICULAR PURPOSE.  See the GNU General Public
20 #                License for more details.
21 #
22 #                The GNU General Public License should be included with
23 #                this file.  If not, you can view it at
24 #                http://www.gnu.org/copyleft/gpl.html
25 #                or write to the Free Software Foundation, Inc.,
26 #                51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
27 #                USA
28 #
29 # Modification : If you modify this file please consider whether your 
30 #                changes ought to be passed back to the OSXPackageBuilder
31 #                module.
32 #
33
34 #  This preinstall script:
35 #  1. Shuts down Privoxy if it's running
36 #  2. Detects the version of OS X on which we're installing
37 #  3. Creates the privoxy group and user
38 #  4. Backs up any existing log & configuration files
39
40 # create installation log file
41 logfile='/var/privoxy_installation.log'
42 echo 'Privoxy installation on' `/bin/date` > ${logfile}
43
44 # 1. Stop the Privoxy service if it's running
45 #
46 # The following will only return a value if there is a process whose name contains '/privoxy'
47 # We need the / so as to exclude the scripts that'll be running during installtion
48 privoxy_pid=`ps -ax | grep /privoxy | grep -v grep | awk '{ print $1 }'`
49 # if privoxy_pid is a number (reroute stderr to dump "integer expression expected" error which'll be thrown if Privoxy isn't running)
50 if [ "$privoxy_pid" -gt 0 ] 2>/dev/null; then
51         # try using the appropriate startup method to stop it (else it might simply restart)
52         if [ `/bin/launchctl list | /usr/bin/grep "org.ijbswa.privoxy" | /usr/bin/awk '{print $3}'` ]; then
53                 echo 'Stopping Privoxy via LaunchDaemon.' >> ${logfile}
54                 /bin/launchctl unload /Library/LaunchDaemons/org.ijbswa.privoxy.plist >> ${logfile} 2>&1
55                 # give Privoxy a chance to shut down
56                 sleep 3
57         fi
58         privoxy_pid=`ps -ax | grep privoxy | grep -v grep | awk '{ print $1 }'`
59         if [ "$privoxy_pid" -gt 0 ] 2>/dev/null; then
60                 if [ -f /Library/StartupItems/Privoxy/Privoxy ]; then
61                         echo 'Stopping Privoxy via StartupItem.' >> ${logfile}
62                         /Library/StartupItems/Privoxy/Privoxy stop >> ${logfile} 2>&1
63                         # give Privoxy a chance to shut down
64                         sleep 3
65                 fi
66                 # If neither of the above shutdown methods (installed by a prior 'approved' installer) work,
67                 # then shut down Privoxy the Unix way
68                 privoxy_pid=`ps -ax | grep privoxy | grep -v grep | awk '{ print $1 }'`
69                 if [ "$privoxy_pid" -gt 0 ] 2>/dev/null; then
70                         echo 'Privoxy not shutting down via OS X startup methods, doing it the Unix way.' >> ${logfile}
71                         /bin/kill -TERM "${privoxy_pid}" >> ${logfile} 2>&1
72                         # give Privoxy a chance to shut down
73                         sleep 3
74                         # if Privoxy is still running then terminate it aggressively
75                         privoxy_pid=`ps -ax | grep privoxy | grep -v grep | awk '{ print $1 }'`
76                         if [ "$privoxy_pid" -gt 0 ] 2>/dev/null; then
77                                 echo 'Privoxy still not shutting down so must be hung. Killing it aggressively.' >> ${logfile}
78                                 /bin/kill -KILL "${privoxy_pid}" >> ${logfile} 2>&1
79                                 /bin/rm -f /var/run/privoxy.pid
80                         fi
81                 fi
82         fi
83 else
84         echo 'Privoxy not running.' >> ${logfile}
85 fi
86
87 # 2. Detect the version of OS X on which we're installing
88 #
89 darwin_major_rel_num="`/usr/bin/uname -r | /usr/bin/sed 's/\..*//'`"
90 echo 'Darwin major release version is:' ${darwin_major_rel_num} >> ${logfile}
91
92 # 3. Create the privoxy group and user (using the host-specific command to do so)
93 #
94 case "${darwin_major_rel_num}" in
95   # Mac OS X 10.5 or higher
96   9|1*)
97     GNAME="`/usr/bin/dscl /Local/Default -list /groups | /usr/bin/grep -E '^(_)?privoxy?'`"
98     UNAME="`/usr/bin/dscl /Local/Default -list /users | /usr/bin/grep -E '^(_)?privoxy?'`"
99         # create group if it does not exist
100         if [ -z ${GNAME} ]; then
101           GID1="`/usr/bin/dscl /Local/Default -list /groups gid | /usr/bin/awk '{print $2}' | /usr/bin/sort -n | /usr/bin/tail -1`"
102           Privoxy_GID="`/bin/expr ${GID1} + 1`"
103                 echo 'Creating privoxy group using dscl' >> ${logfile}
104           /usr/bin/dscl /Local/Default -create /Groups/_privoxy >> ${logfile} 2>&1
105           /usr/bin/dscl /Local/Default -append /Groups/_privoxy RecordName privoxy >> ${logfile} 2>&1
106           /usr/bin/dscl /Local/Default -create /Groups/_privoxy Password "*" >> ${logfile} 2>&1
107           /usr/bin/dscl /Local/Default -create /Groups/_privoxy PrimaryGroupID ${Privoxy_GID} >> ${logfile} 2>&1
108                 /usr/bin/dscl /Local/Default -create /Groups/_privoxy RealName "privoxy users" >> ${logfile} 2>&1
109         fi
110         # create user if it does not exist
111         if [ -z ${UNAME} ]; then
112           UID1="`/usr/bin/dscl /Local/Default -list /users uid | /usr/bin/awk '{print $2}' | /usr/bin/sort -n | /usr/bin/tail -1`"
113           Privoxy_UID="`/bin/expr ${UID1} + 1`"
114                 echo 'Creating privoxy user using dscl' >> ${logfile}
115           /usr/bin/dscl /Local/Default -create /Users/_privoxy >> ${logfile} 2>&1
116           /usr/bin/dscl /Local/Default -append /Users/_privoxy RecordName privoxy >> ${logfile} 2>&1
117           /usr/bin/dscl /Local/Default -create /Users/_privoxy NFSHomeDirectory /var/empty >> ${logfile} 2>&1
118           /usr/bin/dscl /Local/Default -create /Users/_privoxy Password "*" >> ${logfile} 2>&1
119           /usr/bin/dscl /Local/Default -create /Users/_privoxy PrimaryGroupID ${Privoxy_GID} >> ${logfile} 2>&1
120           /usr/bin/dscl /Local/Default -create /Users/_privoxy RealName "privoxy server" >> ${logfile} 2>&1
121           /usr/bin/dscl /Local/Default -create /Users/_privoxy UniqueID ${Privoxy_UID} >> ${logfile} 2>&1
122           /usr/bin/dscl /Local/Default -create /Users/_privoxy UserShell /usr/bin/false >> ${logfile} 2>&1
123         fi
124   ;;
125   # Mac OS X 10.4, 10.3
126   8|7)
127     GNAME="`/usr/bin/nireport . /groups name | /usr/bin/grep -E '^privoxy?'`"
128     UNAME="`/usr/bin/nireport . /users name | /usr/bin/grep -E '^privoxy?'`"
129         # create group if it does not exist
130         if [ -z ${GNAME} ]; then
131           GID1="`/usr/bin/nireport . /groups gid | /usr/bin/sort -n | /usr/bin/tail -1`"
132           Privoxy_GID="`/bin/expr ${GID1} + 1`"
133                 echo 'Creating privoxy group using nicl' >> ${logfile}
134           /usr/bin/nicl . -create /groups/privoxy >> ${logfile} 2>&1
135           /usr/bin/nicl . -create /groups/privoxy passwd "*" >> ${logfile} 2>&1
136           /usr/bin/nicl . -create /groups/privoxy gid ${Privoxy_GID} >> ${logfile} 2>&1
137         fi
138         # create user if it does not exist
139         if [ -z ${UNAME} ]; then
140           UID1="`/usr/bin/nireport . /users uid | /usr/bin/sort -n | /usr/bin/tail -1`"
141           Privoxy_UID="`/bin/expr ${UID1} + 1`"
142                 echo 'Creating privoxy user using nicl' >> ${logfile}
143           /usr/bin/nicl . -create /users/privoxy >> ${logfile} 2>&1
144           /usr/bin/nicl . -create /users/privoxy passwd "*" >> ${logfile} 2>&1
145           /usr/bin/nicl . -create /users/privoxy uid ${Privoxy_UID} >> ${logfile} 2>&1
146           /usr/bin/nicl . -create /users/privoxy gid ${Privoxy_GID} >> ${logfile} 2>&1
147           /usr/bin/nicl . -create /users/privoxy change 0 >> ${logfile} 2>&1
148           /usr/bin/nicl . -create /users/privoxy expire 0 >> ${logfile} 2>&1
149           /usr/bin/nicl . -create /users/privoxy realname "Privoxy Web Proxy" >> ${logfile} 2>&1
150           /usr/bin/nicl . -create /users/privoxy home /var/empty >> ${logfile} 2>&1
151           /usr/bin/nicl . -create /users/privoxy shell /usr/bin/false >> ${logfile} 2>&1
152           /usr/bin/nicl . -create /users/privoxy _shadow_passwd "" >> ${logfile} 2>&1
153         fi
154   ;;
155   # default
156   *)
157     GNAME=""
158     UNAME=""
159     ;;
160 esac
161
162 # 4. Back up any existing log & configuration files
163 #
164 if [ -f /var/log/privoxy/logfile.log ]; then
165         echo 'Backing up existing logfile' >> ${logfile}
166   /bin/cp /var/log/privoxy/logfile.log /var/log/privoxy/logfile.old >> ${logfile} 2>&1
167         /usr/sbin/chown privoxy:privoxy /var/log/privoxy/logfile.old >> ${logfile} 2>&1
168 fi
169 if [ -d /usr/local/etc/privoxy ]; then
170         for i in config match-all.action trust user.action user.filter; do
171                 echo 'Backing up existing config file:' ${i} >> ${logfile}
172                 if [ -f /usr/local/etc/privoxy/$i ]; then
173                         /bin/cp /usr/local/etc/privoxy/$i /usr/local/etc/privoxy/$i.old >> ${logfile} 2>&1
174                         /usr/sbin/chown privoxy:privoxy /usr/local/etc/privoxy/$i.old >> ${logfile} 2>&1
175                 fi
176         done
177         echo 'Backing up existing config templates' >> ${logfile}
178         /bin/cp -R /usr/local/etc/privoxy/templates /usr/local/etc/privoxy/templates.old >> ${logfile} 2>&1
179         /usr/sbin/chown -R privoxy:privoxy /usr/local/etc/privoxy/templates.old >> ${logfile} 2>&1
180 fi
181
182 exit 0