*** empty log message ***
[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 macsetup module.
31 #
32
33 #  This preinstall script:
34 #  1. Shuts down Privoxy if it's running
35 #  2. Detects the version of OS X on which we're installing
36 #  3. Creates the privoxy group and user
37 #  4. Backs up any existing log & configuration files
38
39 # create installation log file
40 logfile='/var/privoxy_installation.log'
41 echo 'Privoxy installation on' `/bin/date` > ${logfile}
42
43 # 1. Stop the Privoxy service if it's running
44 #
45 # The following will only return a value if there is a process whose name contains '/privoxy'
46 # We need the / so as to exclude the scripts that'll be running during installtion
47 privoxy_pid=`ps -ax | grep /privoxy | grep -v grep | awk '{ print $1 }'`
48 # if privoxy_pid is a number (reroute stderr to dump "integer expression expected" error which'll be thrown if Privoxy isn't running)
49 if [ "$privoxy_pid" -gt 0 ] 2>/dev/null; then
50         # try using the appropriate startup method to stop it (else it might simply restart)
51         if [ `/bin/launchctl list | /usr/bin/grep "org.ijbswa.privoxy" | /usr/bin/awk '{print $3}'` ]; then
52                 echo 'Stopping Privoxy via LaunchDaemon.' >> ${logfile}
53                 /bin/launchctl unload /Library/LaunchDaemons/org.ijbswa.privoxy.plist >> ${logfile} 2>&1
54                 # give Privoxy a chance to shut down
55                 sleep 3
56         fi
57         privoxy_pid=`ps -ax | grep privoxy | grep -v grep | awk '{ print $1 }'`
58         if [ "$privoxy_pid" -gt 0 ] 2>/dev/null; then
59                 if [ -f /Library/StartupItems/Privoxy/Privoxy ]; then
60                         echo 'Stopping Privoxy via StartupItem.' >> ${logfile}
61                         /Library/StartupItems/Privoxy/Privoxy stop >> ${logfile} 2>&1
62                         # give Privoxy a chance to shut down
63                         sleep 3
64                 fi
65                 # If neither of the above shutdown methods (installed by a prior 'approved' installer) work,
66                 # then shut down Privoxy the Unix way
67                 privoxy_pid=`ps -ax | grep privoxy | grep -v grep | awk '{ print $1 }'`
68                 if [ "$privoxy_pid" -gt 0 ] 2>/dev/null; then
69                         echo 'Privoxy not shutting down via OS X startup methods, doing it the Unix way.' >> ${logfile}
70                         /bin/kill -TERM "${privoxy_pid}" >> ${logfile} 2>&1
71                         # give Privoxy a chance to shut down
72                         sleep 3
73                         # if Privoxy is still running then terminate it aggressively
74                         privoxy_pid=`ps -ax | grep privoxy | grep -v grep | awk '{ print $1 }'`
75                         if [ "$privoxy_pid" -gt 0 ] 2>/dev/null; then
76                                 echo 'Privoxy still not shutting down so must be hung. Killing it aggressively.' >> ${logfile}
77                                 /bin/kill -KILL "${privoxy_pid}" >> ${logfile} 2>&1
78                                 /bin/rm -f /var/run/privoxy.pid
79                         fi
80                 fi
81         fi
82 else
83         echo 'Privoxy not running.' >> ${logfile}
84 fi
85
86 # 2. Detect the version of OS X on which we're installing
87 #
88 darwin_major_rel_num="`/usr/bin/uname -r | /usr/bin/sed 's/\..*//'`"
89 echo 'Darwin major release version is:' ${darwin_major_rel_num} >> ${logfile}
90
91 # 3. Create the privoxy group and user (using the host-specific command to do so)
92 #
93 case "${darwin_major_rel_num}" in
94   # Mac OS X 10.7, 10.6, 10.5
95   11|10|9)
96     GNAME="`/usr/bin/dscl /Local/Default -list /groups | /usr/bin/grep -E '^(_)?privoxy?'`"
97     UNAME="`/usr/bin/dscl /Local/Default -list /users | /usr/bin/grep -E '^(_)?privoxy?'`"
98         # create group if it does not exist
99         if [ -z ${GNAME} ]; then
100           GID1="`/usr/bin/dscl /Local/Default -list /groups gid | /usr/bin/awk '{print $2}' | /usr/bin/sort -n | /usr/bin/tail -1`"
101           Privoxy_GID="`/bin/expr ${GID1} + 1`"
102                 echo 'Creating privoxy group using dscl' >> ${logfile}
103           /usr/bin/dscl /Local/Default -create /Groups/_privoxy >> ${logfile} 2>&1
104           /usr/bin/dscl /Local/Default -append /Groups/_privoxy RecordName privoxy >> ${logfile} 2>&1
105           /usr/bin/dscl /Local/Default -create /Groups/_privoxy Password "*" >> ${logfile} 2>&1
106           /usr/bin/dscl /Local/Default -create /Groups/_privoxy PrimaryGroupID ${Privoxy_GID} >> ${logfile} 2>&1
107                 /usr/bin/dscl /Local/Default -create /Groups/_privoxy RealName "privoxy users" >> ${logfile} 2>&1
108         fi
109         # create user if it does not exist
110         if [ -z ${UNAME} ]; then
111           UID1="`/usr/bin/dscl /Local/Default -list /users uid | /usr/bin/awk '{print $2}' | /usr/bin/sort -n | /usr/bin/tail -1`"
112           Privoxy_UID="`/bin/expr ${UID1} + 1`"
113                 echo 'Creating privoxy user using dscl' >> ${logfile}
114           /usr/bin/dscl /Local/Default -create /Users/_privoxy >> ${logfile} 2>&1
115           /usr/bin/dscl /Local/Default -append /Users/_privoxy RecordName privoxy >> ${logfile} 2>&1
116           /usr/bin/dscl /Local/Default -create /Users/_privoxy NFSHomeDirectory /var/empty >> ${logfile} 2>&1
117           /usr/bin/dscl /Local/Default -create /Users/_privoxy Password "*" >> ${logfile} 2>&1
118           /usr/bin/dscl /Local/Default -create /Users/_privoxy PrimaryGroupID ${Privoxy_GID} >> ${logfile} 2>&1
119           /usr/bin/dscl /Local/Default -create /Users/_privoxy RealName "privoxy server" >> ${logfile} 2>&1
120           /usr/bin/dscl /Local/Default -create /Users/_privoxy UniqueID ${Privoxy_UID} >> ${logfile} 2>&1
121           /usr/bin/dscl /Local/Default -create /Users/_privoxy UserShell /usr/bin/false >> ${logfile} 2>&1
122         fi
123   ;;
124   # Mac OS X 10.4, 10.3
125   8|7)
126     GNAME="`/usr/bin/nireport . /groups name | /usr/bin/grep -E '^privoxy?'`"
127     UNAME="`/usr/bin/nireport . /users name | /usr/bin/grep -E '^privoxy?'`"
128         # create group if it does not exist
129         if [ -z ${GNAME} ]; then
130           GID1="`/usr/bin/nireport . /groups gid | /usr/bin/sort -n | /usr/bin/tail -1`"
131           Privoxy_GID="`/bin/expr ${GID1} + 1`"
132                 echo 'Creating privoxy group using nicl' >> ${logfile}
133           /usr/bin/nicl . -create /groups/privoxy >> ${logfile} 2>&1
134           /usr/bin/nicl . -create /groups/privoxy passwd "*" >> ${logfile} 2>&1
135           /usr/bin/nicl . -create /groups/privoxy gid ${Privoxy_GID} >> ${logfile} 2>&1
136         fi
137         # create user if it does not exist
138         if [ -z ${UNAME} ]; then
139           UID1="`/usr/bin/nireport . /users uid | /usr/bin/sort -n | /usr/bin/tail -1`"
140           Privoxy_UID="`/bin/expr ${UID1} + 1`"
141                 echo 'Creating privoxy user using nicl' >> ${logfile}
142           /usr/bin/nicl . -create /users/privoxy >> ${logfile} 2>&1
143           /usr/bin/nicl . -create /users/privoxy passwd "*" >> ${logfile} 2>&1
144           /usr/bin/nicl . -create /users/privoxy uid ${Privoxy_UID} >> ${logfile} 2>&1
145           /usr/bin/nicl . -create /users/privoxy gid ${Privoxy_GID} >> ${logfile} 2>&1
146           /usr/bin/nicl . -create /users/privoxy change 0 >> ${logfile} 2>&1
147           /usr/bin/nicl . -create /users/privoxy expire 0 >> ${logfile} 2>&1
148           /usr/bin/nicl . -create /users/privoxy realname "Privoxy Web Proxy" >> ${logfile} 2>&1
149           /usr/bin/nicl . -create /users/privoxy home /var/empty >> ${logfile} 2>&1
150           /usr/bin/nicl . -create /users/privoxy shell /usr/bin/false >> ${logfile} 2>&1
151           /usr/bin/nicl . -create /users/privoxy _shadow_passwd "" >> ${logfile} 2>&1
152         fi
153   ;;
154   # default
155   *)
156     GNAME=""
157     UNAME=""
158     ;;
159 esac
160
161 # 4. Back up any existing log & configuration files
162 #
163 if [ -f /var/log/privoxy/logfile.log ]; then
164         echo 'Backing up existing logfile' >> ${logfile}
165   /bin/cp /var/log/privoxy/logfile.log /var/log/privoxy/logfile.old >> ${logfile} 2>&1
166         /usr/sbin/chown privoxy:privoxy /var/log/privoxy/logfile.old >> ${logfile} 2>&1
167 fi
168 if [ -d /usr/local/etc/privoxy ]; then
169         for i in config match-all.action trust user.action user.filter; do
170                 echo 'Backing up existing config file:' ${i} >> ${logfile}
171                 if [ -f /usr/local/etc/privoxy/$i ]; then
172                         /bin/cp /usr/local/etc/privoxy/$i /usr/local/etc/privoxy/$i.old >> ${logfile} 2>&1
173                         /usr/sbin/chown privoxy:privoxy /usr/local/etc/privoxy/$i.old >> ${logfile} 2>&1
174                 fi
175         done
176         echo 'Backing up existing config templates' >> ${logfile}
177         /bin/cp -R /usr/local/etc/privoxy/templates /usr/local/etc/privoxy/templates.old >> ${logfile} 2>&1
178         /usr/sbin/chown -R privoxy:privoxy /usr/local/etc/privoxy/templates.old >> ${logfile} 2>&1
179 fi
180
181 exit 0