#!/bin/sh

if [ ! -f /etc/debian_version ]; then
	echo "Debian (clone) required."
	exit
fi

# Check for CMake
#
cmake --version 2> /dev/null
if [ $? != 0 ]; then
	echo "CMake is not installed but required for building Hiawatha."
	exit
fi

# Checking for packages required for building a Debian package
#
echo "-- Checking for required packages"
packages="make gcc libc6-dev dpkg-dev debhelper fakeroot libxml2-dev libxslt1-dev zlib1g-dev"
missing=""
for package in ${packages}; do
	installed=`dpkg -l ${package} | tail -1 | grep "^ii" | wc -l`
	if [ "$installed" = "0" ]; then
		missing="${missing} ${package}"
	fi
done
if [ "${missing}" != "" ]; then
	echo "The following packages are missing:${missing}"
	exit
fi

# Setup build directory
#
cd `dirname $0`/..
if [ -d build_debian_package ]; then
	rm -rf build_debian_package
fi
mkdir build_debian_package
cd build_debian_package
cp -r ../extra/debian .

# Compile Hiawatha
#
cmake .. -DCMAKE_INSTALL_PREFIX="" -DCMAKE_INSTALL_LIBDIR=usr/lib \
         -DCMAKE_INSTALL_BINDIR=usr/bin -DCMAKE_INSTALL_SBINDIR=usr/sbin \
         -DCMAKE_INSTALL_SYSCONFDIR=etc -DCMAKE_INSTALL_MANDIR=usr/share/man \
         -DENABLE_TOMAHAWK=ON -DENABLE_MONITOR=ON
make

# Make Debian package
#
echo "-- Building package"
dh_testdir
fakeroot dh_testroot
if [ -f /usr/bin/dh_prep ]; then
	dh_prep
else
	dh_clean -k
fi
dh_installdirs
make install DESTDIR=`pwd`/debian/hiawatha
gzip -9 debian/hiawatha/usr/share/man/man1/*
sed "s/#ServerId/ServerId/" config/hiawatha.conf > debian/hiawatha/etc/hiawatha/hiawatha.conf
cp -r logrotate.d debian/hiawatha/etc
fakeroot dh_installdocs
cp ../ChangeLog debian/changelog
fakeroot dh_installchangelogs
gzip -9 debian/hiawatha/usr/share/doc/hiawatha/changelog*
tar -czf debian/hiawatha/usr/share/doc/hiawatha/letsencrypt.tar.gz -C ../extra letsencrypt
dh_installinit
fakeroot dh_installdeb
dh_shlibdeps -l/usr/lib/hiawatha
fakeroot dh_gencontrol
fakeroot dh_md5sums
fakeroot dh_builddeb

# Done
#
cd ..
if [ "$1" != "-b" ]; then
	rm -rf build_debian_package
fi
