#!/bin/sh
#

# Build the Debian package for Sherpath Groupware
# Veersion: 1.0 (Xavier Roche)

if ! test -d "config/setup"; then
	cd ../..
fi
if ! test -d "config/setup"; then
	echo "could not find config/setup in current directory"
fi

rm -rf /tmp/Sherpath
cp -ax setup/utils/Sherpath.template /tmp/Sherpath
cp -ax . /tmp/Sherpath/var/www/Sherpath

# do not erase previous config file
mv /tmp/Sherpath/var/www/Sherpath/config/setup/general.config.php3 /tmp/Sherpath/var/www/Sherpath/config/setup/general.config.php3.orig
mv /tmp/Sherpath/var/www/Sherpath/config/setup/pregeneral.config.php3 /tmp/Sherpath/var/www/Sherpath/config/setup/pregeneral.config.php3.orig

VERSION="`grep -E \"^.?shx_version=\" index.php|sed -e 's/.*=\\\"\(.*\)\\\".*/\1/'|cut -f1-2 -d'.'`"
VSIZE=`du -ks /tmp/Sherpath | sed -e 's/\([0-9]*\).*/\1/'`

cat /tmp/Sherpath/DEBIAN/control | sed -e "s/__VERSIONNUM__/$VERSION/g" -e "s/__INSTSIZE__/$VSIZE/g" > /tmp/Sherpath/DEBIAN/control.new
mv /tmp/Sherpath/DEBIAN/control.new /tmp/Sherpath/DEBIAN/control

# Perms
chmod -R 0755 /tmp/Sherpath

dpkg-deb --build "/tmp/Sherpath"

NAME="sherpath_${VERSION}-1_i386.deb"
rm -f "/tmp/${NAME}.deb"
mv -f "/tmp/Sherpath.deb" "/tmp/${NAME}"

echo "package successfuly built! (/tmp/${NAME})"
