#!/bin/sh

#
# bootstrap
#
# Copyright (C) 2008, 2009 Francesco Salvestrini
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#

ME=bootstrap

#
# Internal functions
#
missing ()
{
    PROGRAM=$1

    if test x"$PROGRAM" = x""; then
	return 1
    fi
    IFS=":"

    for i in $PATH
    do

	if test -f $i/$PROGRAM; then
	    echo $i/$PROGRAM
	    return 0
	fi
    done

    return 1
}

#
# External tools
#
AUTOHEADER=`missing autoheader`
ACLOCAL=`missing aclocal`
AUTOCONF=`missing autoconf`
AUTOMAKE=`missing automake`
AUTORECONF=`missing autoreconf`
LIBTOOL=`( missing glibtool || missing libtool )`
LIBTOOLIZE=`( missing glibtoolize || missing libtoolize)`
GNULIBTOOL=`missing gnulib-tool`
GIT=`missing git`
SED=`missing sed`

#
# Dump some useful informations
#
echo "$ME: Dumping build-tools information ..."
for tool in	\
    $SED        \
    $GIT        \
    $AUTOHEADER	\
    $AUTOCONF	\
    $LIBTOOL	\
    $LIBTOOLIZE	\
    $ACLOCAL	\
    $AUTOMAKE	\
    $AUTORECONF \
    $GNULIBTOOL \
    ;
do
    if test -n "$tool" ; then
	TOOLVER=`$tool --version | head -1`
	echo "$ME:  $tool -> $TOOLVER"
    fi
done

#
# Prevent running bootstrap on a non-repository copy
#
$GIT log > /dev/null 2>&1 || {
    echo "$ME: I must run on a git clone ..."
    exit 1
}
if test x"`$GIT tag -l 2>/dev/null | $SED -n '$='`" = x""; then
    echo "$ME: I require a repository tag in order to run ..."
    exit 1
fi

#
# Remove files left from the previous run
#
echo "$ME: Removing autotools related files and directories ..."
rm -f  `find ./ -name "Makefile.in"`                 || exit 1
rm -f  config.cache aclocal.m4 config.h.in configure || exit 1
rm -f  config.guess config.sub ltmain.sh             || exit 1
rm -rf autom4te.cache                                || exit 1
rm -rf `find ./ -name ".deps"`                       || exit 1

#
# configure.ac
#
echo "$ME: Building configure.ac from configure.ac.in"
rm -f configure.ac
VERSION=`git tag -l | sort -s -t '.' -k 1,1n -k 2,2n -k 3,3n | tail -n 1`
cat configure.ac.in | sed -e 's,[@]VERSION[@],'$VERSION',' > configure.ac || \
    { rm -f configure.ac ; exit 1 ; }
chmod a-w configure.ac

#
# src/config/Makefile.cfg
#
echo "$ME: Building src/config/Makefile.cfg ..."
OUTPUT_CFG=src/config/Makefile.cfg
INPUT_CFGS=`find ./ -name "rules.cfg"`

for i in $INPUT_CFGS ; do
    CURRENT=`echo $i | sed -e 's,./,$(top_srcdir)/,'`
    FILES="$FILES \\
$CURRENT"
done

rm -f $OUTPUT_CFG || exit 1
touch $OUTPUT_CFG || exit 1

cat > $OUTPUT_CFG <<EOF
##
## Include file for automake
##
## This file has been generated automatically, DO NOT EDIT!
##

CFG_FILES = $FILES

EOF

#
# src/*/Makefile.am generation from src/*/Makefile.am.in
#
for i in `find ./src -name "Makefile.inc.in"` ; do
    srcfile=$i
    dstfile=`dirname $i`/`basename $i .in`
    relativepath=`echo $i | sed -e 's,\.\/src/,,' | sed -e 's,/Makefile\.inc\.in,,'`

    #echo "$ME: Generating $dstfile from $srcfile ($relativepath)"
    #echo "$ME: Generating $dstfile ($relativepath)"
    echo "$ME: Generating $relativepath"

    #cat $srcfile | sed \
    #	-e "s,@subdir_srcdir@,$\(top_srcdir\)\/src/$relativepath,g" \
    #	-e "s,@subdir_builddir@,$\(top_builddir\)\/src/$relativepath,g" \
    #	> $dstfile
    #cat $srcfile | sed \
    #	-e "s,@subdir_srcdir@,$relativepath,g" \
    #	-e "s,@subdir_builddir@,$relativepath,g" \
    #	-e "s,@reldir@,$relativepath,g" \
    #	-e "s,@reldir@,$relativepath,g" \
    #	> $dstfile
    cat $srcfile | sed \
	-e "s,@reldir@,$relativepath,g" \
	> $dstfile || {
	echo "$ME: Cannot rearrange $srcfile into $dstfile"
	exit 1
    }
done

#
# autoreconf
#
echo "$ME: Handling autotools bootstrap ..."

echo "$ME: Running autoreconf ..."
$ACLOCAL    --force --install -I ./tools/autotools/m4 && \
    $AUTORECONF --verbose --force --install -Wall         || {
    exit 1
}

#
# bootstrap other packages
#
(cd tools/choose && ./bootstrap) || {
    echo "$ME: Cannot bootstrap tools/choose directory"
    exit 1
}

exit 0
