#!/bin/sh

install_dir="C:\\\\Program Files\\\\Hiawatha"

if [ `uname -o` != "Cygwin" ]; then
	echo "Cygwin 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 tools required for building a Windows package
#
echo "-- Checking for required tools"
tools="/usr/bin/cmake /usr/bin/make /usr/bin/cat /usr/bin/gcc /usr/bin/man /usr/bin/ps2pdf /usr/bin/unix2dos /usr/bin/zip /usr/bin/cygrunsrv"
missing=""
for tool in ${tools}; do
	if [ ! -f ${tool} ]; then
		missing="${missing} ${tool}"
	fi
done
if [ "${missing}" != "" ]; then
	echo "The following tools are missing:${missing}"
	exit
fi

# Setup build directory
#
cd `dirname $0`/..
if [ -d build_windows_package ]; then
	rm -rf build_windows_package
fi
mkdir build_windows_package
cd build_windows_package

# Compile Hiawatha
#
install_dir_cyg=`cygpath -p "${install_dir}"`
cmake .. -DCMAKE_INSTALL_SBINDIR="${install_dir_cyg}/bin" \
         -DCONFIG_DIR="${install_dir_cyg}/config" \
         -DLOG_DIR="${install_dir_cyg}/log" \
         -DPID_DIR="${install_dir_cyg}/log" \
         -DWORK_DIR="${install_dir_cyg}/work" \
         -DWEBROOT_DIR="C:\wwwroot" -DCMAKE_LEGACY_CYGWIN_WIN32=0
make

# Make Windows package
#
echo "-- Building package"
version=`grep VERSION config.h | cut -f2 -d'"'`
dir="hiawatha-${version}"

mkdir -p ${dir}/Hiawatha
mkdir ${dir}/Hiawatha/bin
mkdir ${dir}/Hiawatha/config
mkdir ${dir}/Hiawatha/log
mkdir ${dir}/Hiawatha/work
mkdir ${dir}/wwwroot

cp hiawatha.exe ${dir}/Hiawatha/bin
cp ssi-cgi.exe ${dir}/Hiawatha/bin
cp wigwam.exe ${dir}/Hiawatha/bin
cp mbedtls/library/cygmbedtls-*.dll ${dir}/Hiawatha/bin
strip ${dir}/Hiawatha/bin/*.exe

files="cygcrypt-0.dll cyggcc_s-1.dll cygrunsrv.exe cygiconv-2.dll cygwin1.dll cygxml2-2.dll cygxslt-1.dll cygz.dll cyglzma-5.dll"
for file in ${files}; do
	cp /bin/${file} ${dir}/Hiawatha/bin
done

cp ../config/index.xslt ${dir}/Hiawatha/config
cp ../config/error.xslt ${dir}/Hiawatha/config
cp ../config/mimetype.conf ${dir}/Hiawatha/config
cp ../extra/windows/*.lnk ${dir}/Hiawatha
sed "s/INSTALL_DIR/${install_dir}/" ../extra/windows/Hiawatha.bat > ${dir}/Hiawatha/bin/Hiawatha.bat
sed "s/INSTALL_DIR/${install_dir}/" ../extra/windows/TestConfig.bat > ${dir}/Hiawatha/bin/TestConfig.bat
sed "s/INSTALL_DIR/${install_dir}/" ../extra/windows/hiawatha.conf > ${dir}/Hiawatha/config/hiawatha.conf
cp ../extra/windows/Hiawatha.ico ${dir}/Hiawatha
cp ../extra/windows/Installation.txt ${dir}
cp ../extra/index.html ${dir}/wwwroot
cp ../ChangeLog ${dir}/ChangeLog.txt
unix2dos ${dir}/ChangeLog.txt
cp ../LICENSE ${dir}/License.txt
unix2dos ${dir}/License.txt

touch ${dir}/Hiawatha/log/access.log
touch ${dir}/Hiawatha/log/error.log
touch ${dir}/Hiawatha/log/exploit.log
touch ${dir}/Hiawatha/log/system.log

man -P cat -t man/hiawatha.1 | ps2pdf - > ${dir}/hiawatha.pdf
man -P cat -t ../man/ssi-cgi.1 | ps2pdf - > ${dir}/ssi-cgi.pdf
man -P cat -t ../man/wigwam.1 | ps2pdf - > ${dir}/wigwam.pdf

zip -r ../hiawatha-${version}.zip ${dir}

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