#!/bin/sh

# Shell script to handle installs on BSD-systems.
# Copyright (C) 2001-2004 Open Challenge B.V.
# Copyright (C) 2004-2005 OpenEyeT Professional Services.
# Copyright (C) 2005-2009 QNH.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; see the file COPYING.
# If not, write to the Free Software Foundation,
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# Contact information: http://scc.qnh-infrastructure.nl/index.html 

# SCC-release:	1.9.28
# file-version:	$Revision: 1.14 $

if [ $2 = "DEINSTALL" ]
then
	# Upgrades of packages are performed by removing the existing installation followed
	# by the install of the new version.
	# This part of the code runs when an existing package is removed. It is the first
	# code that runs during the upgrade.
	# We use this to record some settings to be used after the install of the new version.

	# file-version:	$Revision: 1.5 $
	
	# Do not use POSIX features in this script as it can be sourced by a non-POSIX system shell!
	
	SCC_BIN=/opt/scc/bin
	SCC_TOP=/var/opt/scc
	export SCC_BIN SCC_TOP
	
	SCC_DATA=${SCC_TOP}/data
	SCC_TMP=${SCC_TOP}/tmp
	SCC_CONF=/etc/opt/scc/conf
	export SCC_DATA SCC_TMP SCC_CONF
	
	# Avoid too strict permissions for newly created /var/opt directory.
	# Otherwise scc-srv will not work correctly.
	umask 022
	mkdir -p ${SCC_TOP} 2>/dev/null
	
	umask 077
	mkdir -p ${SCC_DATA} ${SCC_TMP} 2>/dev/null
	
	name=`uname -n | sed -e 's/\..*//'`
	
	# Just before the install we run the current version to detect any changes
	# in the configuration of the system. After the install we remove the current
	# snapshot to avoid comparison of the snapshots. This enables us to change
	# the format of the snapshots without flooding the log files with differences.
	if [ -x ${SCC_BIN}/scc-log ]
	then
		echo "NOTE:    Running scc to create a snapshot with the current"
		echo "NOTE:    version of the software. This may take some minutes."
	
		if [ -f ${SCC_DATA}/scc.${name}.lock ]
		then
			echo "ERROR:   install aborted, another instance of scc"
			echo "ERROR:   is active."
			exit 2
		else
			# During installs on HP-UX stm often hangs; do not run it.
			SCC_IGNORE_STM="yes";			export SCC_IGNORE_STM
	
			# During installs, rpm is locked; indicate the installation phase:
			SCC_INSTALL_PHASE="preinstall";		export SCC_INSTALL_PHASE
	
			${SCC_BIN}/scc-log
		fi
	fi
	
	# Avoid that scc is started between the pre- and the post-install.
	# We create the lock-file for scc-log together with a file indicating to 
	# the postinstall that the install is running.
	touch ${SCC_DATA}/scc.${name}.lock ${SCC_DATA}/scc.${name}.install
fi

if [ $2 = "POST-DEINSTALL" ]
then
	rmdir /opt/scc 2>/dev/null
fi

if [ $2 = "PRE-INSTALL" ]
then
	SCC_BIN=/opt/scc/bin
	SCC_TOP=/var/opt/scc
	export SCC_BIN SCC_TOP
	
	SCC_DATA=${SCC_TOP}/data
	SCC_TMP=${SCC_TOP}/tmp
	SCC_CONF=/etc/opt/scc/conf
	export SCC_DATA SCC_TMP SCC_CONF

	if [ ! -x ${SCC_BIN}/scc ]
	then
		# Fresh install. No version of scc yet present.
		# Avoid complaints of the post-install.
		umask 077
		name=`uname -n | sed -e 's/\..*//'`
		mkdir -p ${SCC_DATA}
		touch ${SCC_DATA}/scc.${name}.lock ${SCC_DATA}/scc.${name}.install
		mkdir -p ${SCC_CONF}
	fi
fi

if [ $2 = "POST-INSTALL" ]
then
	# The new release has been installed. Collect the new scc-data.

	# file-version: $Revision: 1.5 $
	
	# Do not use POSIX features in this script as it can be sourced by a non-POSIX system shell!
	
	SCC_BIN=/opt/scc/bin
	SCC_TOP=/var/opt/scc
	export SCC_BIN SCC_TOP
	
	SCC_DATA=${SCC_TOP}/data
	SCC_TMP=${SCC_TOP}/tmp
	SCC_CONF=/etc/opt/scc/conf
	SCC_NEWCONF=/etc/opt/scc/newconfig
	export SCC_DATA SCC_TMP SCC_CONF
	
	umask 077
	
	# Just before the install we run the current version to detect any changes
	# in the configuration of the system. After the install we remove the current
	# snapshot to avoid comparison of the snapshots. This enables us to change
	# the format of the snapshots without flooding the log files with differences.
	
	name=`uname -n | sed -e 's/\..*//'`
	
	if [ ! -f ${SCC_DATA}/scc.${name}.lock -a ! -f ${SCC_DATA}/scc.${name}.install ]
	then
		echo "WARNING: Lock- and or install-file do not exist during install."
		exit 2
	fi
	
	# Remove current snapshot and create new snapshot.
	echo "NOTE:    Running scc to create a snapshot with the new version"
	echo "NOTE:    of the software. This may take some minutes."
	
	# During installs on HP-UX, stm often hangs; do not run it.
	SCC_IGNORE_STM="yes";			export SCC_IGNORE_STM
	
	# During installs, rpm is locked; indicate the installation phase:
	SCC_INSTALL_PHASE="postinstall";	export SCC_INSTALL_PHASE
	
	rm -f ${SCC_DATA}/scc.${name}.lock ${SCC_DATA}/scc.${name}.install
	${SCC_BIN}/scc-log -c "install of SCC release 1.9.28" -r
	
	# Do not overwrite an existing stylesheet.
	if [ ! -f ${SCC_DATA}/style.css ]
	then
		cp ${SCC_NEWCONF}/style.css ${SCC_DATA}
		chmod 400 ${SCC_DATA}/style.css
	fi
	
	echo "NOTE:    The snapshot and logbook can be found in ${SCC_DATA}"
fi

exit 0
