#!/bin/bash

vif_version=1.4.8

echo 'configure: VIF'

vif_h=src/include/vif.h

rm -f Makefile
rm -f $vif_h
cp vif.h.in $vif_h

top_config () {
	FN=/tmp/vif.h.tmp
	{ echo $1; cat $vif_h; } > $FN && mv $FN $vif_h
	rm -f $FN
}

echo 'configure: check platform'

echo 'checking build system type...' `uname -mrs`
echo 'checking platform...' $OSTYPE
case "$OSTYPE" in
linux*)
	if command -v sed >/dev/null 2>&1; then
		echo 'checking for sed... yes'
	else
		echo 'checking for sed... no'
		echo 'configure: cannot build VIF'
		exit
	fi
	if command -v make >/dev/null 2>&1; then
		echo 'checking for make... yes'
	else
		echo 'checking for make... no'
		echo 'configure: cannot build VIF'
		exit
	fi
	top_config '#define HAVE_LINUX'
	;;
freebsd*)
	if command -v gsed >/dev/null 2>&1; then
		echo 'checking for gsed... yes'
	else
		echo 'checking for gsed... no'
		echo 'configure: cannot build VIF'
		exit
	fi
	if command -v gmake >/dev/null 2>&1; then
		echo 'checking for gmake... yes'
	else
		echo 'checking for gmake... no'
		echo 'configure: cannot build VIF'
		exit
	fi
	top_config '#define HAVE_FREEBSD'
	;;
*)
	echo 'configure: cannot build VIF'
	exit
	;;
esac
if command -v gcc >/dev/null 2>&1; then
	echo 'checking for gcc... yes'
else
	echo 'checking for gcc... no'
	echo 'configure: cannot build VIF'
	exit
fi

echo 'configure: check include files'

include_test()
{
	FN=/tmp/vif.tmp.c
	echo "#include <$1>" > $FN
	echo "void main (void) {;}" >> $FN
	if gcc $FN >/dev/null 2>&1; then
		echo "checking for $1... yes"
	else
		echo "checking for $1... no"
		echo 'configure: cannot build VIF'
		exit
	fi
	rm -f $FN
}

includes=(
	"complex.h"
	"ctype.h"
	"endian.h"
	"errno.h"
	"execinfo.h"
	"float.h"
	"inttypes.h"
	"libgen.h"
	"limits.h"
	"math.h"
	"quadmath.h"
	"signal.h"
	"stdarg.h"
	"stddef.h"
	"stdint.h"
	"stdio.h"
	"stdlib.h"
	"string.h"
	"sys/stat.h"
	"sys/time.h"
	"sys/utsname.h"
	"time.h"
)

for i in "${includes[@]}"; do
    include_test $i
done

echo 'configure: check types'

type_test()
{
	FN=/tmp/vif.tmp.c
	echo "#include <complex.h>" > $FN
	echo "#include <inttypes.h>" >> $FN
	echo "#include <quadmath.h>" >> $FN
	echo "void main (void) {$1 _z_;}" >> $FN
	if gcc $FN >/dev/null 2>&1; then
		echo "checking for $1... yes"
	else
		echo "checking for $1... no"
		echo 'configure: cannot build VIF'
		exit
	fi
	rm -f $FN
}

types=(
	"int16_t"
	"int"
	"int64_t"
	"unsigned"
	"float"
	"double"
	"__float128"
	"complex"
	"double complex"
	"__complex128"
)

for i in "${types[@]}"; do
    type_test $i
done

echo 'configure: check libraries'

link_test()
{
	FN=/tmp/vif.tmp.c
	echo "char $1();" >> $FN
	echo "char main (void) {return $1();}" >> $FN
	if gcc -w $FN $2 >/dev/null 2>&1; then
		echo "checking for $1 in $2... yes"
	else
		echo "checking for $1 in $2... no"
		echo 'configure: cannot build VIF'
		exit
	fi
	rm -f $FN
}

link_test "cos" "-lm"
link_test "lgammaq" "-lquadmath"

case "$OSTYPE" in
freebsd*)
	link_test "backtrace" "-lexecinfo"
	;;
esac

echo 'configure: creating configuration'

FN=/tmp/vif_h

cat > $FN.tmp << \_EOF
//! @file vif.h
//! @author J. Marcel van der Veer
//
//! @section Copyright
//
// This file is part of VIF - vintage FORTRAN compiler.
// Copyright 2020-2026 J. Marcel van der Veer <jmvdveer@algol68genie.nl>.
//
//! @section License
//
// 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 3 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, see <http://www.gnu.org/licenses/>.

//! @section Synopsis
//!
//! VIF include file.

// This is a monolithic include file to avoid cluttering the installation directory.

#if ! defined (__VIF_H__)
#define __VIF_H__
_EOF

cat $FN.tmp $vif_h > $FN.tmq
mv $FN.tmq $vif_h
rm -f $FN.tmp

echo '#endif' >> $vif_h

echo 'configure: creating makefile'

if [ -e Makefile.in ]; then
	cp Makefile.in Makefile
elif [ -e Makefile-dist.in ]; then
	cp Makefile-dist.in Makefile
fi

top_makefile () {
	FN=/tmp/vif-makefile.tmp
	{ echo $1; cat Makefile; } > $FN && mv $FN Makefile
}

case "$OSTYPE" in
linux*)
	top_makefile "export LD_FLAGS=-lvif -lquadmath -lm"	
	top_makefile "SED=sed"	
	top_makefile "VERSION=$vif_version"
	echo "configure: type 'make', 'make install' or 'make check'"
	;;
freebsd*)
	top_makefile "export LD_FLAGS=-lvif -lquadmath -lm -lexecinfo"
	top_makefile "SED=gsed"	
	top_makefile "VERSION=$vif_version"
	echo "configure: type 'gmake', 'gmake install' or 'gmake check'"
	;;
esac
