#! /usr/bin/env bash

defines=$1/ats_defines
c_output=$1/c/ats_defines.h
fc_output=$1/fc/ats_defines.f90

#------------------------------------------------------------------------------#
# Begin C Language output
#------------------------------------------------------------------------------#

echo "/*" > $c_output
echo " * THIS FILE WAS GENERATED BY make_defines DO NOT EDIT" >> $c_output
echo "*/" >> $c_output
echo "" >> $c_output
echo "#if !defined(_ats_source) && !defined(_include_ats_h)" >> $c_output
echo "#error \"Error: do not include this file directly, use #include <ats.h>\"" >> $c_output
echo "#endif" >> $c_output
echo "" >> $c_output
echo "#ifndef ats_defines_h" >> $c_output
echo "#define ats_defines_h" >> $c_output
echo >> $c_output

#------------------------------------------------------------------------------#
# Begin Fortran output
#------------------------------------------------------------------------------#

echo "!" > $fc_output
echo "! THIS FILE WAS GENERATED BY make_defines DO NOT EDIT" >> $fc_output
echo "!" >> $fc_output
echo "" >> $fc_output
echo "module ats_defines" >> $fc_output
echo "" >> $fc_output
echo "   use, intrinsic :: ISO_C_BINDING" >> $fc_output
echo "   use :: ats_data" >> $fc_output
echo "" >> $fc_output

#------------------------------------------------------------------------------#
# Process input file
#------------------------------------------------------------------------------#

while read p ; do
	code=`echo $p | awk '{ print $1 }'`
	value=`echo $p | awk '{ print $2 }'`

	#---------------------------------------------------------------------------#
	# C Language defines
	#---------------------------------------------------------------------------#

	echo "static const int32_t $code = $value;" >> $c_output

	#---------------------------------------------------------------------------#
	# Fortran defines
	#---------------------------------------------------------------------------#

	echo "      integer(int32_t), bind(C, name='$code') :: $code" >> $fc_output

done < $defines

#------------------------------------------------------------------------------#
# Finish C Language output
#------------------------------------------------------------------------------#

echo "" >> $c_output
echo "#endif // ats_defines_h" >> $c_output

#------------------------------------------------------------------------------#
# Finish Fortran output
#------------------------------------------------------------------------------#

echo "" >> $fc_output
echo "end module ats_defines" >> $fc_output
