#!/bin/sh

# Generate pacman package to install scc by means of pacman.
# Copyright (C) 2007-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.10 $

# 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-1.pkg.tar.gz
#

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

# Calling by non-root users results in wrong permissions of the installed files.
uid=$(id | sed -e 's/(.*//' -e 's/.*=//')
if [ "${uid}" != "0" ]
then
	echo "${ProgName}: only root should run this program" >&2
	exit 2
fi

# Determine what files are present in order to cleanup later.
for f in *
do
	if [ -f "${f}" ]
	then
		echo "${f}"
	fi
done >file_list

# The pacman package consists of a tar.gz file with the software hierarchy and
# some additional file. Create these files in the software directory and create the package.

# Create the .PKGINFO file
cat <<-_X_ >software/.PKGINFO
	# Generated by ${ProgName} at $(date)
	pkgname = scc
	pkgver = 1.9.28-1
	pkgdesc = System Configuration Collector
	url = http://scc.qnh-infrastructure.nl/index.html
	license = ("GPL")
	builddate = $(date)
	packager = $(who am i)
_X_

# Create the .FILELIST file
find software				|
sed	-e '/\.PKGINFO/d'	\
	-e '/\.FILELIST/d'	\
	-e 's@^software/@@'		|
sort >software/.FILELIST

# Move the install file to .INSTALL
mv install/pacman-install software/.INSTALL
cd software
tar cf ../scc-1.9.28-1.pkg.tar .PKGINFO .FILELIST .INSTALL etc opt var
cd ..
gzip scc-1.9.28-1.pkg.tar

rm -rf install software $(cat file_list)

exit 0
