#!/bin/sh

# Shell script to install source version of scc
# 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.20 $
 
# To install the software, untar and call:
#    cd scc-1.9.28
#    ./scc-install

# This script is called in a directory with the following sub-directories:
#	src/install
#	src/software/opt/scc		(bin, doc and man)
#	src/software/var/opt/scc/conf	(conf, data, newconfig and tmp)

release="1.9.28"
umask 077

TESTING="testing"
x="$(echo "${TESTING}" 2>/dev/null)"
if [ "${x}" != "${TESTING}" ]
then
	# The /bin/sh on this system does not support the features we are using in scc.
	# Change to /bin/ksh. Abort the installation when this shell is absent.
	if [ ! -x /bin/ksh ]
	then
		echo "${0}: cannot install scc from src, no ksh or POSIX-shell found" >&2
		exit 1
	fi

	# Change the shell in all scripts.
	{
		find install -type f
		find software -type f
	}					|
	while read file
	do
		# Use ed to preserve the permissions.
		cat <<-_X_	| ed -s ${file} 2>/dev/null | sed -e '/^?$/d'
			1,$ s@^#!/bin/sh@#!/bin/ksh@
			1,$ s@SHELL=/bin/sh@SHELL=/bin/ksh@
			w
			q
		_X_
	done

	# Finally we have to re-run this script with ksh,
	# as the remainder of this script also uses subshells via $().
	exec /bin/ksh ${0} "$@"
fi

uid=$(id | sed -e 's/(.*//' -e 's/.*=//')
if [ "${uid}" != "0" ]
then
	echo "${0##*/}: only root should run this program" >&2
	exit 2
fi

if [ "$(uname -s)" != "OSF1" ]
then
	owner_group="root:$(id -gn)"
else
	owner_group="root:system"
fi

./install/preinstall

# Move the files to their places
for dir in /opt/scc /var/opt/scc /etc/opt/scc
do
	find software/${dir} -type f      |
	while read file
	do
		f_name=$(echo "${file}" | sed -e "s@.*${dir}/@${dir}/@")
		d_name=$(dirname ${f_name})

		if [ ! -d ${d_name} ]
		then
			mkdir -p ${d_name} 2>/dev/null
			chown ${owner_group} ${d_name}
		fi
		cp -p ${file} ${d_name}
		chown ${owner_group} ${d_name}/${file##*/}
	done
done

# The tar-ball contains some empty directories that are required by scc.
for dir in /etc/opt/scc /var/opt/scc
do
	find software/${dir} -type d	|
	while read dir
	do
		if [ ! -d ${d_name} ]
		then
			mkdir -p ${d_name} 2>/dev/null
			chown ${owner_group} ${d_name}
		fi
	done
done

SCC_INSTALL_METHOD="source";		export SCC_INSTALL_METHOD
./install/postinstall

exit 0
