#!/bin/sh

###
###  Copyright 2000-2007 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
HOSTNAME=`uname -n`

# 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'};

# The default action is to install the files, but can be
# set so we can deinstall the default files as well.
OP="install"


# 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=${BASEDIR}/etc/psg.conf.sample;
	SRC_MODULES=${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" -o "$2" = "POST-DEINSTALL" \) ]; then
	exit 0

# more FreeBSD Packages
elif [ $# -ge 2 -a -n "${PKG_PREFIX}" -a \( "$2" = "POST-INSTALL" -o "$2" = "DEINSTALL" \) ]; 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
	if [ "$2" = "DEINSTALL" ]; then
		OP="delete"

		### If we already have a psgconf_modules file,
		### look into what Package Manager we are using.
		res=`grep -c FreeBSD::Ports ${ROOT_PATH}${ETC_DIR}/psgconf_modules`;
		if [ ${res:=0} -gt 0 ]; then
			PKG_MGR="FreeBSD::Ports";
		fi
	fi

# 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 -
		res=$?
		if [ $res -ne 0 -a ${OP} = "install" ]; 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
		elif [ $res -eq 0 -a ${OP} = "delete" ]; then
			rm -f ${ROOT_PATH}${ETC_DIR}/psgconf_modules
		fi
	fi

	if [ ${OP} = "install" ]; then
		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;
	fi
else
	echo "Could not find source psgconf_modules file (${SRC_MODULES})"
	exit 1
fi

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

	# FIXME:  Need to figure out the default network interface...
	sed -e "s/^#hostname.*$/hostname \"$HOSTNAME\";/" \
		< ${SRC_CONF} \
		> ${ROOT_PATH}${ETC_DIR}/psg.conf
elif [ -f ${ROOT_PATH}${ETC_DIR}/psg.conf -a ${OP} = "delete" ]; then
	sed -e "s/^#hostname.*$/hostname \"$HOSTNAME\";/" \
		< ${SRC_CONF} \
		| cmp -s ${ROOT_PATH}${ETC_DIR}/psg.conf -
	if [ $? -eq 0 ]; then
		rm -f ${ROOT_PATH}${ETC_DIR}/psg.conf
	fi
fi

exit 0


syntax highlighted by Code2HTML, v. 0.9.1