#!/bin/sh

# This file is part of the Poliqarp suite.
# 
# Copyright (C) 2010 by Instytut Podstaw Informatyki Polskiej
# Akademii Nauk (IPI PAN; Institute of Computer Science, Polish
# Academy of Sciences; cf. www.ipipan.waw.pl).  All rights reserved.
# 
# This file may be distributed and/or modified under the terms of the
# GNU General Public License version 2 as published by the Free Software
# Foundation and appearing in the file gpl.txt included in the packaging
# of this file.  (See http://www.gnu.org/licenses/translations.html for
# unofficial translations.)
# 
# A commercial license is available from IPI PAN (contact
# Michal.Ciesiolka@ipipan.waw.pl or ipi@ipipan.waw.pl for more
# information).  Licensees holding a valid commercial license from IPI
# PAN may use this file in accordance with that license.
# 
# This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
# THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE.

set -e -x

# You may want to customize these:
host=i686-w64-mingw32
doc2html=http://docbook.sourceforge.net/release/xsl/current/html/docbook.xsl
root=$(mktemp -d -t poliqarp-win32.XXXXXX)
# Note that $buildroot contents will be removed!

version=$(sed -e '/.*(\([0-9.]\+\)).*/ { s//\1/g; q}' < doc/changelog)

pwd=$(pwd)

src="$root/poliqarp-$version/"
dist="$src/devel/win32-installer/poliqarp/"
mkdir -p "$src/" "$dist/"

cd "$root/"

# Download and unpack 3rd party libraries and (L)GPL licenses:

cat <<EOF | wget -c -i -
http://xmlsoft.org/sources/win32/libxml2-2.7.6.win32.zip
http://www.jgoodies.com/download/libraries/forms/forms-1_3_0.zip
http://www.jgoodies.com/download/libraries/looks/looks-2_3_1.zip
http://ftp.gnome.org/pub/gnome/binaries/win32/dependencies/win-iconv-dev_tml-20090213_win32.zip
http://ftp.gnome.org/pub/gnome/binaries/win32/dependencies/win-iconv-dll_tml-20090213_win32.zip
http://ftp.gnome.org/pub/gnome/binaries/win32/dependencies/zlib_1.2.4-2_win32.zip
http://www.gnu.org/licenses/gpl-2.0.dbk
http://www.gnu.org/licenses/lgpl-2.1.dbk
EOF
sha256sum -c <<EOF
de6b53d3311f7ac987fdf56225bc64e68028f52c6d4c2b2875907120ac45dff8  libxml2-2.7.6.win32.zip
f707a074264c43e37dce7c42df24e433d089fab828a673bf264082397d1f5329  forms-1_3_0.zip
f66b7e79ca53e849d6082ba0d937f9056d855cd6269edb9b3c52dd1347b14b8d  looks-2_3_1.zip
074e712137b257d0bae082158427a07944f3603090ecc6e42d95e8a2ea8ceb36  win-iconv-dev_tml-20090213_win32.zip
093101df265fdb802ffd7bfc77e997f5e0951c9df9b06b98a0f843081c2c2574  win-iconv-dll_tml-20090213_win32.zip
97fe3bfe484874d73ec0c7c35594fba93cd46661dbeb4181d88bb580e7fa7628  zlib_1.2.4-2_win32.zip
EOF
find -name '*.zip' -exec unzip '{}' ';'
ln -s -f libxml2-*.win32/ libxml2 
ln -s -f looks-*/looks-*.jar looks.jar
ln -s -f forms-*/forms-*.jar forms.jar

# Craft a fake xml2-config script:

cd "$root/libxml2/bin/"
:> xml2-config
>> xml2-config echo 'self=$(readlink -f "$0")'
>> xml2-config echo 'version="${self%.win32/*}"'
>> xml2-config echo 'version="${version##*-}"'
>> xml2-config echo 'case "$1" in'
>> xml2-config echo '  --version) echo "$version" ;;'
>> xml2-config echo '  --cflags) echo "-I${self%/*}/../include/" ;;'
>> xml2-config echo '  --libs) echo "-lxml2 -L${self%/*}/../bin/" ;;'
>> xml2-config echo 'esac'
chmod +x xml2-config

# Build Poliqarp from sources:

cd "$src"
cp -av "$pwd"/* .
export CPATH="$root/libxml2/include/:$root/include/"
./configure \
  --disable-xmltest --with-xml-exec-prefix="$root"/libxml2/ \
  --host="$host"
make clean
make

# Install binaries:

rm -Rf "$dist"
mkdir -p "$dist" "$dist/doc/"

cp -t "$dist/" "$src/win32/pthread/pthread.dll"
cp -t "$dist/" "$root/bin/iconv.dll"
cp -t "$dist/" "$root/bin/zlib1.dll"
cp -t "$dist/" "$root/libxml2/bin/libxml2.dll"
cp -t "$dist/" "$root/looks.jar" "$root/forms.jar"
cd "$src/bin/"
cp -t "$dist/" bp.exe bpng.exe bpindexer.exe bpupgrade.exe poliqarp.exe poliqarpc.exe poliqarpd.exe
"$host-strip" "$dist"/*.exe
cd "$src/gui/"
cp -t "$dist/" help.jar poliqarp.jar

# Build and install the documentation:

cd "$src/doc/"
for file in COPYING.* README TODO NEWS changelog
do
    if [ "$file" = "COPYING.GPL" ]
    then
        xsltproc --nonet "$doc2html" "$root/gpl-2.0.dbk" > "$dist/doc/${file%.xml}.html"
    elif [ "$file" = "COPYING.LIB" ]
    then
        xsltproc --nonet "$doc2html" "$root/lgpl-2.1.dbk" > "$dist/doc/${file%.xml}.html"
    elif [ "$file" = "changelog" ]
    then
        sed -r -e 's/^(  [*])/\n\1/' -e 's/^(    )[+]/\n\1-/' doc/changelog \
        | cat -s \
        | rst2html > "$dist/doc/${file}.html"
    else
        rst2html "$file" > "$dist/doc/${file}.html"
    fi
done
cp -L gpl.txt "$dist/doc/"
cd "$src/doc/manual-pages"
for file in *.xml
do
   xsltproc --nonet "$doc2html" "$file" > "$dist/doc/${file%.xml}.html"
done

# Build the installer:
cd "$dist"/..
makensis poliqarp-installer.nsi
mv -T Setup.exe "$pwd/poliqarp-$version-setup.exe"

# Cleanup:
rm -Rf "$root"

# vim:ts=4 sw=4 et
