#!/usr/bin/perl
# makemodulerpm.pl
# Create an RPM for a webmin module or theme
$target_dir = "/tmp"; # where to copy the RPM to
if (-d "/usr/src/OpenLinux") {
$spec_dir = "/usr/src/OpenLinux/SPECS";
$source_dir = "/usr/src/OpenLinux/SOURCES";
$rpm_dir = "/usr/src/OpenLinux/RPMS/i386";
}
else {
$spec_dir = "/usr/src/redhat/SPECS";
$source_dir = "/usr/src/redhat/SOURCES";
$rpm_dir = "/usr/src/redhat/RPMS/i386";
}
$ver = 1;
@ARGV == 1 || @ARGV == 2 || die "usage: makemodulerpm.pl [version]";
($dir, $ver) = @ARGV;
$ver = 1 if (!$ver);
chop($par = `dirname $dir`);
chop($mod = `basename $dir`);
# Is this actually a module or theme directory?
-d $dir || die "$dir is not a directory";
if (&read_file("$dir/module.info", \%minfo) && $minfo{'desc'}) {
$depends = join(" ", grep { !/^[0-9\.]+$/ }
split(/\s+/, $minfo{'depends'}));
$prefix = "wbm";
$desc = "Webmin module for '$minfo{'desc'}'";
$post_config = 1;
}
elsif (&read_file("$dir/theme.info", \%tinfo) && $tinfo{'desc'}) {
$prefix = "wbt";
$desc = "Webmin theme '$tinfo{'desc'}'";
$post_config = 0;
}
else {
die "$dir does not appear to be a webmin module or theme";
}
# Tar up the directory
system("cd $par ; tar cf $source_dir/$mod.tar.gz $mod");
# Create the SPEC file
open(SPEC, ">$spec_dir/$prefix-$mod.spec");
print SPEC <; close(F); \$l[0] = "#\!/usr/bin/perl\$1\n" if (\$l[0] =~ /#\!\\S*perl\\S*(.*)/); open(F,">\$_"); print F \@l; close(F)'
(find . -name '*.bak' ; find . -name core) | xargs rm -f
%install
mkdir -p %{BuildRoot}/usr/libexec/webmin/$mod
cp -rp * %{BuildRoot}/usr/libexec/webmin/$mod
echo rpm >%{BuildRoot}/usr/libexec/webmin/$mod/install-type
%clean
%{rmDESTDIR}
%files
/usr/libexec/webmin/$mod
%pre
# Check if webmin is installed
if [ ! -r /etc/webmin/config -o ! -d /usr/libexec/webmin ]; then
echo "Webmin does not appear to be installed on your system."
echo "This RPM cannot be installed unless the RPM version of Webmin"
echo "is installed first."
exit 1
fi
if [ "$depends" != "" ]; then
# Check if depended webmin modules are installed
for \$d in $depends; do
if [ !-r /usr/libexec/webmin/\$d/module.info ]; then
echo "The Webmin depends on the module \$d, which is"
echo "not installed on your system."
exit 1
fi
done
fi
# Check if this module is already installed
if [ -d /usr/libexec/webmin/$mod -a "\$1" = "1" ]; then
echo "This Webmin module is already installed on your system."
exit 1
fi
%post
if [ "$post_config" = "1" ]; then
# Copy config file to /etc/webmin
os_type=`grep "^os_type=" /etc/webmin/config | sed -e 's/os_type=//g'`
os_version=`grep "^os_version=" /etc/webmin/config | sed -e 's/os_version=//g'`
/usr/bin/perl /usr/libexec/webmin/copyconfig.pl \$os_type \$os_version /usr/libexec/webmin /etc/webmin $mod
# Update the ACL for the root user, or the first user in the ACL
grep "^root:" /etc/webmin/webmin.acl >/dev/null
if [ "\$?" = "0" ]; then
user=root
else
user=`head -1 /etc/webmin/webmin.acl | cut -f 1 -d :`
fi
mods=`grep \$user: /etc/webmin/webmin.acl | cut -f 2 -d :`
echo \$mods | grep " $mod" >/dev/null
if [ "\$?" != "0" ]; then
grep -v ^\$user: /etc/webmin/webmin.acl > /tmp/webmin.acl.tmp
echo \$user: \$mods $mod > /etc/webmin/webmin.acl
cat /tmp/webmin.acl.tmp >> /etc/webmin/webmin.acl
rm -f /tmp/webmin.acl.tmp
fi
fi
%preun
%postun
EOF
close(SPEC);
# Build the actual RPM
system("rpm -ba $spec_dir/$prefix-$mod.spec") && exit;
unlink("$source_dir/$mod.tar.gz");
system("cp $rpm_dir/wbm-$mod-$ver-1.i386.rpm $target_dir/wbm-$mod.rpm");
# read_file(file, &assoc, [&order], [lowercase])
# Fill an associative array with name=value pairs from a file
sub read_file
{
open(ARFILE, $_[0]) || return 0;
while() {
s/\r|\n//g;
if (!/^#/ && /^([^=]+)=(.*)$/) {
$_[1]->{$_[3] ? lc($1) : $1} = $2;
push(@{$_[2]}, $1) if ($_[2]);
}
}
close(ARFILE);
return 1;
}