#!/bin/sh

# Generate PISI package to install scc by means of pisi.
# Copyright (C) 2008-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.5 $

# 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-<release>.pisi
#

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 lzma 2>/dev/null)"
if [ ! -x "${exe}" ]
then
	echo "${Progname}: cannot find lzma" >&2
	exit 2
fi

# 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

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

rm -f install.tar
rm -f install.tar.lzma

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

# Create the files.xml and the tar-file:
(
	cd software

	echo "<Files>"

	while read type dir
	do
		for file in ${dir}/*
		do
			if [ -f "${file}" ]
			then
				size=$(ls -l ${file} | awk '{ print $5 }')
				echo "    <File>"
				echo "        <Path>${file}</Path>"
				echo "        <Type>${type}</Type>"
				echo "        <Size>${size}</Size>"
				echo "        <Hash>$(sha1sum ${file} | awk '{ print $1 }')</Hash>"
				echo "    </File>"

				if [ ! -f ../install.tar ]
				then
					tar -c -f ../install.tar ${file}
				else
					tar -r -f ../install.tar ${file}
				fi
			fi
		done
	done	<<-_X_
		executable	opt/scc/bin
		executable	opt/scc/bin/scc_modules
	        man		opt/scc/doc
		man		opt/scc/man/man1
		man		opt/scc/man/man4
		man		opt/scc/man/man5
		config		etc/opt/scc/newconfig
	_X_

	echo "</Files>"
) >files.xml

# We encountered two variants of lzma, one requires the 'e' command, the other the --compress option.
cp install.tar install.tar.org
lzma e install.tar install.tar.lzma 2>/dev/null
if [ $? -ne 0 ]
then
    rm -f install.tar.lzma
    mv install.tar.org install.tar
    lzma --compress install.tar
else
    rm -f install.tar.org
fi

# Now create the metadata.xml
total_size=$(sed -n -e 's@</Size>@@' -e 's/^[ 	]*<Size>//p' files.xml | awk '{ t+=$1 } END { print t }')

cat >metadata.xml <<_X_
<PISI>
    <Source>
        <Name>scc</Name>
        <Homepage>http://scc.qnh-infrastructure.nl/index.html</Homepage>
        <Packager>
            <Name>Siem Korteweg</Name>
            <Email>scc_feedback@qnh.nl</Email>
        </Packager>
    </Source>
    <Package>
        <Name>scc</Name>
        <Summary xml:lang="en">System Configuration Collector</Summary>
        <Description xml:lang="en">An application to collect configuration data and to detect changes.</Description>
        <IsA>app:console</IsA>
        <License>GPL-2</License>
        <Files>
            <Path fileType="executable">/opt/scc/bin</Path>
            <Path fileType="executable">/opt/scc/bin/scc_modules</Path>
            <Path fileType="man">/opt/scc/doc</Path>
            <Path fileType="man">/opt/scc/man</Path>
            <Path fileType="man">/opt/scc/man/man1</Path>
            <Path fileType="man">/opt/scc/man/man4</Path>
            <Path fileType="man">/opt/scc/man/man5</Path>
            <Path fileType="data">/var/opt/scc/data</Path>
            <Path fileType="data">/var/opt/scc/tmp</Path>
            <Path fileType="config">/etc/opt/scc/newconfig</Path>
            <Path fileType="config">/etc/opt/scc/conf</Path>
        </Files>
        <Provides>
            <COMAR script="scc.py">System.Package</COMAR>
        </Provides>
        <History>
            <Update release="${minor}">
                <Date>$(date '+%Y-%m-%d')</Date>
                <Version>${major}</Version>
                <Name>Siem Korteweg</Name>
                <Email>scc_feedback@qnh.nl</Email>
                <Comment>Refer to website.</Comment>
            </Update>
        </History>
        <Build>${patch}</Build>
        <Distribution>Pardus</Distribution>
        <DistributionRelease>Any</DistributionRelease>
        <Architecture>Any</Architecture>
        <InstalledSize>${total_size}</InstalledSize>
        <Source>
            <Name>scc</Name>
            <Homepage>http://scc.qnh-infrastructure.nl/index.html</Homepage>
            <Packager>
                <Name>Siem Korteweg</Name>
                <Email>scc_feedback@qnh.nl</Email>
            </Packager>
        </Source>
    </Package>
</PISI>
_X_

mkdir -p comar
cat >comar/scc.py <<_X_
#!/usr/bin/python
# -*- coding: utf-8 -*-

import os

def preRemove():
    os.system( '/opt/scc/bin/scc-log' )
    return True

def postInstall(fromVersion, fromRelease, toVersion, toRelease):
    os.system( '/opt/scc/bin/scc-log -r -c "installing version ${scc_release}"' )
    return True
_X_

# Now we have the files.xml, metadata.xml and install.tar.lzma
zip -q -X scc-${major}.${minor}-${patch}.pisi comar/scc.py metadata.xml files.xml install.tar.lzma

rm -f $(cat file_list)
rm -rf software install metadata.xml files.xml comar install.tar install.tar.lzma

exit 0
