#!/bin/sh

# Generate FreeBSD package to install scc 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/index.html 

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

# This program requires to be called in a directory with the following hierarchy:
#
#	install			pre/post install scripts
#	software/opt/scc	bin, doc and man
#	software/var/opt/scc	conf, data, newconfig and tmp
#
# After using this program, the directory contains one of the files:
#
#	scc-1.9.28.FreeBSD.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-1.9.28.${suffix}

mv software/opt/scc/* .
mv software/etc/opt/scc/* .
cp install/BSDinstall +INSTALL
mv install/BSDinstall +DEINSTALL
rm -rf install
echo "System Configuration Collector 1.9.28" >+COMMENT
{
	echo "scc collects configuration data from Unix/Linux/*BSD systems."
	echo "Changes in the configuration are detected and added to a logbook."
	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-1.9.28
	@cwd /opt/scc
	@${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/scc_modules
	@dirrm bin
	@dirrm doc
	@cwd /etc/opt/scc
	@${label} $(pwd)
	@owner root
	@group wheel
_X_

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

cat >>${LIST} <<-_X_
	@dirrm newconfig
	@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 newconfig
gzip -S .tgz ${PKG}

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

exit 0
