#!/bin/sh
# rebuild
# (c) 2002 Karel 'Clock' Kulhavy, Mikulas Patocka, Petr 'Brain' Kulhavy
# This file is a part of the Links program, released under GPL.

make=make
if which gmake 2>/dev/null >/dev/null && gmake --version | grep -i "GNU Make" >/dev/null; then
	echo Found GNU Make under gmake. Going to use gmake instead of make.
	make=gmake
fi

N="`grep -c ^processor /proc/cpuinfo 2>/dev/null || true`"
if ! test "$N" = 0 -o "$N" = ""; then
	MAKEFLAGS=-j$N
	export MAKEFLAGS
fi

if which automake-1.4 2>/dev/null >/dev/null; then
	automake="automake-1.4"
else
	automake="automake"
fi

if which aclocal-1.4 2>/dev/null >/dev/null; then
	aclocal="aclocal-1.4"
	if [ -d /usr/share/aclocal-1.4/ ]; then
		aclocal="$aclocal --acdir=/usr/share/aclocal-1.4/"
	fi
else
	aclocal="aclocal"
fi

if which autoconf2.13 2>/dev/null >/dev/null; then
	autoconf="autoconf2.13"
elif which autoconf-2.13 2>/dev/null >/dev/null; then
	autoconf="autoconf-2.13"
else
	autoconf="autoconf"
fi

if which autoheader2.13 2>/dev/null >/dev/null; then
	autoheader="autoheader2.13"
elif which autoheader-2.13 2>/dev/null >/dev/null; then
	autoheader="autoheader-2.13"
else
	autoheader="autoheader"
fi

if [ "$1" = reconf ]; then
	if ! which pkg-config 2>/dev/null >/dev/null; then
		echo Warning: You don\'t have pkg-config. The PKG_CHECK_MODULES macro will probably fail. >&2
	fi

	if ! which $autoconf 2>/dev/null >/dev/null; then
		echo You don\'t have autoconf. Wiping out. >&2
		exit 1
	fi

	if ! $autoconf --version | grep -i "GNU Autoconf" >/dev/null; then
		echo This looks like autoconf installed on OpenBSD. >&2
		if ! AUTOCONF_VERSION=2.13 $autoconf --version 2>/dev/null >/dev/null
		then
			echo Autoconf 2.13 is not found. This script needs at most autoconf 2.13. \
			Trying with some other \(random\) autoconf. >&2
			exit 1
			path=`echo "$PATH" | tr -s ':' '\n'`
			alt=`find $path -name 'autoconf*' -and ! -name autoconf | head -n 1`
			if [ -z "$alt" ]
				then
				echo Cannot find the real autoconf >&2
				exit 1
			fi
			export AUTOCONF_VERSION=`echo $alt | sed -e 's/^.*autoconf-//g'`
			echo Found $alt. Setting AUTOCONF_VERSION to $AUTOCONF_VERSION.>&2
		else
			echo Found Autoconf 2.13. It should work. >&2
			export AUTOCONF_VERSION=2.13
		fi
	fi

	if ! $automake --version | grep -i "GNU automake" >/dev/null
	then
		echo This looks like automake installed on OpenBSD. >&2
		export AUTOMAKE_VERSION=1.4
		if ! $automake --version 2>/dev/null >/dev/null
		then
			echo Automake 1.4 is needed. Wiping out. >&2
			exit 1
		fi
		echo Found Automake 1.4. It should work. >&2
	fi
fi

do_reconf=false

rm -f config.h Makefile config.cache &&
if [ "$1" = reconf ]; then
	do_reconf=true
	shift
	echo NEPOUZIVAT pokud nebylo zmeneno configure.in nebo Makefile.am !!! &&
        echo DO NOT use unless configure.in or Makefile.am has been changed !!! &&
	echo rebuild reconf se NESMI prerusit protoze pak by pri nasledujicim commitu byly commitnuty spatne dependence a pri kompilaci by tise vznikaly zahadne neodladitelne chyby!!! &&
	echo rebuild reconf MUST NOT be interrupted because after the following commit bad dependencies would be commited and during compilation, mysterious and undebuggable bugs would originate!!! &&
	echo Timto mistoprisezne prohlasuji ze toto skutecne nastalo a ze proces neprerusim. &&
	echo I hereby affirm that this really has happened and I won\'t break the process. &&
	read kecy &&
	if [ "$kecy" = "ano" -o "$kecy" = "Ano" -o "$kecy" = "yes" -o "$kecy" = "Yes" ]; then
		echo Zodpovednost prijata. &&
		echo Responsibility accepted. &&
		rm -rf .deps missing depcomp aclocal.m4 &&
		$autoheader &&
		rm -f autoh* &&
		$aclocal -I . &&
		$automake --add-missing &&
		sed 's%sed '\''s/\\\.am\$/\.in/'\''%sed '\''s/\\\.[aA][mM]\$/\.in/'\''%' <missing >missing.tmp &&
		mv -f missing.tmp missing &&
		chmod a+x missing &&
		$autoconf &&
		ed configure <<EOS
1a
LANG=
LC_ALL=
.
/DELETE1/-1,/DELETE1/d
/DELETE2/-1,/DELETE2/d
w
q
EOS

	else
		exit
	fi
fi &&
if [ "$1" = reconf_aby_to_brain_nepouzival ]; then
	echo "Mikulasi, vyser si voko!" &&
	exit
fi &&
#cd Unicode;./gen; cd ..
#cd intl; ./synclang; cd ..
export CFLAGS="-Wall -g -O2 -pipe -fno-common $CFLAGS" &&
#export CFLAGS="-Wall -g -O2 -pipe -ansi -U__STRICT_ANSI__"
export CXXFLAGS="$CXXFLAGS -Wall -g -O2 -pipe" &&
./configure --enable-graphics --enable-debuglevel=2 "$@" &&
$make clean &&
$make &&
if $do_reconf; then
	$autoheader &&
	$automake --add-missing --include-deps --build-dir=. --srcdir-name=. --output-dir=. &&
	sed 's%sed '\''s/\\\.am\$/\.in/'\''%sed '\''s/\\\.[aA][mM]\$/\.in/'\''%' <missing >missing.tmp &&
	mv -f missing.tmp missing &&
	chmod a+x missing &&
	sed -n '{h;s/X11R6/X11R7/p;x;p;}' <configure >configure.tmp &&
	mv -f configure.tmp configure &&
	chmod a+x configure &&
	./configure --enable-graphics --enable-debuglevel=2 "$@"
	if [ $? != 0 ]; then echo CONFIGURE FAILED; exit 1; fi
fi
