#! /bin/sh
# configuration script: Generate two files (one to be included by FORTRAN
# files and a C header file) which will deal with the architecture-related
# differences in upstream code.
#
# Author: Rock Storm <rockstorm@gmx.com>

# Set tag names
zpl_tag="ZPL_TAG"
system_tag="SYSTEM_TAG"
os_tag="OS_TAG"
compiler_tag="COMPILER_TAG"
endianness_tag="ENDIANNESS_TAG"
maxopn="MAX_OPN"
maxlun="MAX_LUN"
trunc="_TRUNC"


# Test whether input file exists
input_file="debian/SpiceZplDeb.in"
header_file="debian/SpiceZplDeb.h"
include_file="debian/splicezpldeb.inc"
if test -f "$input_file"; then :
else
    echo "Error in: $0"
    echo "Cannot find input file: $input_file . Aborting configuration." 1>&2
    exit 2
fi

cp "$input_file" "$header_file"



#
# DETECT WORD SIZE
#
if test "$DEB_TARGET_ARCH_BITS" = "64" ; then

    echo "    #define CSPICE_PC_LINUX_64BIT_GCC"    		>> $header_file
    echo "      CHARACTER*(*)	$compiler_tag"				>> $include_file
    echo "      PARAMETER	( $compiler_tag = 'GCC/64BIT')"	>> $include_file

elif test "$DEB_TARGET_ARCH_BITS" = "32" ; then

    echo "    #define CSPICE_PC_LINUX"              		>> $header_file
    echo "      CHARACTER*(*)	$compiler_tag"				>> $include_file
    echo "      PARAMETER	( $compiler_tag = 'GCC')"		>> $include_file

else

    echo "ERROR: Word size could not be determined." 1>&2
    echo "DEB_TARGET_ARCH_BITS = $DEB_TARGET_ARCH_BITS" 1>&2
    echo "Aborting configuration." 1>&2
    exit 1
    
fi
echo ""														>> $include_file

#
# DETECT ENDIANNESS
#
if test "$DEB_TARGET_ARCH_ENDIAN" = "little" ; then

    echo "      CHARACTER*(*)	$endianness_tag"			>> $include_file
    echo "      PARAMETER	( $endianness_tag = 'LTL-IEEE')">> $include_file

elif test "$DEB_TARGET_ARCH_ENDIAN" = "big" ; then

    echo "      CHARACTER*(*)	$endianness_tag"			>> $include_file
    echo "      PARAMETER	( $endianness_tag = 'BIG-IEEE')">> $include_file

else

    echo "ERROR: Endianness could not be determined." 1>&2
    echo "DEB_TARGET_ARCH_ENDIAN = $DEB_TARGET_ARCH_ENDIAN" 1>&2
    echo "Aborting configuration." 1>&2
    exit 1

fi
echo ""														>> $include_file

#
# DETECT ARCHITECTURE
#
if test "$DEB_TARGET_ARCH" = "sparc"   ||
   test "$DEB_TARGET_ARCH" = "sparc64" ; then

    echo "      INTEGER	$maxopn"							>> $include_file
    echo "      PARAMETER	( $maxopn = 64)"				>> $include_file
    echo ""													>> $include_file
    echo "      INTEGER	$maxlun"							>> $include_file
    echo "      PARAMETER	( $maxlun = 63)"				>> $include_file
    echo ""													>> $include_file
    echo "      INTEGER	$trunc"							    >> $include_file
    echo "      PARAMETER	( $trunc = 10)"				    >> $include_file

else

    echo "      INTEGER	$maxopn"							>> $include_file
    echo "      PARAMETER	( $maxopn = 96)"				>> $include_file
    echo ""													>> $include_file
    echo "      INTEGER	$maxlun"							>> $include_file
    echo "      PARAMETER	( $maxlun = 99)"				>> $include_file
    echo ""													>> $include_file
    echo "      INTEGER	$trunc"							    >> $include_file
    echo "      PARAMETER	( $trunc = 11)"				    >> $include_file

fi


echo ""       >> $header_file
echo "#endif" >> $header_file
