Use chkconfig instead of S scripts inside RPM
[webmin.git] / makerpm.pl
1 #!/usr/local/bin/perl
2 # Build an RPM package of Webmin
3
4 if (-r "/usr/src/OpenLinux") {
5         $base_dir = "/usr/src/OpenLinux";
6         }
7 else {
8         $base_dir = "/usr/src/redhat";
9         }
10 $spec_dir = "$base_dir/SPECS";
11 $source_dir = "$base_dir/SOURCES";
12 $rpms_dir = "$base_dir/RPMS/noarch";
13 $srpms_dir = "$base_dir/SRPMS";
14
15 $< && die "makerpm.pl must be run as root";
16
17 if ($ARGV[0] eq "--nosign" || $ARGV[0] eq "-nosign") {
18         $nosign = 1;
19         shift(@ARGV);
20         }
21 $ver = $ARGV[0] || die "usage: makerpm.pl <version> [release]";
22 $rel = $ARGV[1] || "1";
23
24 $oscheck = <<EOF;
25 if (-r "/etc/.issue") {
26         \$etc_issue = `cat /etc/.issue`;
27         }
28 elsif (-r "/etc/issue") {
29         \$etc_issue = `cat /etc/issue`;
30         }
31 \$uname = `uname -a`;
32 EOF
33 open(OS, "os_list.txt");
34 while(<OS>) {
35         chop;
36         if (/^([^\t]+)\t+([^\t]+)\t+([^\t]+)\t+([^\t]+)\t*(.*)$/ && $5) {
37                 $if = $count++ == 0 ? "if" : "elsif";
38                 $oscheck .= "$if ($5) {\n".
39                             "   print \"oscheck='$1'\\n\";\n".
40                             "   }\n";
41                 }
42         }
43 close(OS);
44 $oscheck =~ s/\\/\\\\/g;
45 $oscheck =~ s/`/\\`/g;
46 $oscheck =~ s/\$/\\\$/g;
47
48 open(TEMP, "maketemp.pl");
49 while(<TEMP>) {
50         $maketemp .= $_;
51         }
52 close(TEMP);
53 $maketemp =~ s/\\/\\\\/g;
54 $maketemp =~ s/`/\\`/g;
55 $maketemp =~ s/\$/\\\$/g;
56
57 system("cp tarballs/webmin-$ver.tar.gz $source_dir");
58 open(SPEC, ">$spec_dir/webmin-$ver.spec");
59 print SPEC <<EOF;
60 #%define BuildRoot /tmp/%{name}-%{version}
61 %define __spec_install_post %{nil}
62
63 Summary: A web-based administration interface for Unix systems.
64 Name: webmin
65 Version: $ver
66 Release: $rel
67 Provides: %{name}-%{version}
68 PreReq: /bin/sh /usr/bin/perl /bin/rm
69 Requires: /bin/sh /usr/bin/perl /bin/rm
70 AutoReq: 0
71 License: Freeware
72 Group: System/Tools
73 Source: http://www.webmin.com/download/%{name}-%{version}.tar.gz
74 Vendor: Jamie Cameron
75 BuildRoot: /tmp/%{name}-%{version}
76 BuildArchitectures: noarch
77 %description
78 A web-based administration interface for Unix systems. Using Webmin you can
79 configure DNS, Samba, NFS, local/remote filesystems and more using your
80 web browser.
81
82 After installation, enter the URL http://localhost:10000/ into your
83 browser and login as root with your root password.
84
85 %prep
86 %setup -q
87
88 %build
89 (find . -name '*.cgi' ; find . -name '*.pl') | perl perlpath.pl /usr/bin/perl -
90 rm -f mount/freebsd-mounts*
91 rm -f mount/openbsd-mounts*
92 rm -f mount/macos-mounts*
93 rm -f webmin-gentoo-init
94 rm -rf format bsdexports hpuxexports sgiexports zones rbac
95 rm -rf acl/Authen-SolarisRBAC-0.1*
96 chmod -R og-w .
97
98 %install
99 mkdir -p %{buildroot}/usr/libexec/webmin
100 mkdir -p %{buildroot}/etc/sysconfig/daemons
101 mkdir -p %{buildroot}/etc/rc.d/{rc0.d,rc1.d,rc2.d,rc3.d,rc5.d,rc6.d}
102 mkdir -p %{buildroot}/etc/init.d
103 mkdir -p %{buildroot}/etc/pam.d
104 cp -rp * %{buildroot}/usr/libexec/webmin
105 cp webmin-daemon %{buildroot}/etc/sysconfig/daemons/webmin
106 cp webmin-init %{buildroot}/etc/init.d/webmin
107 cp webmin-pam %{buildroot}/etc/pam.d/webmin
108 echo rpm >%{buildroot}/usr/libexec/webmin/install-type
109
110 %clean
111 #%{rmDESTDIR}
112 [ "%{buildroot}" != "/" ] && rm -rf %{buildroot}
113
114 %files
115 %defattr(-,root,root)
116 /usr/libexec/webmin
117 %config /etc/sysconfig/daemons/webmin
118 /etc/init.d/webmin
119 %config /etc/pam.d/webmin
120
121 %pre
122 perl <<EOD;
123 $maketemp
124 EOD
125 if [ "\$?" != "0" ]; then
126         echo "Failed to create or check temp files directory /tmp/.webmin"
127         exit 1
128 fi
129 if [ "\$tempdir" = "" ]; then
130         tempdir=/tmp/.webmin
131 fi
132 perl >$tempdir/\$\$.check <<EOD;
133 $oscheck
134 EOD
135 . $tempdir/\$\$.check
136 rm -f $tempdir/\$\$.check
137 if [ ! -r /etc/webmin/config ]; then
138         if [ "\$oscheck" = "" ]; then
139                 echo Unable to identify operating system
140                 exit 2
141         fi
142         echo Operating system is \$oscheck
143         if [ "\$WEBMIN_PORT\" != \"\" ]; then
144                 port=\$WEBMIN_PORT
145         else
146                 port=10000
147         fi
148         perl -e 'use Socket; socket(FOO, PF_INET, SOCK_STREAM, getprotobyname("tcp")); setsockopt(FOO, SOL_SOCKET, SO_REUSEADDR, pack("l", 1)); bind(FOO, pack_sockaddr_in(\$ARGV[0], INADDR_ANY)) || exit(1); exit(0);' \$port
149         if [ "\$?" != "0" ]; then
150                 echo Port \$port is already in use
151                 exit 2
152         fi
153 fi
154 # Save /etc/webmin in case the upgrade trashes it
155 if [ "\$1" != 1 ]; then
156         rm -rf /etc/.webmin-backup
157         cp -r /etc/webmin /etc/.webmin-backup
158 fi
159 # Put back old /etc/webmin saved when an RPM was removed
160 if [ "\$1" = 1 -a ! -d /etc/webmin -a -d /etc/webmin.rpmsave ]; then
161         mv /etc/webmin.rpmsave /etc/webmin
162 fi
163 /bin/true
164
165 %post
166 inetd=`grep "^inetd=" /etc/webmin/miniserv.conf 2>/dev/null | sed -e 's/inetd=//g'`
167 startafter=0
168 if [ "\$1" != 1 ]; then
169         # Upgrading the RPM, so stop the old webmin properly
170         if [ "\$inetd" != "1" ]; then
171                 kill -0 `cat /var/webmin/miniserv.pid 2>/dev/null` 2>/dev/null
172                 if [ "\$?" = 0 ]; then
173                   startafter=1
174                 fi
175                 /etc/init.d/webmin stop >/dev/null 2>&1 </dev/null
176         fi
177 else
178   startafter=1
179 fi
180 cd /usr/libexec/webmin
181 config_dir=/etc/webmin
182 var_dir=/var/webmin
183 perl=/usr/bin/perl
184 autoos=3
185 if [ "\$WEBMIN_PORT\" != \"\" ]; then
186         port=\$WEBMIN_PORT
187 else
188         port=10000
189 fi
190 login=root
191 if [ -r /etc/shadow ]; then
192         #crypt=`grep "^root:" /etc/shadow | cut -f 2 -d :`
193         crypt=x
194 else
195         crypt=`grep "^root:" /etc/passwd | cut -f 2 -d :`
196 fi
197 host=`hostname`
198 ssl=1
199 atboot=1
200 nochown=1
201 autothird=1
202 noperlpath=1
203 nouninstall=1
204 nostart=1
205 if [ "\$tempdir" = "" ]; then
206         tempdir=/tmp/.webmin
207 fi
208 export config_dir var_dir perl autoos port login crypt host ssl nochown autothird noperlpath nouninstall nostart allow atboot
209 ./setup.sh >\$tempdir/webmin-setup.out 2>&1
210 chmod 600 \$tempdir/webmin-setup.out
211 rm -f /var/lock/subsys/webmin
212 if [ "\$inetd" != "1" -a "\$startafter" = "1" ]; then
213         /etc/init.d/webmin start >/dev/null 2>&1 </dev/null
214 fi
215 chkconfig --add webmin
216 chkconfig webmin on
217 cat >/etc/webmin/uninstall.sh <<EOFF
218 #!/bin/sh
219 printf "Are you sure you want to uninstall Webmin? (y/n) : "
220 read answer
221 printf "\\n"
222 if [ "\\\$answer" = "y" ]; then
223         echo "Removing webmin RPM .."
224         rpm -e --nodeps webmin
225         echo "Done!"
226 fi
227 EOFF
228 chmod +x /etc/webmin/uninstall.sh
229 port=`grep "^port=" /etc/webmin/miniserv.conf | sed -e 's/port=//g'`
230 perl -e 'use Net::SSLeay' >/dev/null 2>/dev/null
231 sslmode=0
232 if [ "\$?" = "0" ]; then
233         grep ssl=1 /etc/webmin/miniserv.conf >/dev/null 2>/dev/null
234         if [ "\$?" = "0" ]; then
235                 sslmode=1
236         fi
237 fi
238 musthost=`grep musthost= /etc/webmin/miniserv.conf | sed -e 's/musthost=//'`
239 if [ "$musthost" != "" ]; then
240         host=$musthost
241 fi
242 if [ "\$sslmode" = "1" ]; then
243         echo "Webmin install complete. You can now login to https://\$host:\$port/"
244 else
245         echo "Webmin install complete. You can now login to http://\$host:\$port/"
246 fi
247 echo "as root with your root password."
248 /bin/true
249
250 %preun
251 if [ "\$1" = 0 ]; then
252         grep root=/usr/libexec/webmin /etc/webmin/miniserv.conf >/dev/null 2>&1
253         if [ "\$?" = 0 ]; then
254                 # RPM is being removed, and no new version of webmin
255                 # has taken it's place. Run uninstalls and stop the server
256                 echo "Running uninstall scripts .."
257                 (cd /usr/libexec/webmin ; WEBMIN_CONFIG=/etc/webmin WEBMIN_VAR=/var/webmin LANG= /usr/libexec/webmin/run-uninstalls.pl)
258                 /etc/init.d/webmin stop >/dev/null 2>&1 </dev/null
259                 /etc/webmin/stop >/dev/null 2>&1 </dev/null
260         fi
261 fi
262 /bin/true
263
264 %postun
265 if [ "\$1" = 0 ]; then
266         grep root=/usr/libexec/webmin /etc/webmin/miniserv.conf >/dev/null 2>&1
267         if [ "\$?" = 0 ]; then
268                 # RPM is being removed, and no new version of webmin
269                 # has taken it's place. Rename away the /etc/webmin directory
270                 rm -rf /etc/webmin.rpmsave
271                 mv /etc/webmin /etc/webmin.rpmsave
272                 rm -rf /var/webmin
273         fi
274 fi
275 /bin/true
276
277 %triggerpostun -- webmin
278 if [ ! -d /var/webmin -a "\$1" = 2 ]; then
279         echo Re-creating /var/webmin directory
280         mkdir /var/webmin
281 fi
282 if [ ! -r /etc/webmin/miniserv.conf -a -d /etc/.webmin-backup -a "\$1" = 2 ]; then
283         echo Recovering /etc/webmin directory
284         rm -rf /etc/.webmin-broken
285         mv /etc/webmin /etc/.webmin-broken
286         mv /etc/.webmin-backup /etc/webmin
287         /etc/init.d/webmin stop >/dev/null 2>&1 </dev/null
288         /etc/init.d/webmin start >/dev/null 2>&1 </dev/null
289 else
290         rm -rf /etc/.webmin-backup
291 fi
292 /bin/true
293
294 EOF
295 close(SPEC);
296
297 $cmd = -x "/usr/bin/rpmbuild" ? "rpmbuild" : "rpm";
298 system("$cmd -ba --target=noarch $spec_dir/webmin-$ver.spec") && exit;
299 if (-d "rpm") {
300         system("mv $rpms_dir/webmin-$ver-$rel.noarch.rpm rpm/webmin-$ver-$rel.noarch.rpm");
301         print "Moved to rpm/webmin-$ver-$rel.noarch.rpm\n";
302         system("mv $srpms_dir/webmin-$ver-$rel.src.rpm rpm/webmin-$ver-$rel.src.rpm");
303         print "Moved to rpm/webmin-$ver-$rel.src.rpm\n";
304         system("chown jcameron: rpm/webmin-$ver-$rel.noarch.rpm rpm/webmin-$ver-$rel.src.rpm");
305         if (!$nosign) {
306                 system("rpm --resign rpm/webmin-$ver-$rel.noarch.rpm rpm/webmin-$ver-$rel.src.rpm");
307                 }
308         }
309
310 if (!$webmail && -d "/usr/local/webadmin/rpm/yum") {
311         # Add to our repository
312         system("cp rpm/webmin-$ver-$rel.noarch.rpm /usr/local/webadmin/rpm/yum");
313         }
314
315