#!/bin/sh

# Shell script to handle installs on archlinux with pacman.
# Copyright (C) 2007-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.7 $

# This script is either sourced or called directly. We have to support both ways.

# The available functions after soucring this file:
pre_install() {
	# 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
	return
}

pre_upgrade() {
	pre_install
	return
}

post_install() {
	# 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}"
	return
}

post_upgrade() {
	post_install
	return
}

# The supported values for the first argument:
case "${1}" in
pre_install)	# check for previous version of software
	pre_install
	;;

pre_upgrade)	# check for previous version of software
	pre_install
	;;

post_install)	# run the new version of the software
	post_install
	;;

post_upgrade)	# run the new version of the software
	post_install
	;;
esac

exit 0
