#! /bin/sh

sys=$1
icd=${sys}_*_icd.txt2
ICD=${sys}_ICD.txt

echo Creating ${ICD}

# lbl.txt ICD Format:
# [#|%]ICD PortName EffortName,EffortUnit;FlowName,FlowUnit
# $1       $2       $3
# Comma / Semi-colon distinction unenforced

# icd.txt Format
# PortName EffortName,EffortUnit;FlowName,FlowUnit input|output,input|output
# $1       $2         $3         $4       $5       $6           $7

cat <<EOF > ${ICD}
## Interface Control Definition for System ${sys}
## ${ICD}: Generated by MTT `date`

EOF

cat ${icd}		|\
    sed 's/[\,\;]/\ /g'	|\
    gawk '
    ($2 != "null" && $6 == "input")  { printf ("Input:  %-30s %-30s Causality: Effort   Units: %s\n", $2, $1, $3) }
    ($4 != "null" && $7 == "input")  { printf ("Input:  %-30s %-30s Causality: Flow     Units: %s\n", $4, $1, $5) }
    ($2 != "null" && $6 == "output") { printf ("Output: %-30s %-30s Causality: Effort   Units: %s\n", $2, $1, $3) }
    ($4 != "null" && $7 == "output") { printf ("Output: %-30s %-30s Causality: Flow     Units: %s\n", $4, $1, $5) }
    ' | sort >> ${ICD}
