#!/bin/sh

###
###  Copyright 2000-2006 University of Illinois Board of Trustees
###  All rights reserved.
###
###  postinstall - post install script for use with most package
###				management systems (SVR4, encap, FreeBSD Pakages/Ports)
###
###  Campus Information Technologies and Educational Services
###  University of Illinois at Urbana-Champaign
###

DATE=`date +%Y-%m-%d`
PKG_NAME=psgconf

# ROOT_PATH is the relocatable start of the psgconf tree.
# Ala the root_path that is used in pkgadd(8) on Solaris.
# It's called DESTDIR in the ExtUtiks::MakeMaker or
# destdir in the Module::Build notation and
# PKG_INSTALL_ROOT in JumpStart
ROOT_PATH=${ROOT_PATH:=''};

# ETC_DIR is the default directory for psg.conf and psgconf_modules
# In most cases, it is /etc,
# but on FreeBSD it is /usr/local/etc
ETC_DIR=${ETC_DIR:-'/etc'};

# make install
if [ -n "${MAKE}" ]; then
	SRC_CONF=./etc/psg.conf;
	SRC_MODULES=./etc/psgconf_modules;
	PKG_MGR="Encap";

# Solaris SVR4 Packages
elif [ -n "${PKGINST}" ]; then
	ROOT_PATH=${PKG_INSTALL_ROOT:-};
	SRC_CONF=${CLIENT_BASEDIR}/etc/psg.conf.sample;
	SRC_MODULES=${CLIENT_BASEDIR}/etc/psgconf_modules.sample;
	PKG_MGR="Solaris";
	PKG_NAME=${PKGINST}

# Encap Packages
elif [ -n "${ENCAP_SOURCE}" ]; then
	SRC_CONF=${ENCAP_SOURCE}/${ENCAP_PKGNAME}/etc/psg.conf;
	SRC_MODULES=${ENCAP_SOURCE}/${ENCAP_PKGNAME}/etc/psgconf_modules;
	PKG_MGR="Encap";

# FreeBSD Packages
elif [ $# -ge 2 -a "$2" = "PRE-INSTALL" ]; then
	exit 0

# more FreeBSD Packages
elif [ $# -ge 2 -a -n "${PKG_PREFIX}" -a "$2" = "POST-INSTALL" ]; then
	ETC_DIR=${PKG_PREFIX}/etc;
	SRC_CONF=${PKG_PREFIX}/etc/psg.conf.sample;
	SRC_MODULES=${PKG_PREFIX}/etc/psgconf_modules.sample;
	if [ ! -n "$PKG_MGR" ]; then
		PKG_MGR="FreeBSD::Packages";
	fi
	PKG_NAME=$1

# RPMs
elif [ -n "${RPM}" ]; then
	SRC_CONF=/etc/psg.conf.sample;
	SRC_MODULES=/etc/psgconf_modules.sample;
	PKG_MGR="RedHat::${RPM}";
fi

if [ ! -d ${ROOT_PATH}${ETC_DIR} ]; then
	mkdir -p ${ROOT_PATH}${ETC_DIR}
fi

#
# Check to see if the current psgconf_modules file is the same as the
# new one we want to install.  If not, then install the new one.  Make
# sure we take into account the package management system we are using
# on this platform.
#
if [ -f ${SRC_MODULES} ]; then
	if [ -f ${ROOT_PATH}${ETC_DIR}/psgconf_modules ]; then
		sed -e "s/PackageManager::.*$/PackageManager::${PKG_MGR}/" \
			-e "s,config_file=.*$,config_file=${ETC_DIR}/psg.conf," \
			< ${SRC_MODULES} \
			| cmp -s ${ROOT_PATH}${ETC_DIR}/psgconf_modules -
		if [ $? -ne 0 ]; then
			if [ -f ${ROOT_PATH}${ETC_DIR}/psgconf_modules.lock ]; then
				echo "${ROOT_PATH}${ETC_DIR}/psgconf_modules file locked, could not update."
				exit 0
			fi
			if [ ! -f ${ROOT_PATH}${ETC_DIR}/psgconf_modules.pre.${PKG_NAME}.${DATE} ]; then
				cp ${ROOT_PATH}${ETC_DIR}/psgconf_modules ${ROOT_PATH}${ETC_DIR}/psgconf_modules.pre.${PKG_NAME}.${DATE};
			fi
		fi
	fi

	echo "Installing ${ROOT_PATH}${ETC_DIR}/psgconf_modules";
	sed -e "s/PackageManager::.*$/PackageManager::${PKG_MGR}/" \
		-e "s,config_file=.*$,config_file=${ETC_DIR}/psg.conf," \
		< ${SRC_MODULES} \
		> ${ROOT_PATH}${ETC_DIR}/psgconf_modules;
else
	echo "Could not find source psgconf_modules file (${SRC_MODULES})"
	exit 1
fi

if [ ! -f ${ROOT_PATH}${ETC_DIR}/psg.conf ]; then
	echo "Generating a default ${ROOT_PATH}${ETC_DIR}/psg.conf file";

	# FIXME:  Need to figure out the default network interface...
	HOSTNAME=`uname -n`
	sed -e "s/^#hostname.*$/hostname \"$HOSTNAME\";/" \
		< ${SRC_CONF} \
		> ${ROOT_PATH}${ETC_DIR}/psg.conf
fi

exit 0
