#! /bin/sh


     ###################################### 
     ##### Model Transformation Tools #####
     ######################################

# Bourne shell script: rbg2ibg_m
# Based on rbg2abg_m

while [ -n "`echo $1 | grep '^-'`" ]; do
    case $1 in
	-I)
	    info=info;;
	*)
	    echo "$1 is an invalid argument - ignoring" ;;
    esac
    shift
done

# Set up some vars
sys=$1
ibg=${sys}_ibg.m
err=mtt_error.txt
log=rbg2ibg_m.log

# Remove the old log file
rm -f ${ibg}
rm -f ${log}

# Inform user
echo Creating ${ibg}

# Use matrix manipulation to accomplish the transformation
${MATRIX} > ${log} 2> ${err} <<EOF
  name = '$1'
  infofile = fopen('mtt_info.txt', 'w');
  errorfile = fopen('mtt_error.txt', 'w');
  
  ## Interpret the connections from the fig version
  [rbonds,rstrokes,rcomponents,port_coord,port_name,port_list] = $1_rbg;
  [n_ports, junk] = size(port_list);
  
  ## Strip the directional information
  rbonds = rbonds(:,1:6);
  
  [bonds] = \
      rbg2ibg(name,rbonds,rstrokes,rcomponents,port_coord,port_name, \
	      infofile,errorfile);
  
  ## Write the intermediate bond graph m-file
  write_ibg(name,bonds);
EOF

# Errors and info
if [ "$info" = "info" ]; then
    cat mtt_info.txt
fi

if mtt_error mtt_error.txt; then
    exit 0
else
    exit 1
fi