#!/bin/sh
#
# This script attempts to compile the given ASN.1 specification and then
# transforms the resulting Makefile.am.sample tailored to that specification
# into a customized Makefile.
#
# The information which is necessary for this script is passed from the
# Makefile which is going to be replaced with an updated Makefile.
# This is called "boot-strapping" and is frequently the source of inside jokes.
#
# $Id$
#

if test -z "$ASN1PDU"	\
 -o  -z "$ASN1MODULES"	\
 -o  -z "$PROGNAME"	\
; then
	echo "ASN1CMDOPTS=\"$ASN1CMDOPTS\""
	echo "ASN1MODULES=\"$ASN1MODULES\""
	echo "ASN1PDU=\"$ASN1PDU\""
	echo "PROGNAME=\"$PROGNAME\""
	echo "ASN1PDU, ASN1MODULES, and PROGNAME are mandatory"
	exit
fi

CMDOPTS="-pdu=${ASN1PDU} ${ASN1CMDOPTS} ${ASN1MODULES}"

if test -x ../../asn1c/asn1c ; then
  echo "Compiling ${ASN1MODULES} using local compiler"
  ../../asn1c/asn1c -S ../../skeletons ${CMDOPTS} || exit $?
else
  echo "Compiling ${ASN1MODULES} using system compiler"
  asn1c ${CMDOPTS} || exit $?
fi

if test ! -f Makefile.am.sample ; then
	echo "Makefile.am.sample is missing"
	exit 1
fi

EXTRA_CPPFLAGS="-DJUNKTEST -D_DEFAULT_SOURCE ${CPPFLAGS}"
test -f config.h && EXTRA_CPPFLAGS="-DHAVE_CONFIG_H ${EXTRA_CPPFLAGS}"
test -n "$TITLE" && EXTRA_CPPFLAGS="-DASN_CONVERTER_TITLE=\"$TITLE\" ${EXTRA_CPPFLAGS}"

set -x
cat Makefile.am.sample						\
	| sed -e "s/^CPPFLAGS += /CPPFLAGS += ${EXTRA_CPPFLAGS} /"	\
	| sed -e "s/^all: /all: maybe-wip-pause ${ASN1PDU}.c /"		\
	| sed -e "s/progname/${PROGNAME}/"			\
	> Makefile.$$
set +x

(	echo
	echo "${ASN1PDU}.c: $0 ${ASN1MODULES}"
	echo "	make regen-makefile"
	echo "	@touch ${ASN1PDU}.c"
	echo "	make"
	echo
	echo "regen-makefile:"
	test -n "$TITLE" && echo "	TITLE=\"$TITLE\" \\"
	echo "	ASN1CMDOPTS=\"${ASN1CMDOPTS}\" \\"
	echo "	ASN1MODULES=\"${ASN1MODULES}\" \\"
	echo "	ASN1PDU=${ASN1PDU} \\"
	echo "	PROGNAME=${PROGNAME} \\"
	echo "	CPPFLAGS=\"${CPPFLAGS}\" \\"
	echo "	$0"
	echo
	echo 'check: ${TARGET} check-ber check-xer check-oer check-per'
	echo '	@echo ================'
	echo '	@echo All tests passed'
	echo '	@echo ================'
	echo
	echo 'check-ber:'
	echo "	@if test -f sample-${ASN1PDU}-1.[db]er ; then \\"
	echo "	for f in sample-*-*.[db]er; do \\"
	echo '	pdu=`echo $$f | sed -E -e "s/sample-([A-Za-z-]+)-[0-9].*/\\1/"`; \'
	echo '	for b in 1 17 33 980 8192; do \'
	echo '	echo "Recoding $$f ($$pdu) into XER and back ($$b)..."; \'
	echo '	./${TARGET} -p $$pdu -b $$b -iber -oxer $$f > ./.tmp.1.$$$$ || exit 2; \'
	echo '	./${TARGET} -p $$pdu -b $$b -ixer -oxer ./.tmp.1.$$$$ > ./.tmp.2.$$$$ || exit 3; \'
	echo '	diff ./.tmp.1.$$$$ ./.tmp.2.$$$$ || exit 4; \'
	echo '	rm -f ./.tmp.[12].$$$$; \'
	echo '	echo "Test junking $$f (please wait)..."; \'
	echo '	./${TARGET} -J0.0001 -n 1000 -p $$pdu -b $$b -iber -onull $$f || exit 5; \'
	echo '	./${TARGET} -J0.001 -n 1000 -p $$pdu -b $$b -iber -onull $$f || exit 6; \'
	echo '	done; done; fi'
	echo
	echo 'check-xer:'
	echo "	@if test -f sample-${ASN1PDU}-1.xer ; then \\"
	echo "	for f in sample-*-*.xer; do \\"
	echo '	pdu=`echo $$f | sed -E -e "s/sample-([A-Za-z-]+)-[0-9].*/\\1/"`; \'
	echo '	for b in 1 17 33 980 8192; do \'
	echo '	echo "Recoding $$f ($$pdu) into DER and back ($$b)..."; \'
	echo '	./${TARGET} -p $$pdu -b $$b -ixer -oder $$f > ./.tmp.1.$$$$ || exit 2; \'
	echo '	./${TARGET} -p $$pdu -b $$b -iber -oxer ./.tmp.1.$$$$ > ./.tmp.2.$$$$ || exit 3; \'
	echo '	diff $$f ./.tmp.2.$$$$ || exit 4; \'
	echo '	rm -f ./.tmp.[12].$$$$; \'
	echo '	echo "Test junking $$f (please wait)..."; \'
	echo '	./${TARGET} -J0.0001 -n 1000 -p $$pdu -b $$b -ixer -onull $$f || exit 5; \'
	echo '	./${TARGET} -J0.001 -n 1000 -p $$pdu -b $$b -ixer -onull $$f || exit 6; \'
	echo '	done; done; fi'
	echo
	echo 'check-oer:'
	echo "	@if test -f sample-${ASN1PDU}-1.*oer ; then \\"
	echo "	for f in sample-*-*.{c,}oer; do \\"
	echo '	pdu=`echo $$f | sed -E -e "s/sample-([A-Za-z-]+)-[0-9].*/\\1/"`; \'
	echo '	for b in 1 17 33 980 8192; do \'
	echo '	echo "Recoding $$f ($$pdu) into XER and back ($$b)..."; \'
	echo '	./${TARGET} -p $$pdu -b $$b -ioer -oxer $$f > ./.tmp.1.$$$$ || exit 2; \'
	echo '	./${TARGET} -p $$pdu -b $$b -ioer -oxer ./.tmp.1.$$$$ > ./.tmp.2.$$$$ || exit 3; \'
	echo '	diff ./.tmp.1.$$$$ ./.tmp.2.$$$$ || exit 4; \'
	echo '	rm -f ./.tmp.[12].$$$$; \'
	echo '	echo "Test junking $$f (please wait)..."; \'
	echo '	./${TARGET} -J0.0001 -n 1000 -p $$pdu -b $$b -ioer -onull $$f || exit 5; \'
	echo '	./${TARGET} -J0.001 -n 1000 -p $$pdu -b $$b -ioer -onull $$f || exit 6; \'
	echo '	done; done; fi'
	echo
	echo 'check-per:'
	echo "	@if test -f sample-${ASN1PDU}-1-nopad.per ; then \\"
	echo "	for f in sample-*-[1-9]-nopad.per; do \\"
	echo '	pdu=`echo $$f | sed -E -e "s/sample-([A-Za-z-]+)-[0-9].*/\\1/"`; \'
	echo '	for b in 1 17 33 980 8192; do \'
	echo '	echo "Recoding non-padded $$f ($$pdu) into DER into XER and back ($$b)..."; \'
	echo '	./${TARGET} -p $$pdu -b $$b -per-nopad -iper -oder $$f > ./.tmp.1.$$$$ || exit 2; \'
	echo '	./${TARGET} -p $$pdu -b $$b -iber -oxer ./.tmp.1.$$$$ > ./.tmp.2.$$$$ || exit 3; \'
	echo '	./${TARGET} -p $$pdu -b $$b -ixer -oder ./.tmp.2.$$$$ > ./.tmp.3.$$$$ || exit 4; \'
	echo '	diff ./.tmp.1.$$$$ ./.tmp.3.$$$$ || exit 5; \'
	echo '	rm -f ./.tmp.[123].$$$$; \'
	echo '	echo "Test junking $$f (please wait)..."; \'
	echo '	./${TARGET} -J0.0001 -n 1000 -p $$pdu -b $$b -per-nopad -iper -onull $$f || exit 6; \'
	echo '	./${TARGET} -J0.001 -n 1000 -p $$pdu -b $$b -per-nopad -iper -onull $$f || exit 7; \'
	echo '	done; done; fi'
	echo "	@if test -f sample-${ASN1PDU}-1.per ; then \\"
	echo "	for f in sample-*-[1-9].per; do \\"
	echo '	pdu=`echo $$f | sed -E -e "s/sample-([A-Za-z-]+)-[0-9].*/\\1/"`; \'
	echo '	for b in 1 17 33 980 8192; do \'
	echo '	echo "Recoding $$f ($$pdu) into DER into XER and back ($$b)..."; \'
	echo '	./${TARGET} -p $$pdu -b $$b -iper -oder $$f > ./.tmp.1.$$$$ || exit 3; \'
	echo '	./${TARGET} -p $$pdu -b $$b -iber -oxer ./.tmp.1.$$$$ > ./.tmp.2.$$$$ || exit 4; \'
	echo '	./${TARGET} -p $$pdu -b $$b -ixer -oper ./.tmp.2.$$$$ > ./.tmp.1.$$$$ || exit 5; \'
	echo '	diff $$f ./.tmp.1.$$$$ || exit 6; \'
	echo '	rm -f ./.tmp.[12].$$$$; \'
	echo '	echo "Test junking $$f (please wait)..."; \'
	echo '	./${TARGET} -J0.0001 -n 1000 -p $$pdu -b $$b -iper -onull $$f || exit 7; \'
	echo '	./${TARGET} -J0.001 -n 1000 -p $$pdu -b $$b -iper -onull $$f || exit 8; \'
	echo '	done; done; fi'
	echo
	echo "maybe-wip-pause:"
	echo "	@if [ -f WIP ]; then cat WIP; sleep 2; fi"
	echo
	echo "distclean: clean"
	echo '	rm -f $(ASN_MODULE_SOURCES)'
	echo '	rm -f $(ASN_MODULE_HEADERS)'
	echo '	rm -f $(ASN_CONVERTER_SOURCES) $(ASN_CONVERTER_HEADERS)'
	echo "	rm -f Makefile.am.sample"
) >> Makefile.$$

set -x
mv Makefile.$$ Makefile || exit $?
rm Makefile.am.sample || exit $?
set +x

echo
echo "Makefile generation finished"
