#
# Makefile for building a Solaris package of prngd 
#   $Id: prngd.Makefile,v 1.1 2001/11/27 17:25:08 dgregor Exp $
# 
# Copyright (c) 1998-2001 Daniel J. Gregor, Jr., All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. All advertising materials mentioning features or use of this software
#    must display the following acknowledgement:
# 	This product includes software developed by Daniel J. Gregor, Jr.
# 4. The name of Daniel J. Gregor, Jr. may not be used to endorse or promote
#    products derived from this software without specific prior written
#    permission.
# 
# THIS SOFTWARE IS PROVIDED BY DANIEL J. GREGOR, JR. ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL DANIEL J. GREGOR, JR. BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
#
# You can obtain the prngd source from the master site at
#   http://ftp.aet.TU-Cottbus.DE/personen/jaenicke/postfix_tls/prngd.html
#

BUILDDIR    = prngd-0.9.23

PKGNAME     = LJprngd
DESTDIR     = ${TOPDIR}/pkg 

BASEDIR     = /opt/${PKGNAME}
ETCDIR      = /etc/opt/${PKGNAME}
VARDIR      = /var/opt/${PKGNAME}

CONFIGFILE  = ${ETCDIR}/prngd.conf
RANDSAVE    = ${VARDIR}/prngd-seed

CONFOPTS    = 

NAME        = prngd
DESC        = prngd - a pseudo-random, but strong, random number generator
ARCH:sh     = uname -p
FULLVER     = echo ${BUILDDIR} | sed 's/.*-//'
CATEGORY    = application
VENDOR      = Lutz Jaenicke
EMAIL       = dj@gregor.com
CLASSES     = none
PSTAMP:sh   = date "+`hostname`%Y%m%d%H%M%S"

PKGPROTO    = /usr/bin/pkgproto
PKGMK       = /usr/bin/pkgmk
PKGTRANS    = /usr/bin/pkgtrans

INSTUSER    = root
INSTGROUP   = root

SPOOLDIR    = /var/spool/pkg

TOPDIR:sh   = pwd

all: pkg

build: .configured.${BUILDDIR}
	cd ${BUILDDIR} ; ${MAKE} \
		CFLAGS="-O -DSOLARIS" \
		SYSLIBS="-lsocket -lnsl" \
		DEFS="-DCONFIGFILE=\\\"${CONFIGFILE}\\\" -DRANDSAVENAME=\\\"${RANDSAVE}\\\""

clean:
	cd ${BUILDDIR} ; ${MAKE} clean

pkg:	${BUILDDIR}.pkg

${DESTDIR}:
	mkdir ${DESTDIR}
	mkdir -p ${DESTDIR}/${BASEDIR}
	mkdir -p ${DESTDIR}/${ETCDIR}
	mkdir -p ${DESTDIR}/${VARDIR}
	mkdir -p ${DESTDIR}/${VARDIR}

.untarred.${BUILDDIR}: ${BUILDDIR}.tar.gz 
	gzip -cd ${BUILDDIR}.tar.gz | tar xvf -
	touch .untarred.${BUILDDIR}

clean.${DESTDIR}:
	rm -rf ${DESTDIR}

install-pkg:  build clean.${DESTDIR} ${DESTDIR}
	mkdir ${DESTDIR}${BASEDIR}/sbin
	cp ${BUILDDIR}/prngd ${DESTDIR}${BASEDIR}/sbin/.
	cp ${BUILDDIR}/contrib/Solaris-7/prngd.conf.solaris-7 \
		${DESTDIR}${CONFIGFILE}.default

.patched.${BUILDDIR}: .untarred.${BUILDDIR}
	${APPLYPATCHES}
	touch .patched.${BUILDDIR}

.configured.${BUILDDIR}: .patched.${BUILDDIR}
	touch .configured.${BUILDDIR}

veryclean:
	rm -rf ${BUILDDIR} ${DESTDIR} .configured.${BUILDDIR} \
		.untarred.${BUILDDIR} .patched.${BUILDDIR}

${BUILDDIR}.pkg: ${DESTDIR}/prototype
	( cd ${DESTDIR} ; ${PKGMK} -d ${SPOOLDIR} -or . )
	( cd ${SPOOLDIR}/${PKGNAME}/root ; find . -depth -print | \
		grep -v '^\.$$' | cpio -odm | compress > ../root.cpio.Z )
	rm -rf ${SPOOLDIR}/${PKGNAME}/root
	$(PKGTRANS) ${SPOOLDIR} ${TOPDIR}/${BUILDDIR}.pkg ${PKGNAME}

${DESTDIR}/prototype: ${DESTDIR}/pkginfo ${DESTDIR}/install/postinstall \
	${DESTDIR}/install/postremove ${DESTDIR}/install/preremove \
	${DESTDIR}/etc/init.d/${NAME} ${DESTDIR}/etc/rc2.d/S20${NAME}
	( cd ${DESTDIR} ; echo "i pkginfo" ; \
	 find . -print | egrep -v './(prototype|pkginfo)' | $(PKGPROTO) | \
	 sed 's/ \([0-9][0-9]*\) [^ ][^ ]* [^ ][^ ]*$$/ \1 '"${INSTUSER} ${INSTGROUP}/" | \
	 sed 's/^f none install\/\([^ ][^ ]*\) .*$$/i \1=install\/\1/' | \
	 sed '/^d none install/d' | \
	 sed 's/none /none \//' | \
	 sed '/\/opt /d;/\/etc /d;/\/etc\/init.d /d;/\/etc\/rc.\.d /d' | \
	 sed '/\/var /d;/\/var\/opt /d;/\/etc\/opt /d' \
	 ) > ${DESTDIR}/prototype

${DESTDIR}/pkginfo:	install-pkg
	rm -f $@
	@echo "PKG=\"${PKGNAME}\"" >> $@
	@echo "NAME=\"${NAME}\"" >> $@
	@echo "ARCH=\"${ARCH}\"" >> $@
	@echo "VERSION=\"${FULLVER:sh}\" (`uname -s`-`uname -r`)" >> $@
	@echo "CATEGORY=\"${CATEGORY}\"" >> $@
	@echo "VENDOR=\"${VENDOR}\"" >> $@
	@echo "EMAIL=\"${EMAIL}\"" >> $@
	@echo "BASEDIR=\"/\"" >> $@
	@echo "CLASSES=\"${CLASSES}\"" >> $@
	@echo "PSTAMP=\"${PSTAMP}\"" >> $@

${DESTDIR}/install/postinstall: ${DESTDIR}/install
	@echo "#!/bin/sh" >> $@
	@echo "" >> $@
	@echo 'test -f $${PKG_INSTALL_ROOT}${RANDSAVE} || \' >> $@
	@echo '	tail -100 /var/adm/* /var/log/* > \' >> $@
	@echo '	$${PKG_INSTALL_ROOT}${RANDSAVE}' >>$@
	@echo 'test -f $${PKG_INSTALL_ROOT}${CONFIGFILE} || \' >> $@
	@echo '	cp $${PKG_INSTALL_ROOT}${CONFIGFILE}.default \' >> $@
	@echo '	$${PKG_INSTALL_ROOT}${CONFIGFILE}' >> $@
	@echo "" >> $@
	@echo "if [ \"x\$${PKG_INSTALL_ROOT}\" = \"x\" ]" >> $@
	@echo "then" >> $@
	@echo "	/etc/init.d/${NAME} start" >> $@
	@echo "else" >> $@
	@echo "	echo \"${NAME} will start next time you reboot the machine\"" >> $@
	@echo "fi" >> $@
	chmod 755 $@

${DESTDIR}/install/preremove: ${DESTDIR}/install
	rm -f $@
	@echo "#!/bin/sh" >> $@
	@echo "" >> $@
	@echo "if [ \"x\$${PKG_INSTALL_ROOT}\" = \"x\" ]" >> $@
	@echo "then" >> $@
	@echo "        /etc/init.d/${NAME} stop" >> $@
	@echo "fi" >> $@
	chmod 755 $@

${DESTDIR}/install/postremove: ${DESTDIR}/install
	rm -f $@
	@echo "echo \"Run \\\"rm -rf \$${PKG_INSTALL_ROOT}${ETCDIR} \$${PKG_INSTALL_ROOT}${VARDIR}\\\" to remove ${NAME} configuration files" >> $@
	chmod 755 $@

${DESTDIR}/etc/init.d/${NAME}:
	mkdir -p ${DESTDIR}/etc/init.d
	rm -f $@

	@echo "#!/bin/sh" >> $@
	@echo "#" >> $@
	@echo "# Start/Stop the PRNG entropy-gathering daemon" >> $@
	@echo "# For Solaris 8" >> $@
	@echo "#" >> $@
	@echo "# Links should be set up using something of the form:" >> $@
	@echo "#" >> $@
	@echo "# ln -s /usr/local/init.d/prngd /etc/rc0.d/K30prngd" >> $@
	@echo "# ln -s /usr/local/init.d/prngd /etc/rc1.d/K30prngd" >> $@
	@echo "# ln -s /usr/local/init.d/prngd /etc/rc2.d/S20prngd" >> $@
	@echo "# ln -s /usr/local/init.d/prngd /etc/rcS.d/K30prngd" >> $@
	@echo "#" >> $@
	@echo "# Joseph Walton, 2001-08-06" >> $@
	@echo "" >> $@
	@echo "DESC='PRNGD entropy-gathering daemon'" >> $@
	@echo "" >> $@
	@echo "PRNGD=${BASEDIR}/sbin/prngd" >> $@
	@echo "SOCKET=${VARDIR}/prngd-socket" >> $@
	@echo "" >> $@
	@echo "COMMAND=\"\"" >> $@
	@echo "" >> $@
	@echo 'case $$1 in' >> $@
	@echo "'start')" >> $@
	@echo '        if ls $$SOCKET > /dev/null 2>&1; then' >> $@
	@echo '                rm -f $$SOCKET' >> $@
	@echo "        fi" >> $@
	@echo '        COMMAND="$$PRNGD $$SOCKET"' >> $@
	@echo "        CONCLUSION='started'" >> $@
	@echo "        ;;" >> $@
	@echo "" >> $@
	@echo "'stop')" >> $@
	@echo '        if ls $$SOCKET > /dev/null 2>&1; then' >> $@
	@echo '                COMMAND="$$PRNGD $$SOCKET -k"' >> $@
	@echo "        fi" >> $@
	@echo "        CONCLUSION='stopped'" >> $@
	@echo "        ;;" >> $@
	@echo "" >> $@
	@echo "*)" >> $@
	@echo '        echo "Usage: $$0 { start | stop }"' >> $@
	@echo "        exit 1" >> $@
	@echo "        ;;" >> $@
	@echo "esac" >> $@
	@echo "" >> $@
	@echo 'if $$COMMAND; then' >> $@
	@echo '        echo $$DESC $$CONCLUSION' >> $@
	@echo "else" >> $@
	@echo '        echo $$DESC not $$CONCLUSION' >> $@
	@echo "fi" >> $@
	chmod 755 $@

${DESTDIR}/etc/rc2.d/S20${NAME}: ${DESTDIR}/etc/init.d/${NAME}
	mkdir -p ${DESTDIR}/etc/rc2.d 
	mkdir -p ${DESTDIR}/etc/rc0.d 
	mkdir -p ${DESTDIR}/etc/rc1.d 
	mkdir -p ${DESTDIR}/etc/rcS.d 
	ln -s ../init.d/${NAME} ${DESTDIR}/etc/rc2.d/S20${NAME}
	ln -s ../init.d/${NAME} ${DESTDIR}/etc/rc0.d/K30${NAME}
	ln -s ../init.d/${NAME} ${DESTDIR}/etc/rc1.d/K30${NAME}
	ln -s ../init.d/${NAME} ${DESTDIR}/etc/rcS.d/K30${NAME}

${DESTDIR}/install:
	mkdir -p ${DESTDIR}/install
