#!/bin/sh
#This script is used to re-create the Makefile.am using ccbuild
# and then run allt the necessary autotools. Configure.in should
# be edited separately.
#
#  This file is part of ccbuild.

#  ccbuild 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 3 of the License, or
#  (at your option) any later version.

#  ccbuild 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 ccbuild.  If not, see <http://www.gnu.org/licenses/>.

if [ "x$1" = "xclean" ]; then
	echo Cleaning
	make distclean
  rm -f configure Makefile.in depcomp missing Makefile.am aclocal.m4 install-sh
  rm -rf autom4te.cache
	exit
fi
## Generate Makefile.am from ccbuild
PSOURCE=src/ccbuild.cc
PNAME=`basename "${PSOURCE}" .cc`
echo "Generating Makefile.am from ccbuild"
which ccbuild && (ccbuild md5 --recursive-include . "${PSOURCE}" > MD5SUMS)
SOURCES=`sed  -r 's/^[a-z0-9]+  //; s/ /\\ /' < MD5SUMS | tr '\n' ' '`

#Overwrite Makefile.am
cat > Makefile.am <<EOF
AUTOMAKE_OPTIONS = subdir-objects

bin_PROGRAMS = ${PNAME}

#Sources, generated list from ccbuild md5 output
${PNAME}_SOURCES = ${SOURCES}	

${PNAME}_CXXFLAGS = \${DEPS_CFLAGS} \${BOOST_CPPFLAGS} -DNODEBUG -fopenmp
${PNAME}_LDADD = \${DEPS_LIBS} \${BOOST_LDFLAGS} -lbobcat -lgomp -lgnutls-openssl
man_MANS = doc/${PNAME}/${PNAME}.1

#Flex it!
src/SourceScanner/yylex.cc: src/SourceScanner/lexer
	\${LEX} \$<
	mv yylex.cc \$@

EOF

echo "Running autotools:"
##BOOTSTRAP
echo " aclocal"
aclocal #Sometimes nice to have: -I /usr/share/autoconf-archive
echo " autoconf"
autoconf
echo " automake"
automake --add-missing --copy


