#!/bin/sh
#
#  sdl_prep	V 2.0	10/25/96 
#
###################################################################
#
# program: sdl_prep 
# purpose: Prepare SDL schematic for analog simulation and layout. 
# input: <design>
# output: <design> viewpoint sdl 
# authors: David M. Zar
#
###################################################################

  version="2.0"


#################################################################
# function: help
###################################################################

help() {
  cat <<EOF

This program Prepares your SDL schematic design for 
analog simulation with AccuSim II

Usage: sdl_prep <design> [ -technology -verbose -help ]

  where <design> is the name of your mentor design directory

    -technology	   - 2.0 1.2 0.8 0.6 where the default is 0.8 

    -verbose       - Run the script in verbose mode.
                     All output messages will be  shown on monitor
                     as well as piped to sdl_prep.log file.

    -help          - Displays this message

Note:

    Your MGC_WD environment variable must be set to the location
    of your design.


Examples: sdl_prep mydesign 
          sdl_prep mydesign -verbose
	  sdl_prep me_design -0.8

EOF

help_ret=1
return_code
}

#################################################################
# function: return_code
#################################################################

return_code() {

  if [ $ret -ne 0 ]; then
    if [ $program = "none" ] ; then
        echo "Errors found."
	echo "Check sdl_prep.log file."
    else
        echo "Errors found while running $program."
	echo "Check sdl_prep.log file."
    fi
  fi

  rm -f $mmgc_wd/.retcode
  echo $ret > $mmgc_wd/.retcode
  MGC_WD=$old_mgc_wd
  export MGC_WD
  echo "sdl_prep ended with return code " $ret
  echo "Done."
  exit $ret
}


###################################################################

rm_run() {

  program="rm"
  cmd="rm -f $1"

  echo "$cmd"
  echo "$cmd" >> $log_file 2>&1
  $cmd >> $log_file 2>&1

  ret=$?
  if [ $ret -ne 0 ]; then
    return_code
  fi

}

#################################################################
# start main shell
#################################################################

cat <<EOS

                   sdl_prep Version $version


EOS

#################################################################
# init parameters
#################################################################

# Set Environment Variable for execution  
# 2-18-99 edited by C.Dovrolis for UW-CS installation 
# MGC_HOME=/afs/cs.wisc.edu/s/mentor-c.1-2
export MGC_HOME

# Begin Variables

# Value of Lamda
lambda=0.4
verbose_opt="false"
help_ret=0
ret=0
err_ret=0
err_opt=" "

# End Variables

if [ $# -lt 1 ] ; then
  help_ret=1
fi


#################################################################
# scan off command line arguments
#################################################################

while [ $# -gt 0 ]; do

  if [ null$1 = "null" ]; then
    shift
  elif [ $1 = "-verbose" ]; then
    verbose_opt="true"
    shift
  elif [ $1 = "-VERBOSE" ]; then
    verbose_opt="true"
    shift
  elif [ $1 = "-h" ]; then
    help_ret=1
    shift
  elif [ $1 = "-H" ]; then
    help_ret=1
    shift
  elif [ $1 = "-help" ]; then
    help_ret=1
    shift
  elif [ $1 = "-HELP" ]; then
    help_ret=1
    shift
  elif [ $1 = "2.0" ]; then
    lambda=1
    shift
  elif [ $1 = "1.2" ]; then
    lambda=0.6
    shift
  elif [ $1 = "0.8" ]; then
    lambda=0.4
    shift
  elif [ $1 = "0.6" ]; then
    lambda=0.3
    shift
  elif [ `echo $1 | cut -c1` = "-" ]; then
  	err_msg="Error: Illegal option specified on the command line:  "
	err_opt=$1
        err_ret=1
        shift
  else
    design=$1
    shift
  fi

done


#################################################################
# strip off leading directory hierarchy 
# use leaf for -of argument
#################################################################

tmp_design=`echo $design | sed -e 's|\(.*\)/\(.*\)|\1:\2|'`
head=`echo $tmp_design | cut -d: -f1`
leaf=`echo $tmp_design | cut -d: -f2`
if [ $head = $leaf ]; then
  head=`pwd | sed -e "s./tmp_mnt.."`
fi
if [ null$head = "null" ]; then
  head=`pwd | sed -e "s./tmp_mnt.."`
fi

###################################################################
# Reset MGC_WD to directory containing design...
###################################################################

old_mgc_wd=${MGC_WD}
MGC_WD=$head
export MGC_WD
mmgc_wd=`$MGC_HOME/bin/get_hard_name -s $MGC_WD`
echo "MGC_WD = " $MGC_WD
log_file=${mmgc_wd}/sdl_prep.log

###################################################################
# create log file sdl_prep.log...
###################################################################

program="rm"
rm -f $log_file
ret=$?
if [ ! $ret -eq 0 ]; then
  return_code
fi

program="touch"
touch $log_file
ret=$?
if [ ! $ret -eq 0 ]; then
  return_code
fi


#################################################################
# check env variables
#################################################################


if [ null$MGC_WD = "null" ]; then
  echo "Error: MGC_WD variable needs to be set to design directory"
  echo "Error: MGC_WD variable needs to be set to design directory" >> $log_file 2>&1
  ret=1
  help
fi


if [ ! -d $mmgc_wd ]; then
  echo "Error: MGC_WD = $mmgc_wd , directory not found."
  echo "Error: MGC_WD = $mmgc_wd , directory not found." >> $log_file 2>&1
  ret=1
  help
fi

if [ null$MGC_HOME = "null" ]; then
  echo "Error: MGC_HOME needs to be set to the MGC_HOME tree."
  echo "Error: MGC_HOME needs to be set to the MGC_HOME tree." >> $log_file 2>&1
  echo ""
  ret=1
  help
fi

if [ ! -d $MGC_HOME ]; then
  echo "Error: MGC_HOME = $MGC_HOME , directory not found!"
  echo "Error: MGC_HOME = $MGC_HOME , directory not found!" >> $log_file 2>&1
  echo ""
  ret=1
  help
fi


#################################################################
# check command line options and arguments
#################################################################

if [ null$design = "null" ]; then
  echo "Error: <design> not specified on command line"
  echo "Error: <design> not specified on command line" >> $log_file 2>&1
  ret=1
  help
fi

if [ ! -d $design ]; then
  echo "Error: <design> = $design , directory not found!"
  echo "Error: <design> = $design , directory not found!" >> $log_file 2>&1
  echo ""
  ret=1
  help
fi


###################################################################
# function: if verbose option is specified then start the tail
#           process.
# input: sdl_prep.log
# output:
###################################################################

if [ $verbose_opt = "true" ]; then

	echo "tail -f " $log_file " &" >> $log_file 2>&1
	tail -f $log_file &

	trap return_code 2
	tail_flag="true"
fi


########################  Main Design Flow ########################


########################  dve  ####################################


###################################################################
# function: run dve on $design
# condition: dve needs to be run only once.
#            If $design/sdl.Eddm_design_viewpoint.attr file exists,
#            then don't run dve.
# input: $design
# output: $design/sdl.Eddm_design_viewpoint.attr
###################################################################

if [ ! -f ${design}/sdl.Eddm_design_viewpoint.attr ]; then

  program="dve"
  echo "dve $design"
  echo "dve $design" >> $log_file 2>&1

dve $design -nodisplay <<!
  dofile \$MGC_HOME/mgc_hep/userware/dve/sdl.setup
  \$add_parameter("lambda", "${lambda}e-6", @number);
  \$save_design_viewpoint("sdl", @nolock, @nocleanup);
  \$close_session(@discard);
!
  
  ret=$?
  if [ $ret -ne 0 ]; then
    return_code
  fi

  grep "Error:" $log_file | grep "//" > /dev/null
  ret=$?
  if [ $ret -eq 0 ] ; then
    ret=1
    return_code
  fi

fi

###################################################################
