b77bb64
#! /bin/bash
b77bb64
b77bb64
# Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
b77bb64
#  
b77bb64
# This program is free software; you can redistribute it and/or modify
b77bb64
# it under the terms of the GNU General Public License as published by
b77bb64
# the Free Software Foundation; version 2 of the License.
b77bb64
#  
b77bb64
# This program is distributed in the hope that it will be useful,
b77bb64
# but WITHOUT ANY WARRANTY; without even the implied warranty of
b77bb64
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
b77bb64
# GNU General Public License for more details.
b77bb64
#  
b77bb64
# You should have received a copy of the GNU General Public License
b77bb64
# along with this program; if not, write to the Free Software
b77bb64
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
b77bb64
b77bb64
function showHelp()
b77bb64
{
b77bb64
    echo \
b77bb64
$"Usage: clamd-gen --service=<SERVICE> --version=<VERSION> --release=<RELEASE>
b77bb64
                   --license=<LICENSE> --username=>USERNAME>
b77bb64
"
b77bb64
    exit 0
b77bb64
}
b77bb64
b77bb64
function rpm.generatePreamble()
b77bb64
{
b77bb64
    cat <
443889f
%{!?release_func:%define release_func()	%1%{?dist}}
b77bb64
# The name of the minit service
b77bb64
%define minitsvcdir	%minitdir/services/%name
b77bb64
# The configuration file for the SysV initservice
b77bb64
%define conffile	%_sysconfdir/clamd.d/%service.conf
b77bb64
# The directory, where the milter socket will be placed into; this
b77bb64
# socket will be named clamd.sock
b77bb64
%define rundir		/var/run/clamd.%service
b77bb64
# The name of the logfile
b77bb64
%define logfile		/var/log/clamd.%service
b77bb64
# The user under whose id, the clamd shall be running. This user must
b77bb64
# be able to read the files from the base-service and is usually
b77bb64
# created there.
b77bb64
%define username	$USERNAME
b77bb64
# The packagename of the service
b77bb64
%define service		$SERVICE
b77bb64
# The service name as used by the system's initscripts; usually this
b77bb64
# is %service
b77bb64
%define baseservice	%service
b77bb64
b77bb64
%define __chkconfig	/sbin/chkconfig
b77bb64
%define minitdir	%_sysconfdir/minit
b77bb64
b77bb64
b77bb64
EOF
b77bb64
}
b77bb64
b77bb64
function rpm.generateHeader()
b77bb64
{
b77bb64
    cat <
b77bb64
Summary:		Clamav server for '%service'
b77bb64
Name:			clamd.%service
b77bb64
Version:		$VERSION
b77bb64
Release:		%release_func $RELEASE
b77bb64
Epoch:			0
b77bb64
License:		$LICENSE
b77bb64
Group:			System Environment/Daemons
b77bb64
BuildRoot:		%_tmppath/%name-%version-%release-root
b77bb64
BuildArch:		noarch
b77bb64
Requires:		init(%name)
b77bb64
Requires(pre):		%service
b77bb64
Requires:		clamav-server
b77bb64
BuildRequires:		clamav-devel
b77bb64
b77bb64
%package sysv
b77bb64
Summary:		SysV initscripts for a %service clamav-server
b77bb64
Group:			System Environment/Daemons
b77bb64
Provides:		init(%name) = sysv
b77bb64
Conflicts:		init(%name) < sysv
b77bb64
Conflicts:		init(%name) > sysv
f1f8b45
Requires:		clamav-server-sysv
b77bb64
Requires(post):		%name = %epoch:%version-%release
b77bb64
Requires(post):		diffutils mktemp %__chkconfig
b77bb64
Requires(preun):	%__chkconfig
b77bb64
Requires(pre):		%_initrddir
b77bb64
Requires(postun):	%_initrddir
b77bb64
b77bb64
%package minit
b77bb64
Summary:		minit initscripts for a %service clamav-server
b77bb64
Group:			System Environment/Daemons
b77bb64
Provides:		init(%name) = minit
b77bb64
Conflicts:		init(%name) < minit
b77bb64
Conflicts:		init(%name) > minit
b77bb64
Requires(post):		%name = %epoch:%version-%release
b77bb64
Requires(post):		diffutils mktemp
b77bb64
Requires(pre):		minit-setup
b77bb64
Requires(postun):	minit-setup
b77bb64
Requires(triggers):	minit-tools
b77bb64
b77bb64
b77bb64
%description
b77bb64
Basic setup for a clamav server for '%service'.
b77bb64
b77bb64
b77bb64
%description sysv
b77bb64
Basic setup for a clamav server for '%service'.
b77bb64
b77bb64
This package contains initscripts for SysV based systems.
b77bb64
b77bb64
b77bb64
%description minit
b77bb64
Basic setup for a clamav server for '%service'.
b77bb64
b77bb64
This package contains initscripts for minit based systems.
b77bb64
b77bb64
EOF
b77bb64
}
b77bb64
b77bb64
b77bb64
function rpm.genBody()
b77bb64
{
b77bb64
    cat <<"XEOFX"
b77bb64
%prep
b77bb64
%build
b77bb64
b77bb64
%install
b77bb64
rm -rf $RPM_BUILD_ROOT
b77bb64
%__install -d -m755 $RPM_BUILD_ROOT{%minitsvcdir,%_sbindir,%rundir,/var/log}
b77bb64
b77bb64
d=/usr/share/clamav/template
b77bb64
b77bb64
function subst
b77bb64
{
b77bb64
	src=$d/$1
b77bb64
	dst=$RPM_BUILD_ROOT$2
b77bb64
b77bb64
	%__install -d -m755 $(dirname "$dst")
b77bb64
	sed -e 's!^\(#?LogFile \).*!\1%logfile!g;
b77bb64
		s!^#?\(LocalSocket \).*!\1%rundir/clamd.sock!g;
b77bb64
		s!^#?\(PidFile \).*!\1%rundir/clamd.pid!g;
b77bb64
		s!<SERVICE>!%service!g;
b77bb64
		s!<USER>!%username!g;' "$src" >"$dst"
b77bb64
	chmod --reference "$src" "$dst"
b77bb64
}
b77bb64
b77bb64
subst clamd.conf      %conffile
b77bb64
subst clamd.logrotate %_sysconfdir/logrotate.d/clamd.%service
b77bb64
b77bb64
%if 0%{!?_without_sysv:1}
b77bb64
subst clamd.sysconfig %_sysconfdir/sysconfig/clamd.%service
b77bb64
subst clamd.init      %_initrddir/clamd.%service
b77bb64
%endif
b77bb64
b77bb64
ln -s clamd $RPM_BUILD_ROOT%_sbindir/clamd.%service
b77bb64
b77bb64
touch $RPM_BUILD_ROOT%logfile
b77bb64
touch $RPM_BUILD_ROOT%rundir/clamd.sock
b77bb64
b77bb64
%if 0%{!?_without_minit:1}
b77bb64
ln -s %_sbindir/clamd.%service $RPM_BUILD_ROOT%minitsvcdir/run
b77bb64
touch                          $RPM_BUILD_ROOT%minitsvcdir/respawn
b77bb64
cat <<EOF                     >$RPM_BUILD_ROOT%minitsvcdir/params
b77bb64
-c
b77bb64
%conffile
b77bb64
EOF
b77bb64
%endif
b77bb64
b77bb64
%clean
b77bb64
rm -rf $RPM_BUILD_ROOT
b77bb64
b77bb64
b77bb64
%triggerin minit -- %baseservice
b77bb64
minit-svc add services/clamd.%service services/%baseservice/
b77bb64
b77bb64
%triggerun minit -- %baseservice
b77bb64
test "$1" != 0 -a "$2" != 0 || \
b77bb64
	minit-svc del services/clamd.%service services/%baseservice/
b77bb64
b77bb64
b77bb64
%post minit
b77bb64
d=$(mktemp /tmp/clamd.%service.XXXXXX)
b77bb64
sed -e 's!^#Foreground!Foreground!' "%conffile" >"$d"
b77bb64
grep -q '^Foreground' $d || echo 'Foreground' >>$d
b77bb64
cmp -s "$d" %conffile || cat "$d" >"%conffile"
b77bb64
rm -f "$d"
b77bb64
b77bb64
%post sysv
b77bb64
d=$(mktemp /tmp/clamd.%service.XXXXXX)
b77bb64
sed -e 's!^Foreground!#Foreground!' "%conffile" >"$d"
b77bb64
cmp -s "$d" %conffile || cat "$d" >"%conffile"
b77bb64
rm -f "$d"
b77bb64
b77bb64
%__chkconfig --add %name
b77bb64
b77bb64
b77bb64
%preun sysv
b77bb64
test "$1" != 0 || %__chkconfig --del %name
b77bb64
b77bb64
XEOFX
b77bb64
}
b77bb64
b77bb64
b77bb64
function rpm.genFiles
b77bb64
{
b77bb64
    cat <<"EOF"
b77bb64
%files
b77bb64
%defattr(-,root,root,-)
b77bb64
%doc
b77bb64
%config(noreplace) %verify(not size md5 mtime) %attr(0620,root,%username)  %logfile
b77bb64
%config(noreplace) %verify(not mtime) %conffile
b77bb64
%config(noreplace) %verify(not mtime) %_sysconfdir/logrotate.d/clamd.%service
b77bb64
%_sbindir/clamd.%service
b77bb64
%dir %attr(0700,%username,root) %rundir
b77bb64
%ghost %rundir/clamd.sock
b77bb64
b77bb64
b77bb64
%if 0%{!?_without_sysv:1}
b77bb64
%files sysv
b77bb64
%defattr(-,root,root,-)
b77bb64
%config            %verify(not mtime) %_initrddir/clamd.%service
b77bb64
%config(noreplace) %verify(not mtime) %_sysconfdir/sysconfig/clamd.%service
b77bb64
%endif
b77bb64
b77bb64
b77bb64
%if 0%{!?_without_minit:1}
b77bb64
%files minit
b77bb64
%defattr(-,root,root,-)
b77bb64
%dir                                  %minitsvcdir
b77bb64
%config(noreplace) %verify(not mtime) %minitsvcdir/params
b77bb64
%config                               %minitsvcdir/run
b77bb64
                                      %minitsvcdir/respawn
b77bb64
%endif
b77bb64
EOF
b77bb64
}
b77bb64
b77bb64
b77bb64
SERVICE=
b77bb64
VERSION=
b77bb64
RELEASE=
b77bb64
LICENSE=
b77bb64
USERNAME=
b77bb64
tmp=$(getopt -o '' --long service:,version:,release:,license:,username:,help -n "$0" -- "$@") || exit 1
b77bb64
eval set -- "$tmp"
b77bb64
b77bb64
while true; do
b77bb64
    case "$1" in
b77bb64
	(--help)	showHelp $0;;
b77bb64
	(--service)	SERVICE=$2;  shift;;
b77bb64
	(--version)	VERSION=$2;  shift;;
b77bb64
	(--release)	RELEASE=$2;  shift;;
b77bb64
	(--license)	LICENSE=$2;  shift;;
b77bb64
	(--username)	USERNAME=$2; shift;;
b77bb64
	(--)		shift; break;;
b77bb64
    esac
b77bb64
    shift
b77bb64
done
b77bb64
b77bb64
for i in SERVICE VERSION RELEASE LICENSE USERNAME; do
b77bb64
    eval tmp=\$${i}
b77bb64
    test "$tmp" || {
b77bb64
	echo $"No value for $i specified; assuming @${i}@"  >&2;
b77bb64
	eval $i=@${i}@;
b77bb64
    }
b77bb64
done
b77bb64
b77bb64
b77bb64
rpm.generatePreamble
b77bb64
rpm.generateHeader
b77bb64
rpm.genBody
b77bb64
rpm.genFiles