#! /bin/sh

sys=$1
lang=$2

ICD=${sys}_ICD.${lang}
txt=${sys}_ICD.txt
struc=${sys}_struc.txt

case ${lang} in
    c)	lb='['; rb=']'; offset=0; comment='//';;
    cc)	lb='('; rb=')'; offset=1; comment='//';;
    m)	lb='('; rb=')'; offset=0; comment='#';;
    *)	echo Language ${lang} not yet supported.; exit -1;;
esac

echo Creating ${ICD}

cat <<EOF > ${ICD}
${comment} Interface Control Definition mappings for system ${sys}
${comment} ${ICD}: Generated by MTT `date`

EOF

# ICD.txt Format:
# Input|Output ICD_Name PortName Causality: causality Units: units
# $1           $2       $3       $4         $5        $6     $7

cat <<EOF >> ${ICD}

${comment} Inputs

EOF
for name in `cat ${txt} | awk '($1 == "Input:") { print $3 }'`
do
    num=`grep ${name} ${struc} | awk '($1 == "input") { print $2 }'`
    grep ^Input ${txt} |\
	awk '($3 == name) { printf ("\tmttu%c%d%c\t= %s;\n", lb, num-offset, rb, $2) }'\
	lb=${lb} rb=${rb} name=${name} num=${num} offset=${offset} >> ${ICD}
done

cat <<EOF >> ${ICD}

${comment} Outputs

EOF
for name in `cat ${txt} | awk '($1 == "Output:") { print $3 }'`
do
    num=`grep ${name} ${struc} | awk '($1 == "output") { print $2 }'`
    grep ^Output ${txt} |\
	awk '($3 == name) { printf ("\t%-30s = mtty%c%d%c;\n", $2, lb, num-offset, rb) }'\
	lb=${lb} rb=${rb} name=${name} num=${num} offset=${offset} >> ${ICD}
done

