#!/bin/ksh

# Generate Solaris package to install scc-srv by means of pkgadd.
# 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.22 $

# This program requires to be called in a directory with the following hierarchy:
#
#	install				pre/post install scripts
#	software/opt/scc-srv		bin, doc and man
#	software/var/opt/scc-srv	conf, data, newconfig and tmp
#
# After using this program, the directory contains one file:
#
#	scc-srv-1.7.5.sunos.pkg
#

TESTING="testing"
x="$(echo "${TESTING}" 2>/dev/null)"
if [ "${x}" != "${TESTING}" ]
then
	exec /bin/ksh ${0} "$@"

	echo "${0}: cannot find ksh" >&2
	exit 2
fi

ProgName=${0##*/};			export ProgName

exe="$(which pkgproto 2>/dev/null)"
if [ ! -x "${exe}" ]
then
	echo "${ProgName}: cannot find pkgproto" >&2
	exit 2
fi

for f in *
do
	if [ -f "${f}" ]
	then
		echo "${f}"
	fi
done >file_list

PKG=scc-srv-1.7.5.sunos.pkg;	export PKG

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

# The shell /bin/sh on Solaris systems does not provide the functionality we need.
# Replace it with /bin/ksh
find install software -type f            |
while read f
do
	hdr=$(head -n 1 ${f})
	if [ "${hdr}" = "#!/bin/sh" ]
	then
		sed -e 's@^#!/bin/sh@#!/bin/ksh@' <${f} >scc.tmp
		>${f}
		cat scc.tmp >>${f}
		rm -f scc.tmp
	fi
done

mkdir spool

(
	cat <<-_X_
		i pkginfo
		i postinstall=install/postinstall
		i postremove=install/postremove
	_X_

	# Do not alter permissions for default directories like /opt, /var and /var/opt.
	# Ignore /opt/scc-srv and /var/opt/scc-srv to allow symbolic links for these directories.
	# Do not bother in case of "relocation"
	pkgproto software/opt/scc-srv=/opt/scc-srv software/var/opt/scc-srv=/var/opt/scc-srv		|
	sed	-e '/\/opt /d'			\
		-e '/\/var /d'			\
		-e '/\/scc-srv /d'		\
		-e 's/ 0600 / 0500 /g'		\
		-e 's/root other/root sys/g'
) >sunos-scc-srv.pkg

pkgmk	-o				\
	-d ./spool			\
	-f sunos-scc-srv.pkg		\
	-p sunos58`date '+%Y%m%d%H%M'`	\
	-v 1.7.5

pkgtrans -n ./spool ${PKG} scc-srv

mv spool/${PKG} .
chmod 444 ${PKG}

rm -rf install software spool sunos-scc-srv.pkg $(cat file_list)

exit 0
