#!/bin/sh

grep csnet-ns /etc/services > /dev/null 2>&1
if [ $? -ne 0 ]; then
	echo "adding csnet-ns to /etc/services..."
	cp /etc/services /etc/services.pre.${ENCAP_PKGNAME}
	echo "csnet-ns		105/tcp		ns	# PH" >> /etc/services
fi

if [ ! -d "${ENCAP_TARGET}/etc" ]; then
	echo "creating directory ${ENCAP_TARGET}/etc...";
	mkdir -p "${ENCAP_TARGET}/etc";
fi

if [ ! -f "${ENCAP_TARGET}/etc/ph_server" ]; then
	PH_SERVER=`hostname | awk -F. '(NF > 1) { printf "ns.%s.%s\n", $(NF - 1), $NF; }'`;

	if [ -z "${PH_SERVER}" ]; then
		if [ -x /usr/sbin/nslookup ]; then
			NSLOOKUP=/usr/sbin/nslookup;
		elif [ -x /usr/bin/nslookup ]; then
			NSLOOKUP=/usr/bin/nslookup;
		fi

		if [ -n "${NSLOOKUP}" ]; then
			PH_SERVER=`hostname | xargs ${NSLOOKUP} | awk '/^Name:/ { print $2; exit; }' | awk -F. '{ printf("ns.%s.%s\n", $(NF - 1), $NF); }'`;
		fi
	fi

	if [ -n "${PH_SERVER}" ]; then
		echo "creating default ${ENCAP_TARGET}/etc/ph_server file...";
		echo "${PH_SERVER}" > ${ENCAP_TARGET}/etc/ph_server;
	else
		echo "WARNING: cannot determine PH server name - you must create ${ENCAP_TARGET}/etc/ph_server manually";
	fi
fi

if [ ! -f "${ENCAP_TARGET}/etc/nphrc" ]; then
	echo "installing default ${ENCAP_TARGET}/etc/nphrc file...";
	cp ${ENCAP_SOURCE}/${ENCAP_PKGNAME}/nphrc ${ENCAP_TARGET}/etc/nphrc;
fi

