#!/bin/sh

# Generate FreeBSD package to install scc-srv by means of pkg_add.
# 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.19 $

# 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 of the files:
#
#	scc-srv-1.7.5.FreeBSD.tgz
#	scc-srv-1.7.5.NetBSD.tgz
#

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

my_md5()
{
	exe="$(which md5sum 2>/dev/null)"
	if [ -x "${exe}" ]
	then
		"${exe}" "${1}" | awk '{ print $1 }'
	else
		exe="$(which md5 2>/dev/null)"
		if [ -x "${exe}" ]
		then
			"${exe}" "${1}" | awk '{ print $4 }'
		fi
	fi
}

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

label="srcdir"
suffix="FreeBSD"
PKG=scc-srv-1.7.5.${suffix}

mv software/opt/scc-srv/* .
cp install/BSDinstall +INSTALL
mv install/BSDinstall +DEINSTALL
rm -rf install

mv software/var/opt/scc-srv/* .
# 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 files in a seperate directory and the postinstal copies the files
# to the website.
mv data/www/scc-help data/www/scc-help-new
chmod 700 data/www/scc-help-new
chmod 700 data/www/scc-help-new/scc
chmod 400 data/www/scc-help-new/scc/*
chmod 700 data/www/scc-help-new/scc-srv
chmod 400 data/www/scc-help-new/scc-srv/*
chmod 700 data/www/scc-help-new/scc-win
chmod 400 data/www/scc-help-new/scc-win/*

echo "System Configuration Collector Server 1.7.5" >+COMMENT
{
	echo "scc server offers a web-interface for the scc data that is sent to the server."
	echo ""
	echo "Homepage:"
	echo "http://scc.qnh-infrastructure.nl/index.html"
} >+DESC

LIST="+CONTENTS"
PKG_FILES="+DEINSTALL +INSTALL +DESC +CONTENTS +COMMENT"

cat >${LIST} <<-_X_
	@name scc-srv-1.7.5
	@cwd /opt/scc-srv
	@${label} $(pwd)
	@owner root
	@group wheel
_X_

for f in $(find bin -type f; find doc -type f; find man -type f)
do
	echo $f
	echo "@comment MD5:$(my_md5 ${f})"
done >>${LIST}

cat >>${LIST} <<-_X_
	@dirrm man/man5
	@dirrm man/man4
	@dirrm man/man1
	@dirrm man
	@dirrm bin
	@dirrm doc
	@cwd .
	@dirrm /opt/scc-srv
	@cwd /var/opt/scc-srv
_X_

{
	find conf -type f;
	find data -type f;
	find newconfig -type f;
	find tmp -type f;
} >data_list

for f in $(cat data_list)
do
	echo $f
	echo "@comment MD5:$(my_md5 ${f})"
done >>${LIST}
cat data_list >>pack_list
rm -f data_list

cat >>${LIST} <<-_X_
	@cwd .
	@ignore
	+COMMENT
	@comment MD5:$(my_md5 ./+COMMENT)
	@ignore
	+DESC
	@comment MD5:$(my_md5 ./+DESC)
	@ignore
	+INSTALL
	@comment MD5:$(my_md5 ./+INSTALL)
	@ignore
	+DEINSTALL
	@comment MD5:$(my_md5 ./+DEINSTALL)
_X_

tar cf ${PKG} ${PKG_FILES}
tar uf ${PKG} bin doc man conf data newconfig tmp
gzip -S .tgz ${PKG}

rm -rf bin doc man conf data newconfig tmp software $(cat file_list) +*

exit 0
