#!/bin/csh

## Regschem: Registers a functional or timing model with a Mentor interface
## Usage:    regschem [-f|-t] interface_component model_component

# Usage is printed if no arguments or invalid arguments are given

if ("$1" == "") then
  echo "Usage: regschem [-f|t] interface_component model_component[/schematic]"
  exit
endif

set simtype = $1

set switch_test = `echo $simtype | sed s/^-//1`

if ("$switch_test" == "$simtype") then
  set simtype = -f
  set ifcomp_soft = $1
  set model_soft = $2
  if ("$3" != "") then
    echo "Usage: regschem [-f|t] interface_component model_component[/schematic]"
    exit
  endif
else
  set ifcomp_soft = $2
  set model_soft = $3
  if ("$4" != "") then
    echo "Usage: regschem [-f|t] interface_component model_component[/schematic]"
    exit
  endif
endif

if (("$model_soft" == "") || ("$ifcomp_soft" == "")) then
  echo "Usage: regschem [-f|t] interface_component model_component[/schematic]"
  exit
endif

set label = functional
set opposite_label = timing
if ("$simtype" == "-t") then
  set label = timing
  set opposite_label = functional
else if ("$simtype" != "-f") then
  echo "Usage: regschem [-f|t] interface_component model_component[/schematic]"
  exit
endif

echo "REGISTER SCHEMATIC (October 21, 1996)"

set ifcomp_hard = `$MGC_HOME/bin/get_hard_name $ifcomp_soft`
set ifcomp_soft = `$MGC_HOME/bin/get_soft_name $ifcomp_hard`
set model_hard  = `$MGC_HOME/bin/get_hard_name $model_soft`
set model_soft  = `$MGC_HOME/bin/get_soft_name $model_hard`

/bin/rm -f .cib_output
/bin/rm -f .registry

if (!(-e $ifcomp_hard)) then
  echo "ERROR: Component interface $ifcomp_soft does not exist\!"
  exit
endif

if (-e ${model_hard}.mgc_component.attr) then
  set model_soft = $model_soft/schematic
  set model_hard = $model_hard/schematic
endif

if (!(-e ${model_hard}.mgc_schematic.attr)) then
  echo "ERROR: Schematic model $model_soft does not exist\!"
  exit
endif

/bin/rm -f .view_cmd
echo "view" > .view_cmd
echo "quit" >> .view_cmd

echo "Querying the Component Interface Browser..."
$MGC_HOME/bin/cib $ifcomp_hard < .view_cmd > .cib_output

echo "Checking the model registry..."
getreg.nawk -v MODEL=$model_soft .cib_output

set entry_number = `grep ^Entry .registry | awk '{ print $2 }'`
set last_entry = `grep ^Last .registry | awk '{ print $2 }'`

set register_command = ""
if ("$entry_number" == "") then
  set entry_number = `echo "$last_entry + 1" | bc`
  set register_command = "register_model $model_soft mgc_schematic"
endif

set label_command = ""
set unlabel_command = ""
set label_test = `grep -i $label .registry`
set unlabel_test = `grep -i $opposite_label .registry`
if ("$label_test" == "") then
  set label_command = "add_label $entry_number $label"
endif
if ("$unlabel_test" != "") then
  set unlabel_command = "delete_label $entry_number $opposite_label"
endif

set cib_commands = ""
if ("$register_command" == "") then
  if ("$label_command" == "") then
    echo "Model $model_soft (mgc_schematic)"
    echo "  is already registered with label '$label'"
  else
    echo "Updating $model_soft (mgc_schematic)"
    echo "  with label '$label'..."
    set cib_commands = "$label_command"
  endif
else
  echo "Adding $model_soft (mgc_schematic)"
  echo "  with label '$label'..."
  set cib_commands = "$register_command ; $label_command"
endif

if ("$unlabel_command" != "") then
  echo "Removing label '$opposite_label' from model entry..."
  if ("$cib_commands" == "") then
    set cib_commands = "$unlabel_command"
  else
    set cib_commands = "$cib_commands ; $unlabel_command"
  endif
endif

if ("$cib_commands" != "") then
  set cib_commands = "$cib_commands ; save ; quit"
  echo "$cib_commands" | $MGC_HOME/bin/cib $ifcomp_hard > /dev/null
endif

/bin/rm -f .temp cib_view.log cib.cmd

echo "Writing CIB commands to cib.cmd..."
echo "open $model_soft" > .temp
echo "$register_command" >> .temp
echo "$label_command" >> .temp
echo "$unlabel_command" >> .temp
if ("$cib_commands" != "") then
  echo "save" >> .temp
endif
cat .view_cmd >> .temp
grep -v ^$ .temp > cib.cmd

echo "Writing interface report to cib_view.log..."
$MGC_HOME/bin/cib $ifcomp_hard < .view_cmd  > cib_view.log

/bin/rm -f .cib_output .registry .view_cmd .temp

echo "DONE"
