#!/bin/csh

## Unregschem: Unregisters a model from a Mentor interface
## Usage:      unregschem interface_component model_component

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

if ("$1" == "") then
  echo "Usage: unregschem interface_component model_component[/schematic]"
  exit
endif

set ifcomp_soft = $1
set model_soft = $2
if ("$3" != "") then
  echo "Usage: unregschem interface_component model_component[/schematic]"
  exit
endif

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

echo "UNREGISTER 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

/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 }'`
if ("$entry_number" == "") then
  set model_soft = $model_soft/schematic
  set model_hard = $model_hard/schematic
  getreg.nawk -v MODEL=$model_soft .cib_output
  set entry_number = `grep ^Entry .registry | awk '{ print $2 }'`
endif

set unregister_command = ""
if ("$entry_number" == "") then
  echo "Model $model_soft (mgc_schematic)"
  echo "  is NOT registered with this component\!"
else
  echo "Removing model $model_soft (mgc_schematic)"
  echo "  from this component..."
  set unregister_command = "unregister_model $entry_number"
endif

if ("$unregister_command" != "") then
  set cib_commands = "$unregister_command ; 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
if ("$unregister_command" != "") then
  echo "$unregister_command" >> .temp
  echo "save" >> .temp
endif
cat .view_cmd >> .temp
mv .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

echo "DONE"
