#!/bin/sh

# Generate Debian package to install scc by means of dpkg.
# 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.12 $

# 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
#
# It uses the following programs:
#
#	ar
#	gzip
#
# After using this program, the directory contains one file:
#
#	scc_1.9.28_all.deb
#

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

if [ ! -x /usr/bin/ar ]
then
	echo "${ProgName}: missing /usr/bin/ar (part of binutils), cannot package software" >&2
	exit 2
fi

scc_release="1.9.28"
version=${scc_release%.*}		# Ignore the patch-level
patch=${scc_release##*.}

if [ -x /usr/xpg4/bin/ar ]
then
	alias ar=/usr/xpg4/bin/ar
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

( cd software; tar cf ../data.tar . )
gzip data.tar

echo "2.0" >debian-binary

(
	cd software
	find . -type f		|
	sed -e "s@^\./@@"	|
	while read file
	do
		echo "$(my_md5 ${file})  ${file}"
	done
) >md5sums

{
	echo "/etc/opt/scc/newconfig/scc-localize"
	echo "/etc/opt/scc/newconfig/scc-split.conf"
	echo "/etc/opt/scc/newconfig/style.css"
} >conffiles

cp install/preinstall preinst
cp install/postinstall postinst
cp install/postremove postrm

# Indicate the proper release in the file control.
sed	-e '/^#/d'					\
	-e '/^[ 	]*$/d'				\
	-e "s/^Version.*/Version: ${version}-${patch}/"	\
		<control				\
		>control.tmp
mv -f control.tmp control

rm -f control.tar control.tar.gz
tar cf control.tar conffiles md5sums control preinst postinst postrm
gzip control.tar
rm -f conffiles md5sums control preinst postinst postrm

pkg_file=scc_${version}-${patch}_all.deb
echo "!<arch>" >${pkg_file}			# Specific magic number.
ar -q ${pkg_file} debian-binary			# Add the files
ar -q ${pkg_file} control.tar.gz		# in this
ar -q ${pkg_file} data.tar.gz			# order!

rm -rf install software $(cat file_list) debian-binary control.tar.gz data.tar.gz

exit 0
