#!/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

pwd="$PWD"
buildroot=$(mktemp -dt poliqarp-source.XXXXXX)
export TAR_OPTIONS='--owner root --group root --mode a+rX'
export GZIP='-9 -n'

cd "$buildroot"

# Mercurial archive:
hg archive "$@" -R "$pwd" poliqarp
rm -f poliqarp/.hg*
version=$(sed -e '/.*(\([0-9.]\+\)).*/ { s//\1/g; q; }' < poliqarp/doc/changelog)
mv poliqarp "poliqarp-$version"

# Autoconf & company:
cd "poliqarp-$version"
./devel/autogen
rm -rf *.cache
./devel/generate-makefile.dep
./configure --disable-bp --disable-bpindexer --disable-bpupgrade --disable-poliqarpd --disable-poliqarpc
rm config.log config.status
cd ..

# Build documentation:
make -C "poliqarp-$version/doc/manual-pages/"
make -C "poliqarp-$version/gui" help.jar

# Clean autogenerated stuff:
find "poliqarp-$version/" -name '*.in' -print0 | sed -e 's/.in\(\x00\)/\1/g' | xargs -0 rm
make -C "poliqarp-$version/doc/cheat-sheet/" clean

# Create tarball:
tar -czf "$pwd/poliqarp_$version.tar.gz" "poliqarp-$version"

# Cleanup:
rm -Rf "$buildroot"
