#!/bin/sh
# 
# Copyright (c) 2015 Carsten Larsen
# Copyright (c) 2014 Poul-Henning Kamp
# 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

HDRS='
	compiler.h
	mem.h
	ntimed.h
	ntimed_endian.h
	ntimed_platform.h
	ntimed_queue.h
	ntimed_tricks.h
	ntp.h
	ntp_tbl.h
	net_getaddrinfo.h
	param_instance.h
	param_tbl.h
	udp.h
'

SRCS='
	main_amiga_client.c
	main_amiga_poll.c
	main_amiga_sim.c
	main_amiga_test.c
	combine_delta.c
	mem.c
	net_getaddrinfo.c
	ntp_filter.c
	ntp_packet.c
	ntp_peer.c
	ntp_peerset.c
	ntp_tools.c
	ocx_stdio.c
	param.c
	pll_std.c
	suckaddr.c
	time_amiga.c
	time_sim.c
	time_stuff.c
	time_unix.c
	todo.c
	udp.c
'

#################################################

SRCS1='
	main_amiga_client.c
	combine_delta.c
	mem.c
	net_getaddrinfo.c
	ntp_filter.c
	ntp_packet.c
	ntp_peer.c
	ntp_peerset.c
	ntp_tools.c
	ocx_stdio.c
	param.c
	pll_std.c
	suckaddr.c
	time_amiga.c
	time_stuff.c
	time_unix.c
	todo.c
	udp.c
'

SRCS2='
	main_amiga_poll.c
	combine_delta.c
	mem.c
	net_getaddrinfo.c
	ntp_filter.c
	ntp_packet.c
	ntp_peer.c
	ntp_peerset.c
	ntp_tools.c
	ocx_stdio.c
	param.c
	pll_std.c
	suckaddr.c
	time_amiga.c
	time_stuff.c
	time_unix.c
	todo.c
	udp.c
'

SRCS3='
	main_amiga_sim.c
	combine_delta.c
	mem.c
	net_getaddrinfo.c
	ntp_filter.c
	ntp_packet.c
	ntp_peer.c
	ntp_peerset.c
	ntp_tools.c
	ocx_stdio.c
	param.c
	pll_std.c
	suckaddr.c
	time_amiga.c
	time_sim.c
	time_stuff.c
	todo.c
	udp.c
'

SRCS4='
	main_amiga_test.c
	combine_delta.c
	mem.c
	net_getaddrinfo.c
	ntp_filter.c
	ntp_packet.c
	ntp_peer.c
	ntp_peerset.c
	ntp_tools.c
	ocx_stdio.c
	param.c
	pll_std.c
	suckaddr.c
	time_amiga.c
	time_stuff.c
	time_unix.c
	todo.c
	udp.c
'

#################################################

if make -v 2>&1 | grep GNU > /dev/null 2>&1 ; then
	echo "make is GNU make."
	VALID=true
#else
#	echo "Wrong version of make."
#	VALID=false
fi

if $VALID ; then
	(
	echo '# Portable Makefile generated by configure'
	echo ''
	echo 'all:	ntimed ntimed-poll ntimed-sim ntimed-test'
	echo ''
	if [ -n "$1" ] && [ $1 = "AMIGA" ] ; then
		echo 'CC     = gcc'
		echo "CFLAGS = -O2 -DAOS3 -DWITHTEST -Wall -Werror"
		echo "LDLIBS = -lm"
	elif [ -n "$1" ] && [ $1 = "AROS" ] ; then
		echo 'CC     = gcc'
		echo "CFLAGS = -O2 -DAROS -DWITHTEST -Wall -Werror"
		echo "LDLIBS = -lm"
	else
		echo "CFLAGS = -O2 -Wall -DWITHTEST -Werror"
		echo "LDLIBS = -lm"
	fi
	echo ''

	for f in ${HDRS}
	do
		b=`basename $f .h`
		i=`sed -n -e '/#include.*"/{
			s/"$//
			s/.*"//
			p
		}' $f | sort -u`
		if [ "x${i}" != "x" ] ; then
			echo "${b}.h:	" ${i}
			echo "	touch ${b}.h"
			echo
		fi
	done
	
	l=""
	for f in ${SRCS}
	do
		b=`basename $f .c`
		i=`sed -n -e '/#include.*"/{
			s/"$//
			s/.*"//
			p
		}' $f | sort -u`
		echo "${b}.o:	${b}.c" ${i}
		echo
		l="${l} ${b}.o"
	done

	#################################################
	BIN1=""
	for f in ${SRCS1}
	do
		BIN1="${BIN1} `basename $f .c`.o"
	done

	BIN2=""
	for f in ${SRCS2}
	do
		BIN2="${BIN2} `basename $f .c`.o"
	done

	BIN3=""
	for f in ${SRCS3}
	do
		BIN3="${BIN3} `basename $f .c`.o"
	done

	BIN4=""
	for f in ${SRCS4}
	do
		BIN4="${BIN4} `basename $f .c`.o"
	done
	#################################################

	echo
	echo "ntimed:	${BIN1}"
	echo "	\${CC} \${CFLAGS} -o ntimed ${BIN1} \${LDLIBS}"
	echo
	echo "ntimed-poll:	${BIN2}"
	echo "	\${CC} \${CFLAGS} -o ntimed-poll ${BIN2} \${LDLIBS}"
	echo
	echo "ntimed-sim:	${BIN3}"
	echo "	\${CC} \${CFLAGS} -o ntimed-sim ${BIN3} \${LDLIBS}"
	echo
	echo "ntimed-test:	${BIN4}"
	echo "	\${CC} \${CFLAGS} -o ntimed-test ${BIN4} \${LDLIBS}"
	echo
	echo "clean:"
	echo "	rm -f ${l} ntimed ntimed-poll ntimed-sim ntimed-test"
	echo
	echo "depend:"
	echo "	@echo Dependencies already done"
	) > Makefile
	
	echo "Makefile generated"
fi
