#!/bin/sh
#
# This bootstrap file has been inspired by Bootstraping chapter
# in Autobook by Vaughan, Elliston, Tromey and Taylor.
# http://sources.redhat.com/autobook/
#
# Usage: $0 [path to wxwin.m4]
#


set -x
set -e

# required by autoconf
hg log --style changelog > ChangeLog

# define version
if [ -z "$VER" ]; then
   H=$(hg tags | awk -F'[: ]+' '{if(FNR==1) print $2;}')
   V=$(hg tags | awk -F'[: ]+' '{if(FNR==2) print $1;}')
   VER="${V}+hg${H}"
fi

cat configure.ac | awk -v ver=$VER '{if(/^AC_INIT/) {print "AC_INIT([Munipack],[" ver "],[hroch@physics.muni.cz],[munipack],[http://munipack.physics.muni.cz])";} else print;}' > configure.new
mv configure.new configure.ac

# generate .spec
awk -v v=$VER '{ if( /^Version:/ ) print $1,v; else print; }' < munipack.spec.template > munipack.spec

# generate debs
#cat debian/changelog.template | awk  -v ver=$VER '{if(FNR==1) print "munipack (" ver ") wheezy; urgency=low"; else print; }' > debian/changelog
#mv debian/changelog.new debian/changelog
#cp debian/changelog.template debian/changelog
#DCH=`which debchange 2>/dev/null`
#if [ -x "$DCH" ]; then
#   debchange -b -p -v "${VER}-1" "New upstream release."
#   debchange -r "Release"
#fi


# generate thumbnails in doc/ and icons in xmunipack/, RSS optionaly
( cd doc && sh rss.sh )
( cd doc && sh genthumb.sh )
( cd xmunipack && sh image_list.sh )

# add -I for localy installed wxwin.m4 (or add its parent directory as
#                                      the first command-line argument)

if [ "$1" ]; then
   ACLOCAL_PATH="-I $1"
fi

#aclocal $ACLOCAL_PATH \
#&& autoheader \
#&& automake --gnu --add-missing --copy \
#&& autoconf

autoreconf $ACLOCAL_PATH -i

# ./bootstrap /usr/local/share/aclocal
# ./configure FCFLAGS="-Wall -g -p -fcheck=all -fimplicit-none -fbacktrace -ffpe-trap=invalid,zero,overflow" CFLAGS="-Wall -g -p"  CXXFLAGS="-D__WXDEBUG__ -g -p -Wall -fbounds-check" --prefix=/scratch/opt --enable-bundle BUNDLEDIR=/scratch/opt
