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