#!/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

# SCC-release:	1.7.5
# file-version:	$Revision: 1.20 $

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 have nothin to do during this phase.
	:
fi

if [ $2 = "POST-INSTALL" ]
then
	# Empty directories are not installed on *BSD.
	while read dir perm
	do
		mkdir -p "${dir}"
		chmod "${perm}" "${dir}"
	done <<-_EOF_
		/var/opt/scc-srv/conf				770
		/var/opt/scc-srv/data/log			755
		/var/opt/scc-srv/data/transfer/mail		700
		/var/opt/scc-srv/data/transfer/cp		711
		/var/opt/scc-srv/data/www/scc-help		755
		/var/opt/scc-srv/data/www/scc-help/scc		755
		/var/opt/scc-srv/data/www/scc-help/scc-srv	755
		/var/opt/scc-srv/data/www/scc-help/scc-win	755
		/var/opt/scc-srv/tmp				770
	_EOF_

	# The help-pages on the website are altered to reflect the path under the
	# documentroot. To avoid complaints about the change of their md5sums, we
	# install the originals in a seperate directory and use copies for the website.
	for dir in scc scc-srv scc-win
	do
		for f in /var/opt/scc-srv/data/www/scc-help-new/${dir}/*
		do
			cp "${f}" /var/opt/scc-srv/data/www/scc-help/${dir}
		done
	done
	for f in /var/opt/scc-srv/data/www/scc-help-new/*
	do
		if [ -f "${f}" ]
		then
			cp "${f}" /var/opt/scc-srv/data/www/scc-help/
		fi
	done

	# The new release has been installed. Customize some settings recorded from the previous version.
	# file-version:	$Revision: 1.24 $
	
	# NOTE:
	# On Solaris this file is executed by sh. Do not use Posix functions!!!!!
	
	if [ -x /usr/xpg4/bin/awk ]
	then
		alias awk=/usr/xpg4/bin/awk
	fi
	
	SCC_BIN=/opt/scc-srv/bin
	SCC_DOC=/opt/scc-srv/doc
	SCC_TOP=/var/opt/scc-srv
	export SCC_BIN SCC_TOP SCC_DOC
	
	SCC_DATA=${SCC_TOP}/data
	SCC_TMP=${SCC_TOP}/tmp
	SCC_CONF=${SCC_TOP}/conf
	SCC_NEWCONF=${SCC_TOP}/newconfig
	SCC_WWW=${SCC_DATA}/www
	
	export SCC_DATA SCC_TMP SCC_CONF SCC_NEWCONF
	
	umask 077
	
	# For the package software that is unable to install empty directories.
	for dir in "${SCC_TMP}" "${SCC_CONF}" "${SCC_DATA}/log" "${SCC_DATA}/transfer/cp" "${SCC_DATA}/transfer/mail"
	do
		mkdir -p "${dir}"
	done
	
	for f in scc.conf
	do
		if [ ! -f ${SCC_CONF}/${f} ]
		then
			cp -p ${SCC_NEWCONF}/${f} ${SCC_CONF}
		fi
	done
	
	if [ -d /var/opt ]		# Test existence, in case of relocation.
	then
		chmod a+x /var/opt	# Correction for fresh install on some systems
	fi
	chmod a+x ${SCC_TOP}		# Correction for fresh install on some systems
	cd ${SCC_WWW}
	
	if [ -f custom/style.css ]
	then
		# Check for default, comment only style sheet from one of the previous releases.
		echo "/* Default stylesheet for scc-srv	*/" >custom/style.check
		cmp -s custom/style.css custom/style.check
		if [ $? -eq 0 ]			# Identical?
		then
			rm custom/style.css	# New file required.
		else
			# Check for definitions for colors when comparing systems.
			# This was added after release 1.5.11
			grep "^\.scc_var" custom/style.css >/dev/null 2>/dev/null
			if [ $? -ne 0 ]
			then
				# Add the extensions.
				cat <<-_X_ >>custom/style.css
					/*
					 * The following tag is used to specify the color of variable data in the snapshot.
					 * In previous versions of scc the variable SCC_VAR_COLOR was used for this purpose.
					 */
					.scc_var {
						color: purple;
					}
	
					/*
					 * The following tags are used to specify the colors used for the output when comparing systems.
					 * Used to be hard-coded in previous versions of scc-srv.
					 */
					.scc_sys_a {
						color: red;
					}
					.scc_sys_b {
						color: blue;
					}
	
				_X_
			fi
		fi
		rm -f custom/style.check
	fi
	if [ ! -f custom/style.css ]
	then
		cp ${SCC_NEWCONF}/style.css custom
		chmod 644 custom/style.css
	fi
	
	if [ ! -f custom/scc-auto-realm.conf ]
	then
		cp ${SCC_NEWCONF}/scc-auto-realm.conf custom
		chmod 644 custom/scc-auto-realm.conf
	fi
	
	if [ ! -f index.html ]
	then
		cp -p ${SCC_NEWCONF}/index.html .
	else
		# There is already an index.html. So this is not a fresh install, but an update.
		# Change the contents of the main index.html and the location of the manual pages of scc and scc-srv.
		# Installs with the new layout should not be affected by the following code.
	
		# Check for an old layout of the index file and try to update it. Use ed to preserve the permissions.
		cat <<-_X_	| ed -s index.html 2>/dev/null | sed -e '/^?$/d'
			1,$ s@OpenEyeT@QNH@g
			1,$ s@scc-help/scc-5@scc-help/scc/scc-5@g
			1,$ s@scc-help/scc-srv-5@scc-help/scc-srv/scc-srv-5@g
			w
			q
		_X_
	
		# In releases of scc-src prior to 1.5.2, the SCC resources in the index file
		# indicated the SCC client. Now we have to add the scc-win client and change
		# the client to Unix client.
		grep -l "scc-win" index.html 2>/dev/null >/dev/null
		if [ $? -ne 0 ]
		then
			set dummy `ls -ld index.html`
			case "${2}" in
			?r??r??r??)	new_mode=0444; umask 222;;
			?r??r??-??)	new_mode=0440; umask 227;;
			?r??-??-??)	new_mode=0400; umask 277;;
			?r??-??r??)	new_mode=0404; umask 272;;
			*)		new_mode=0400; umask 077;;
			esac
			new_owner_group="${4}:${5}"
			awk '/scc-5.html>SCC client/	{
								sub( "scc-5.html>SCC client", "scc-5.html>SCC Unix client" );
								print;
								sub( "scc/scc-5.html", "scc-win/scc-win-5.html" );
								sub( "Unix", "Windows" );
								print;
								next;
							}
							{
								print;
							}' index.html >index.html.tmp
			rm -f index.html
			mv index.html.tmp index.html
			chown "${new_owner_group}" index.html
	
			umask 077		# Reset to default
		fi
	
		# In releases of scc-srv up to and including 1.4.22, the manual pages
		# of scc resided in /opt/scc-srv/doc and /var/opt/scc-srv/data/www/scc-help.
		# Now they reside only in /var/opt/scc-srv/data/www/scc-help/scc.
		# Check the contents of this directory to determine which files to remove from the old directories.
		for man in scc-help/scc/*html
		do
			file=`basename ${man}`
			rm -f scc-help/${file} ${SCC_DOC}/${file}
		done
	
		# In releases of scc-srv up to and including 1.4.22, the manual pages
		# of scc resided in /opt/scc-srv/doc and /var/opt/scc-srv/data/www/scc-help.
		# The manual pages in the latter directory are moved to the subdirectory scc-srv in ./scc-help.
		# Check the contents of this subdirectory to determine which files to remove from the old directoriy.
		for man in scc-help/scc-srv/*html
		do
			file=`basename ${man}`
			rm -f scc-help/${file}
		done
	fi
	
	if [ ! -d All ]
	then
		warning=yes
	
		# There is no existing website. All files will be owned by root:root.
		# The administrator has to determine the correct settings.
		${SCC_BIN}/scc-realm -c All
	
		cp ${SCC_NEWCONF}/scc-rules.conf  ${SCC_NEWCONF}/scc-smt-select All/custom
	else
		# We renew the CGI-script in all realms.
		for d in */cgi-bin
		do
			if [ -d "${d}" ]
			then
				rm -f "${d}"/scc-wrapper.cgi
				cp -p ${SCC_BIN}/scc-wrapper.cgi "${d}"
			fi
		done
	
		# There is an existing website. Use the settings recorded in scc-update (-m option)
		if [ -f ${SCC_DATA}/transfer/mail_user_group ]
		then
			mail_opt="-m `cat ${SCC_DATA}/transfer/mail_user_group`"
		fi
		if [ -f "${SCC_WWW}/All/index.html" ]
		then
			set dummy `ls -ld "${SCC_WWW}/All/index.html"`
			web_opt="-w ${4}"
		fi
		rm -f ${SCC_DATA}/mail_owner_group ${SCC_DATA}/web_owner
	
		if [ ! -f All/custom/scc-rules.conf ]
		then
			cp ${SCC_NEWCONF}/scc-rules.conf All/custom
		fi
	
		if [ ! -f All/custom/scc-smt-select ]
		then
			cp ${SCC_NEWCONF}/scc-smt-select All/custom
		fi
	
		if [ "${mail_opt}${web_opt}" ]
		then
			${SCC_BIN}/scc-update -f ${mail_opt} ${web_opt}
		else
			warning=yes		# Nothing saved during preinstall ?!?!?!?
		fi
	fi
	
	if [ "${warning}" ]
	then
		echo "WARNING: All files for the website are owned by root:root"
		echo "WARNING: Use the -m and -w options of scc-update to correct this."
		echo "WARNING: "
		cat ${SCC_DATA}/README
	fi
	
	# Do not exit as this code is also sourced.
fi

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

# We do not need pre-install code. Possible preparations have been done by the DEINSTALL.

exit 0
