Blob Blame History Raw
#! /bin/bash

# Copyright (C) 2004 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
#  
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#  
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#  
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

function showHelp()
{
    echo \
$"Usage: clamd-gen --service=<SERVICE> --version=<VERSION> --release=<RELEASE>
                   --license=<LICENSE> --username=>USERNAME>
"
    exit 0
}

function rpm.generatePreamble()
{
    cat <<EOF
%{!?release_func:%define release_func()	%1%{?dist}}
# The name of the minit service
%define minitsvcdir	%minitdir/services/%name
# The configuration file for the SysV initservice
%define conffile	%_sysconfdir/clamd.d/%service.conf
# The directory, where the milter socket will be placed into; this
# socket will be named clamd.sock
%define rundir		/var/run/clamd.%service
# The name of the logfile
%define logfile		/var/log/clamd.%service
# The user under whose id, the clamd shall be running. This user must
# be able to read the files from the base-service and is usually
# created there.
%define username	$USERNAME
# The packagename of the service
%define service		$SERVICE
# The service name as used by the system's initscripts; usually this
# is %service
%define baseservice	%service

%define __chkconfig	/sbin/chkconfig
%define minitdir	%_sysconfdir/minit


EOF
}

function rpm.generateHeader()
{
    cat <<EOF
Summary:		Clamav server for '%service'
Name:			clamd.%service
Version:		$VERSION
Release:		%release_func $RELEASE
Epoch:			0
License:		$LICENSE
Group:			System Environment/Daemons
BuildRoot:		%_tmppath/%name-%version-%release-root
BuildArch:		noarch
Requires:		init(%name)
Requires(pre):		%service
Requires:		clamav-server
BuildRequires:		clamav-devel

%package sysv
Summary:		SysV initscripts for a %service clamav-server
Group:			System Environment/Daemons
Provides:		init(%name) = sysv
Conflicts:		init(%name) < sysv
Conflicts:		init(%name) > sysv
Requires:		clamav-server-sysv
Requires(post):		%name = %epoch:%version-%release
Requires(post):		diffutils mktemp %__chkconfig
Requires(preun):	%__chkconfig
Requires(pre):		%_initrddir
Requires(postun):	%_initrddir

%package minit
Summary:		minit initscripts for a %service clamav-server
Group:			System Environment/Daemons
Provides:		init(%name) = minit
Conflicts:		init(%name) < minit
Conflicts:		init(%name) > minit
Requires(post):		%name = %epoch:%version-%release
Requires(post):		diffutils mktemp
Requires(pre):		minit-setup
Requires(postun):	minit-setup
Requires(triggers):	minit-tools


%description
Basic setup for a clamav server for '%service'.


%description sysv
Basic setup for a clamav server for '%service'.

This package contains initscripts for SysV based systems.


%description minit
Basic setup for a clamav server for '%service'.

This package contains initscripts for minit based systems.

EOF
}


function rpm.genBody()
{
    cat <<"XEOFX"
%prep
%build

%install
rm -rf $RPM_BUILD_ROOT
%__install -d -m755 $RPM_BUILD_ROOT{%minitsvcdir,%_sbindir,%rundir,/var/log}

d=/usr/share/clamav/template

function subst
{
	src=$d/$1
	dst=$RPM_BUILD_ROOT$2

	%__install -d -m755 $(dirname "$dst")
	sed -e 's!^\(#?LogFile \).*!\1%logfile!g;
		s!^#?\(LocalSocket \).*!\1%rundir/clamd.sock!g;
		s!^#?\(PidFile \).*!\1%rundir/clamd.pid!g;
		s!<SERVICE>!%service!g;
		s!<USER>!%username!g;' "$src" >"$dst"
	chmod --reference "$src" "$dst"
}

subst clamd.conf      %conffile
subst clamd.logrotate %_sysconfdir/logrotate.d/clamd.%service

%if 0%{!?_without_sysv:1}
subst clamd.sysconfig %_sysconfdir/sysconfig/clamd.%service
subst clamd.init      %_initrddir/clamd.%service
%endif

ln -s clamd $RPM_BUILD_ROOT%_sbindir/clamd.%service

touch $RPM_BUILD_ROOT%logfile
touch $RPM_BUILD_ROOT%rundir/clamd.sock

%if 0%{!?_without_minit:1}
ln -s %_sbindir/clamd.%service $RPM_BUILD_ROOT%minitsvcdir/run
touch                          $RPM_BUILD_ROOT%minitsvcdir/respawn
cat <<EOF                     >$RPM_BUILD_ROOT%minitsvcdir/params
-c
%conffile
EOF
%endif

%clean
rm -rf $RPM_BUILD_ROOT


%triggerin minit -- %baseservice
minit-svc add services/clamd.%service services/%baseservice/

%triggerun minit -- %baseservice
test "$1" != 0 -a "$2" != 0 || \
	minit-svc del services/clamd.%service services/%baseservice/


%post minit
d=$(mktemp /tmp/clamd.%service.XXXXXX)
sed -e 's!^#Foreground!Foreground!' "%conffile" >"$d"
grep -q '^Foreground' $d || echo 'Foreground' >>$d
cmp -s "$d" %conffile || cat "$d" >"%conffile"
rm -f "$d"

%post sysv
d=$(mktemp /tmp/clamd.%service.XXXXXX)
sed -e 's!^Foreground!#Foreground!' "%conffile" >"$d"
cmp -s "$d" %conffile || cat "$d" >"%conffile"
rm -f "$d"

%__chkconfig --add %name


%preun sysv
test "$1" != 0 || %__chkconfig --del %name

XEOFX
}


function rpm.genFiles
{
    cat <<"EOF"
%files
%defattr(-,root,root,-)
%doc
%config(noreplace) %verify(not size md5 mtime) %attr(0620,root,%username)  %logfile
%config(noreplace) %verify(not mtime) %conffile
%config(noreplace) %verify(not mtime) %_sysconfdir/logrotate.d/clamd.%service
%_sbindir/clamd.%service
%dir %attr(0700,%username,root) %rundir
%ghost %rundir/clamd.sock


%if 0%{!?_without_sysv:1}
%files sysv
%defattr(-,root,root,-)
%config            %verify(not mtime) %_initrddir/clamd.%service
%config(noreplace) %verify(not mtime) %_sysconfdir/sysconfig/clamd.%service
%endif


%if 0%{!?_without_minit:1}
%files minit
%defattr(-,root,root,-)
%dir                                  %minitsvcdir
%config(noreplace) %verify(not mtime) %minitsvcdir/params
%config                               %minitsvcdir/run
                                      %minitsvcdir/respawn
%endif
EOF
}


SERVICE=
VERSION=
RELEASE=
LICENSE=
USERNAME=
tmp=$(getopt -o '' --long service:,version:,release:,license:,username:,help -n "$0" -- "$@") || exit 1
eval set -- "$tmp"

while true; do
    case "$1" in
	(--help)	showHelp $0;;
	(--service)	SERVICE=$2;  shift;;
	(--version)	VERSION=$2;  shift;;
	(--release)	RELEASE=$2;  shift;;
	(--license)	LICENSE=$2;  shift;;
	(--username)	USERNAME=$2; shift;;
	(--)		shift; break;;
    esac
    shift
done

for i in SERVICE VERSION RELEASE LICENSE USERNAME; do
    eval tmp=\$${i}
    test "$tmp" || {
	echo $"No value for $i specified; assuming @${i}@"  >&2;
	eval $i=@${i}@;
    }
done


rpm.generatePreamble
rpm.generateHeader
rpm.genBody
rpm.genFiles