#!/bin/sh
# 
# Copyright (c) 2015-2017 Carsten Sonne Larsen  <cs@innolan.dk>
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
# 

set -e

APPSRCS='
	app/main/evaluator.cpp
	app/main/functions.cpp
	app/main/graphlist.cpp
	app/main/lexer.cpp
	app/main/nodes.cpp
	app/main/operators.cpp
	app/main/optimizer.cpp
	app/main/parser.cpp
	app/main/statements.cpp
	app/main/token.cpp
	app/main/values.cpp
	app/main/viewer.cpp
	app/system/console.cpp
	app/system/console_amiga.cpp
	app/system/console_stdc.cpp
	app/system/filesystem_amiga.cpp
	app/system/filesystem_stdc.cpp
	app/system/graph.cpp
	app/system/graph_amiga.cpp
	app/system/graph_gtk.cpp
	app/system/language.cpp
	app/system/language_amiga.cpp
	app/system/language_posix.cpp
	app/system/language_stdc.cpp
	app/system/preferences.cpp
	app/system/preferences_amiga.cpp
	app/system/preferences_stdc.cpp
	app/system/proc_amiga.cpp
	app/system/program.cpp
	app/system/program_amiga.cpp
	app/system/program_stdc.cpp
	app/system/program_test.cpp
	app/system/task_amiga.cpp
	app/system/task_stdc.cpp
	app/system/window_amiga.cpp
	app/system/base/io.cpp
	app/main.cpp
'
LIBAPPSRCS='
	app/lib/aengine.cpp
	app/lib/charbuf.cpp
	app/lib/cplex.cpp
	app/lib/fgrid.cpp
	app/lib/ntext.cpp
	app/lib/real.cpp
'
LIBREALSRCS='
	lib/real/acos.c
	lib/real/acosh.c
	lib/real/asin.c
	lib/real/asinh.c
	lib/real/atan.c
	lib/real/atan2.c
	lib/real/atanh.c
	lib/real/cbrt.c
	lib/real/ceil.c
	lib/real/copysign.c
	lib/real/cos.c
	lib/real/cosh.c
	lib/real/exp.c
	lib/real/expm1.c
	lib/real/fabs.c
	lib/real/finite.c
	lib/real/floor.c
	lib/real/fmod.c
	lib/real/hypot.c
	lib/real/isnan.c
	lib/real/kcos.c
	lib/real/kremp2.c
	lib/real/ksin.c
	lib/real/ktan.c
	lib/real/log.c
	lib/real/log10.c
	lib/real/log1p.c
	lib/real/pow.c
	lib/real/remp2.c
	lib/real/round.c
	lib/real/scalbn.c
	lib/real/sin.c
	lib/real/sinh.c
	lib/real/sqrt.c
	lib/real/tan.c
	lib/real/tanh.c
	lib/real/trunc.c
'
LIBCPLEXSRCS='
	lib/cplex/cacos.c
	lib/cplex/cacosh.c
	lib/cplex/cacot.c
	lib/cplex/cacoth.c
	lib/cplex/cacsc.c
	lib/cplex/cacsch.c
	lib/cplex/casec.c
	lib/cplex/casech.c
	lib/cplex/casin.c
	lib/cplex/casinh.c
	lib/cplex/catan.c
	lib/cplex/catanh.c
	lib/cplex/ccbrt.c
	lib/cplex/ccos.c
	lib/cplex/ccosh.c
	lib/cplex/ccot.c
	lib/cplex/ccoth.c
	lib/cplex/ccsc.c
	lib/cplex/ccsch.c
	lib/cplex/cexp.c
	lib/cplex/clog.c
	lib/cplex/clog10.c
	lib/cplex/clogb.c
	lib/cplex/cpow.c
	lib/cplex/csec.c
	lib/cplex/csech.c
	lib/cplex/csgn.c
	lib/cplex/csin.c
	lib/cplex/csinh.c
	lib/cplex/csqrt.c
	lib/cplex/ctan.c
	lib/cplex/ctanh.c
	lib/cplex/prim.c
'
LIBC1SRCS='
	lib/clib/alloccpy.c
	lib/clib/mem.c
	lib/clib/memcpy.c
	lib/clib/memset.c
	lib/clib/strcmp.c
	lib/clib/strlen.c
	lib/clib/untag.c
'

LIBC2SRCS='
	lib/dconv/dragon4.cpp
	lib/dconv/dmath.cpp
	lib/dconv/dprint.cpp
'

if make -v 2>&1 | grep GNU > /dev/null 2>&1 ; then
	echo "make(1) is GNU make."
	VALID=true
else
	echo "make(1) is not GNU make."
	VALID=true
fi

if $VALID ; then
	if [ -n "$1" ];
	then
		ARCFLAG="$1 "
	else
		ARCFLAG=""
	fi

	if [ -n "$2" ];
	then
		CROSSCOMPILE="$2"
	else
		CROSSCOMPILE=""
	fi

	if [ -n "$3" ];
	then
		CROSSCOMPILEFLAGS="$3 "
	else
		CROSSCOMPILEFLAGS=""
	fi

	if [ -n "$GCC295" ];
	then
		if [ "$GCC295" = "y" ] || [ "$GCC295" = "Y" ];
		then
			gcclib="-lgcc"
		fi
	else
		gcclib="-lstdc++"
	fi

	if [ -n "$REGTEST" ];
	then
		if [ "$REGTEST" = "n" ] || [ "$REGTEST" = "N" ];
		then
			options="${options}"
		fi
	else
		options="${options} -DWITHTEST"
	fi

	if [ -n "$DEBUG" ];
	then
		if [ "$DEBUG" = "y" ] || [ "$DEBUG" = "Y" ];
		then
			appop="-O0 -g"
			libop="-O0 -g"
			exestrip=""
		fi
	else
		appop="-O2"
		libop="-O3"
		exestrip="-s"
	fi

	# Use under certain circumstances when GCC is causing problems with FPU
	#nff="-fno-strict-aliasing -ffloat-store "
	nff=" "

	(
	echo
	echo "CC       = ${CROSSCOMPILE}gcc"
	echo "CXX      = ${CROSSCOMPILE}g++"
#	echo "CC       = ${CROSSCOMPILE}clang"
#	echo "CXX      = ${CROSSCOMPILE}clang++"
	echo "AR       = ${CROSSCOMPILE}ar"
	echo "RANLIB   = ${CROSSCOMPILE}ranlib"
	echo "CFLAGS   = ${ARCFLAG}$options ${CROSSCOMPILEFLAGS}"
	echo "CXXFLAGS = ${ARCFLAG}$options ${CROSSCOMPILEFLAGS}-I."
#	echo "CXXFLAGS = -Wno-inline-new-delete ${ARCFLAG}$options ${CROSSCOMPILEFLAGS}-I."
	echo "LFLAGS   = $gcclib -lamathapp${outext} -lcamath${outext} -lcomplex${outext} -lamath${outext}"
	echo
	echo "FLXCAT   = build/flexcat/flexcat"
	echo "MKDIR    = mkdir"
	echo "COPY     = cp"
	echo "DEL      = rm -f"
	echo "DELREC   = rm -Rf"
	echo
	echo "all:	libs app"
	echo
	echo "libs:	libamath libcomplex libcamath libamathapp"
	echo

	i=""
	l=""
	for f in ${APPSRCS}
	do
		b=`basename $f .cpp`
#		i=`sed -n -e '/#include.*"/{
#			s/"$//
#			s/.*"//
#			p
#		}' $f | sort -u`
		echo "${b}.o:	${f}" ${i}
		echo "	\${CXX} ${appop} \${CXXFLAGS} -Ilib -Iapp -Wall -Werror -c ${f}"
		echo
		l="${l} ${b}.o"
	done

	libapp=""
	for f in ${LIBAPPSRCS}
	do
		b=`basename $f .cpp`
		echo "${b}.o:	${f}"
		echo "	\${CC} ${appop} \${CXXFLAGS} -Ilib -Iapp -Wall -Werror -c ${f}"
		echo
		libapp="${libapp} ${b}.o"
	done

	libamath=""
	for f in ${LIBREALSRCS}
	do
		b=`basename $f .c`
		echo "${b}.o:	${f}"
		echo "	\${CC} ${libop} -std=c9x \${CLAGS} ${ARCFLAG}-fno-builtin ${nff}-Wall -Werror -Ilib -Ilib/real -c ${f}"
		echo
		libamath="${libamath} ${b}.o"
	done

	libcomplex=""
	for f in ${LIBCPLEXSRCS}
	do
		b=`basename $f .c`
		echo "${b}.o:	${f}"
		echo "	\${CC} ${libop} -std=c9x \${CFLAGS} ${ARCFLAG}-fno-builtin -Wall -Werror -Ilib -Ilib/cplex -c ${f}"
		echo
		libcomplex="${libcomplex} ${b}.o"
	done

	libc=""
	for f in ${LIBC1SRCS}
	do
		b=`basename $f .c`
		echo "${b}.o:	${f}"
		echo "	\${CC} ${libop} -std=c9x \${CFLAGS} ${ARCFLAG}${ansicon}-fno-builtin -Wall -Werror -Ilib -c ${f}"
		echo
		libc="${libc} ${b}.o"
	done

	for f in ${LIBC2SRCS}
	do
		b=`basename $f .cpp`
		echo "${b}.o:	${f}"
		echo "	\${CXX} ${libop} \${CXXFLAGS} ${ARCFLAG}-fno-builtin -Wall -Werror -Ilib -Ilib/dconv -c ${f}"
		echo
		libc="${libc} ${b}.o"
	done

	echo "catalogsa:" # Application catalogs
	echo "	\${FLXCAT} text/keyword.cd app/localize/kword.h=text/keyword.sd"
	echo "	\${FLXCAT} text/help.cd app/localize/help.h=text/help.sd"
	echo "	\${FLXCAT} text/ident.cd app/localize/ident.h=text/ident.sd"
	echo "	\${FLXCAT} text/text.cd app/localize/text.h=text/text.sd"
	echo
	echo "catalogsu:" # Unix catalogs
	echo "	iconv -f ISO-8859-15 -t UTF-8 catalog/dansk/amath-help.ct >utext/dk-help.dict"
	echo "	iconv -f ISO-8859-15 -t UTF-8 catalog/dansk/amath-ident.ct >utext/dk-ident.dict"
	echo "	iconv -f ISO-8859-15 -t UTF-8 catalog/dansk/amath-text.ct >utext/dk-text.dict"
	echo "	iconv -f ISO-8859-15 -t UTF-8 catalog/dansk/amath-keyword.ct >utext/dk-keyword.dict"
	echo
	echo "catalogsw:" # Windows catalogs
	echo "	iconv -f ISO-8859-15 -t CP850 catalog/dansk/amath-help.ct >utext/dk-help.dict"
	echo "	iconv -f ISO-8859-15 -t CP850 catalog/dansk/amath-ident.ct >utext/dk-ident.dict"
	echo "	iconv -f ISO-8859-15 -t CP850 catalog/dansk/amath-text.ct >utext/dk-text.dict"
	echo "	iconv -f ISO-8859-15 -t CP850 catalog/dansk/amath-keyword.ct >utext/dk-keyword.dict"
	echo
	echo "libamathapp:	${libapp}"
	echo "	\${AR} rcs libamathapp${outext}.a ${libapp}"
	echo "	\${RANLIB} libamathapp${outext}.a"
	echo
	echo "libamath:	${libamath}"
	echo "	\${AR} rcs libamath${outext}.a ${libamath}"
	echo "	\${RANLIB} libamath${outext}.a"
	echo
	echo "libcomplex:	${libcomplex}"
	echo "	\${AR} rcs libcomplex${outext}.a ${libcomplex}"
	echo "	\${RANLIB} libcomplex${outext}.a"
	echo
	echo "libcamath:	${libc}"
	echo "	\${AR} rcs libcamath${outext}.a ${libc}"
	echo "	\${RANLIB} libcamath${outext}.a"
	echo
	echo "app:	libs ${l}"
	echo "	\${CC} \${CFLAGS} ${ARCFLAG}${CROSSCOMPILEFLAGS}${exestrip} -L. -o amath${outext} ${l} \${LFLAGS}"
	echo
	echo "dist: app catalogs"
#	echo "	\${MKDIR} dist"
	echo "	\${COPY} amath dist/"
	echo "	\${COPY} LICENSE dist/"
	echo "	\${COPY} COPYRIGHT dist/"
	echo
	echo "amigacatalogs:"
#	echo "	\${MKDIR}  dist"
	echo "	\${MKDIR}  dist/catalog"
	echo "	\${MKDIR}  dist/catalog/english"
	echo "	\${FLXCAT} text/help.cd catalog/english/amath-help.ct CATALOG dist/catalog/english/amath-help.catalog"
	echo "	\${FLXCAT} text/ident.cd catalog/english/amath-ident.ct CATALOG dist/catalog/english/amath-ident.catalog"
	echo "	\${FLXCAT} text/text.cd catalog/english/amath-text.ct CATALOG dist/catalog/english/amath-text.catalog"
	echo "	\${MKDIR}  dist/catalog/dansk"
	echo "	\${FLXCAT} text/help.cd catalog/dansk/amath-help.ct CATALOG dist/catalog/dansk/amath-help.catalog"
	echo "	\${FLXCAT} text/ident.cd catalog/dansk/amath-ident.ct CATALOG dist/catalog/dansk/amath-ident.catalog"
	echo "	\${FLXCAT} text/text.cd catalog/dansk/amath-text.ct CATALOG dist/catalog/dansk/amath-text.catalog"
	echo "	\${FLXCAT} text/keyword.cd catalog/dansk/amath-keyword.ct CATALOG dist/catalog/dansk/amath-keyword.catalog"
	echo
	echo "clean:"
	echo "	\${DEL} ${libapp} ${libamath} ${libcomplex} ${libc} ${l} libamath${outext}.a libcamath${outext}.a libcomplex${outext}.a libamathapp${outext}.a amath${outext}"
	echo
	echo "distclean:"
	echo "	\${DEL} ${libapp} ${libamath} ${libcomplex} ${libc} ${l} libamath${outext}.a libcamath${outext}.a libcomplex${outext}.a libamathapp${outext}.a amath${outext}"
	echo "	\${DELREC} dist"
	echo
	echo "depend:"
	echo "	@echo Dependencies already done"
	) > Makefile

	echo "Makefile generated${msg}"
#	echo
#	echo "Build with debug symbols in csh:"
#	echo "DEBUG=\"Y\" make"
	echo
fi
