#!/bin/sh

# Production of scc for OpenBSD.
# 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.22 $

# Requires GNU tar, which is called by this script as gnutar.

# 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 file:
#
#	scc-1.9.28-OpenBSD.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

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

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

mv software/opt/scc/* .
mv software/etc/opt/scc/* .
cp install/BSDinstall +DEINSTALL
mv install/BSDinstall +INSTALL
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="+CONTENTS +COMMENT +DESC +INSTALL +DEINSTALL"

cat >${LIST} <<-_X_
	@name scc-1.9.28-OpenBSD
	@arch *
	@cwd 
	@cwd opt/scc
	@owner root
	@group wheel
_X_

{
	find bin -type f;
	find doc -type f;
	find man -type f
} >pack_list

for f in $(cat pack_list)
do
	echo $f
	echo "@md5 $(my_md5 ${f})"
	echo "@size $(wc -c <${f} | sed -e 's/^[ 	]*//')" 
done >>${LIST}

{
	find newconfig -type f
} >config_list

cat >>${LIST} <<-_X_
	@dir man/man5/
	@dir man/man4/
	@dir man/man1/
	@dir man/
	@dir bin/
	@dir doc/
	@cwd .
	@dir opt/scc/bin/scc_modules/
	@dir opt/scc/bin/
	@dir opt/scc/
	@cwd etc/opt/scc
_X_

for f in $(cat config_list)
do
	echo $f
	echo "@md5 $(my_md5 ${f})"
	echo "@size $(wc -c <${f} | sed -e 's/^[ 	]*//')" 
done >>${LIST}
cat config_list >>pack_list
rm -f config_list

# Do not use the dir directive for /etc/opt/scc/conf as upgrade is performed by a delete followed by an install
# and we do not want to remove our configuration file during an upgrade.
cat >>${LIST} <<-_X_
	@dir newconfig/
	@cwd .
	+COMMENT
	@md5 $(my_md5 ./+COMMENT)
	+DESC
	@md5 $(my_md5 ./+DESC)
	+INSTALL
	@md5 $(my_md5 ./+INSTALL)
	+DEINSTALL
	@md5 $(my_md5 ./+DEINSTALL)
_X_

# We use the GNU version of tar because of the --format=ustar.
# On our production system we installed it and renamed the executable to gnutar.
gnutar --format=ustar -z -cf scc-1.9.28-OpenBSD.tgz ${PKG_FILES} $(cat pack_list)

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

exit 0
