#!/bin/sh

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

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

release="1.7.5"
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-srv 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
		case "${file}" in
		*.png)	# Do not change binary file
			;;

		*)	# 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_
		esac
	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

# Copy the files to their location.
umask 022
mkdir -p /opt/scc-srv
cp -pR software/opt/scc-srv/* /opt/scc-srv
mkdir -p /var/opt/scc-srv
cp -pR software/var/opt/scc-srv/* /var/opt/scc-srv

./install/postinstall

exit 0
