#
# Makefile for building a Solaris package of SSH 
#   $Id: ssh.Makefile,v 1.8 1998/11/02 00:08:15 dgregor Exp $
# 
# Copyright (c) 1998 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 SSH source from the master site at
#   ftp://ftp.cs.hut.fi/pub/ssh/
# If you are in the US or Canada, you can also get it from
#   ftp://ftp.net.ohio-state.edu/pub/security/ssh/
#
# The 'homepage' for SSH is at
#   http://www.cs.hut.fi/ssh/
#
# You also need to get the patch for the log_msg buffer overflow from:
#   ftp://ftp.gregor.com/pub/dgregor/ssh-1.2.26-log_msg.patch
#

BUILDDIR    = ssh-1.2.26

PKGNAME     = TYssh
DESTDIR     = ${TOPDIR}/pkg 

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

CONFOPTS    = 

# if you don't want to apply any patches
#APPLYPATCHES = true
#OTHERNAMEADD = 

APPLYPATCHES = /usr/local/bin/patch < ssh-1.2.26-log_msg.patch
OTHERNAMEADD = +log_msg-patch-1

CONFINUSA_YES = --with-rsaref
USACONFOPTS = ${CONFINUSA_${USA_RESIDENT}}

NAMEINUSA_YES = +rsaref
USANAMEADD  = ${NAMEINUSA_${USA_RESIDENT}}

NAMESH      = echo "ssh (${USANAMEADD} ${OTHERNAMEADD})" | sed 's/  / /g;s/ ()//;s/( //;s/ )//'
NAME        = ${NAMESH:sh}
DESC        = Secure SHell
ARCH:sh     = uname -p
FULLVER     = echo ${BUILDDIR} | sed 's/.*-//'
CATEGORY    = application
VENDOR      = Tatu Ylonen
EMAIL       = dj@gregor.com
PSTAMP      = D.J. Gregor
CLASSES     = none

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

INSTUSER    = root
INSTGROUP   = root

SPOOLDIR    = /var/spool/pkg

TOPDIR:sh   = pwd

RSAREFDIST  = rsaref20.tar.Z
RSAREFUNTAR = ${BUILDDIR}/rsaref2

all: pkg

usa-resident-test:
	@if [ "x${USA_RESIDENT}" != "xYES" -a "x${USA_RESIDENT}" != "xNO" ]; \
	then \
		echo "" ;\
		echo "\tYou must set the variable USA_RESIDENT to YES if you" ;\
		echo "\tare a resident of the USA, or NO otherwise." ;\
		echo "\tIf you are a USA resident, you must get the file" ;\
		echo "\t${RSAREFDIST} and place it in the same" ;\
		echo "\tdirectory as the distribution tar file of SSH." ;\
		echo "" ;\
		echo "\tftp://ftp.rsa.com/rsaref/README will tell you how" ;\
		echo "\tto get ${RSAREFDIST}" ;\
		echo "" ;\
		exit 1 ;\
	fi

	@if [ "x${USA_RESIDENT}" = "xYES" ]; \
	then \
		$(MAKE) test-for-rsaref ; \
	fi
	
test-for-rsaref:
	@( test -f ${RSAREFDIST} && exit 0 ; \
		echo "" ;\
		echo "\tCould not find ${RSAREFDIST}" ;\
		echo "" ;\
		echo "\tIf you are a USA resident, you must get the file" ;\
		echo "\t${RSAREFDIST} and place it in the same" ;\
		echo "\tdirectory as the distribution of SSH." ;\
		echo "" ;\
		echo "\tftp://ftp.rsa.com/rsaref/README will tell you how" ;\
		echo "\tto get ${RSAREFDIST}" ;\
		echo "" ;\
		exit 1 \
	)

build: usa-resident-test .configured.${BUILDDIR}
	cd ${BUILDDIR} ; ${MAKE}

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

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

pkg:	${BUILDDIR}.pkg

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

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

	@if [ "x${USA_RESIDENT}" = "xYES" ]; \
	then \
		mkdir ${RSAREFUNTAR} ;\
		( cd ${RSAREFUNTAR} && \
			zcat ${TOPDIR}/${RSAREFDIST} | tar xvf - ) ;\
	fi

	touch .untarred.${BUILDDIR}

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

install-pkg:  build clean.${DESTDIR} ${DESTDIR}
	touch ${DESTDIR}${ETCDIR}/ssh_host_key
	cd ${BUILDDIR} ; ${MAKE} install_prefix=${DESTDIR} \
		INSTALL=/usr/ucb/install install
	-strip ${DESTDIR}${BASEDIR}/bin/*
	-strip ${DESTDIR}${BASEDIR}/sbin/*
	rm -f ${DESTDIR}${ETCDIR}/ssh_host_key*
	sed -e 's/PrintMotd yes/PrintMotd no/' \
		< ${DESTDIR}${ETCDIR}/sshd_config \
		> ${DESTDIR}${ETCDIR}/sshd_config.new
	rm ${DESTDIR}${ETCDIR}/sshd_config
	mv ${DESTDIR}${ETCDIR}/sshd_config.new \
		${DESTDIR}${ETCDIR}/sshd_config

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

.configured.${BUILDDIR}: .patched.${BUILDDIR}
	cd ${BUILDDIR} ; ./configure --prefix=${BASEDIR} \
		--with-etcdir=${ETCDIR} ${CONFOPTS} ${USACONFOPTS}
	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/ssh ${DESTDIR}/etc/rc2.d/S82ssh
	( 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' \
	 ) > ${DESTDIR}/prototype

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

${DESTDIR}/install/postinstall: ${DESTDIR}/install
	@echo "#!/bin/sh" >> $@
	@echo "" >> $@
	@echo 'test -f $${PKG_INSTALL_ROOT}${ETCDIR}/ssh_host_key || $${PKG_INSTALL_ROOT}${BASEDIR}/bin/ssh-keygen -b 1024 -f $${PKG_INSTALL_ROOT}${ETCDIR}/ssh_host_key -N ""' >> $@
	@echo "" >> $@
	@echo "if [ \"x\$${PKG_INSTALL_ROOT}\" = \"x\" ]" >> $@
	@echo "then" >> $@
	@echo "	/etc/init.d/ssh start" >> $@
	@echo "else" >> $@
	@echo "	echo \"SSH 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/ssh stop" >> $@
	@echo "fi" >> $@
	chmod 755 $@

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

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

	@echo "#!/bin/sh" >> $@
	@echo "" >> $@
	@echo "PATH=/sbin:/usr/bin:/usr/sbin" >> $@
	@echo "export PATH" >> $@
	@echo "" >> $@
	@echo "SSHD=${BASEDIR}/sbin/sshd" >> $@
	@echo "PIDFILE=${ETCDIR}/sshd.pid" >> $@
	@echo "" >> $@
	@echo 'case "$$1" in' >> $@
	@echo "	'start')" >> $@
	@echo "		# Start sshd" >> $@
	@echo '		$${SSHD}' >> $@
	@echo '		if [ $$? -ne 0 ]; then' >> $@
	@echo "			echo \"error starting ssh\" >&2" >> $@
	@echo "			exit 1" >> $@
	@echo "		fi" >> $@
	@echo "       	;; " >> $@
	@echo "" >> $@
	@echo "	'stop')" >> $@
	@echo "	       	# Stop sshd" >> $@
	@echo '		kill `cat $$PIDFILE`' >> $@
	@echo "       	;; " >> $@
	@echo "" >> $@
	@echo "" >> $@
	@echo "	*)	# usage" >> $@
	@echo '		echo "usage: $$0 start|stop" >&2' >> $@
	@echo "		exit 1" >> $@
	@echo "	;;" >> $@
	@echo "" >> $@
	@echo "esac  " >> $@
	chmod 755 $@

${DESTDIR}/etc/rc2.d/S82ssh: ${DESTDIR}/etc/init.d/ssh
	mkdir -p ${DESTDIR}/etc/rc2.d 
	cd ${DESTDIR}/etc/rc2.d ; ln -s ../init.d/ssh S99ssh
	mkdir -p ${DESTDIR}/etc/rc0.d 
	cd ${DESTDIR}/etc/rc0.d ; ln -s ../init.d/ssh S10ssh
	mkdir -p ${DESTDIR}/etc/rc1.d 
	cd ${DESTDIR}/etc/rc1.d ; ln -s ../init.d/ssh S10ssh

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