#!/bin/sh
# $Id: make_proto.sh.in,v 1.2 2001/05/28 04:19:47 tim Exp $
#
#	Tim Rice	Wed Sep 24 18:19:04 PDT 1997
#
#	Make prototype
#
#	System utilities used: cat, dirname, grep, mkdir, rm, sed, sort, tr
#
LANG=C export LANG
DIR_BIN=@BIN@
DIR_LIB=@LIBDATA@
DIR_SLIB=@LIBEXEC@
DIR_MAN=@MANDIR@
DIR_SBIN=@SBIN@
DIR_SPOOL=@SPOOL@
DIR_FONTS=@AFMDIR@
DIR_CGI=@CGIDIR@
DIR_HTML=@HTMLDIR@
DIR_SYSVINIT=@SYSVINITDIR@
DIR_SYSVINITSTART="@SYSVINITDIR@/@SYSVINITSTARTDIR@"
DIR_SYSVINITSTOP="@SYSVINITDIR@/@SYSVINITSTOPDIR@"
MANNUM1_8=@MANNUM1_8@
MANNUM4_5=@MANNUM4_5@
LMANNUM1_8=`echo ${MANNUM1_8} | tr '[A-Z]' '[a-z]'`
LMANNUM4_5=`echo ${MANNUM4_5} | tr '[A-Z]' '[a-z]'`
TMP_DIR=/tmp/.$$hylafax
TMP_LIST=$$tmp
LIST="${DIR_BIN} ${DIR_LIB} ${DIR_SLIB} ${DIR_MAN} ${DIR_SBIN}"

case $1 in
	client)	PROTOTYPE=cproto
		DO_HTML="no"
			;;
	server)	PROTOTYPE=sproto
		LIST="${LIST} ${DIR_SPOOL}"
		DO_HTML="@HTML@"
		[ -d ${TMP_DIR} ]  &&  {
			rm -fr ${TMP_DIR}  ||  {
				echo "rm -fr ${TMP_DIR} failed"
				exit 1
			}
		}
		(umask 077 ; mkdir ${TMP_DIR})
			;;
	*)	echo "usage: $0 server or client"
		exit 1 ;;
esac

for i in ${LIST}
do
# skip the first one, it's in prototype.stub
	i=`dirname ${i}`
	while [ ${i} != / ]
	do
		echo "d none ${i} ? ? ?" >> ${TMP_LIST}
		i=`dirname ${i}`
	done

done

if [ "@AFM@" = yes ]
then
# skip the first one, it's in prototype.stub
	DIR_FONTS=`dirname ${DIR_FONTS}`
	while [ ${DIR_FONTS} != / ]
	do
		echo "d none ${DIR_FONTS} ? ? ?" >> ${TMP_LIST}
		DIR_FONTS=`dirname ${DIR_FONTS}`
	done
fi

if [ ${DO_HTML} = yes ]
then
# skip the first one, it's in prototype.stub
	DIR_HTML=`dirname ${DIR_HTML}`
	while [ ${DIR_HTML} != / ]
	do
		grep "d none ${DIR_HTML} ? ? ?" ${TMP_LIST} >/dev/null \
		|| echo "d html ${DIR_HTML} ? ? ?" >> ${TMP_LIST}
		DIR_HTML=`dirname ${DIR_HTML}`
	done
	DIR_CGI=`dirname ${DIR_CGI}`
	while [ ${DIR_CGI} != / ]
	do
		grep "d html ${DIR_CGI} ? ? ?" ${TMP_LIST} >/dev/null \
		|| echo "d html ${DIR_CGI} ? ? ?" >> ${TMP_LIST}
		DIR_CGI=`dirname ${DIR_CGI}`
	done
fi

if [ ${PROTOTYPE} = sproto ]
then
	for i in ${DIR_SYSVINITSTART}
	do
		case ${i} in
			/*)	;;
			*)	i=${DIR_SYSVINIT}/${i} ;;
		esac
		mkdir -p ${TMP_DIR}/${i}
		i=`(cd ${TMP_DIR}/${i} ; pwd) | sed "s~${TMP_DIR}~~"`
		rm -fr ${TMP_DIR}/*
		echo "s none ${i}/@SYSVINITSTARTNAME@=@SYSVINITDIR@/hylafax" >> ${TMP_LIST}
		while [ ${i} != / ]
		do
			echo "d none ${i} ? ? ?" >> ${TMP_LIST}
			i=`dirname ${i}`
		done
	done
	for i in ${DIR_SYSVINITSTOP}
	do
		case ${i} in
			/*)	;;
			*)	i=${DIR_SYSVINIT}/${i} ;;
		esac
		mkdir -p ${TMP_DIR}/${i}
		i=`(cd ${TMP_DIR}/${i} ; pwd) | sed "s~${TMP_DIR}~~"`
		rm -fr ${TMP_DIR}/*
		echo "s none ${i}/@SYSVINITSTOPNAME@=@SYSVINITDIR@/hylafax" >> ${TMP_LIST}
		while [ ${i} != / ]
		do
			echo "d none ${i} ? ? ?" >> ${TMP_LIST}
			i=`dirname ${i}`
		done
	done
	rm -fr ${TMP_DIR}

	echo "f none ${DIR_SYSVINIT}/hylafax=../etc/hylafax 0755 @SYSUID@ @SYSGID@" >> ${TMP_LIST}
	while [ ${DIR_SYSVINIT} != / ]
	do
		echo "d none ${DIR_SYSVINIT} ? ? ?" >> ${TMP_LIST}
		DIR_SYSVINIT=`dirname ${DIR_SYSVINIT}`
	done

	[ -d /etc/config ]  &&  {
		echo "d none /etc ? ? ?" >> ${TMP_LIST}
		echo "d none /etc/config ? ? ?" >> ${TMP_LIST}
		echo "f none /etc/config/fax=../etc/config.fax 0644 @SYSUID@ @SYSGID@" >> ${TMP_LIST}
	}
fi

# prototype file
cat >${PROTOTYPE} <<EOF
i copyright
i depend
i pkginfo
i request
i postinstall
i preremove
i postremove
EOF

grep -v "^#" proto.local >> ${PROTOTYPE}

sort -u ${TMP_LIST} >> ${PROTOTYPE}

rm ${TMP_LIST}

grep " none " ${PROTOTYPE}.stub >> ${PROTOTYPE}
grep " man " ${PROTOTYPE}.stub | sed -e "s~\.${MANNUM1_8}=~.${LMANNUM1_8}=~" \
	-e "s~\.${MANNUM4_5}=~.${LMANNUM4_5}=~" >> ${PROTOTYPE}

if [ "@AFM@" = yes ]
then
	grep " fonts " ${PROTOTYPE}.stub >> ${PROTOTYPE}
fi

if [ ${DO_HTML} = yes ]
then
	grep " html " ${PROTOTYPE}.stub >> ${PROTOTYPE}
fi


