#!/bin/sh
# Fires up magic to stream in gds and save as .mag
# also copies standard cells into directory
#
# Johannes Grad, OSU

# Customize this script here:

osucells_dir=$OSUcells/lib

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

sourceFile=final.gds2
/bin/echo "Checking if $sourceFile exists........\c"
if (test ! -f $sourceFile ) then
    echo "FAILED"
    echo $sourceFile does not exist!
    echo Please run Encounter first
    exit 127
fi
echo "OK"

sourceFile=encounter.conf
/bin/echo "Checking if $sourceFile exists....\c"
if (test ! -f $sourceFile ) then
    echo "FAILED"
    echo $sourceFile does not exist!
    echo Sorry, file is necessary to determine top-level name 
    exit 127
fi
echo "OK"

/bin/echo "Determining top-level name...........\c"
toplevel=`grep "set my_toplevel" encounter.conf | awk '{print $3;}'`
echo "OK ($toplevel)"

/bin/echo "Determining Technology...............\c"
leffile=`grep "leffile" encounter.conf | awk '-F"' '{print $2;}' | awk -F/ '{print $NF}'`
if (test $leffile = "osu025_stdcells.lef") then
    echo "OK (TSMC 0.25um osu_cells)"
    techfile=tsmc025
    sourceDirMagic=$osucells_dir/source/magic
    sourceDirPads="noPads"
    sourceDirSue="$osucells_dir/tsmc025/sue"
elif (test $leffile = "osu018_stdcells.lef") then
    echo "OK (TSMC 0.18um)"
    techfile=tsmc018
    sourceDirMagic=$osucells_dir/source/magic
    sourceDirPads="noPads"
    sourceDirSue="$osucells_dir/tsmc018/sue"
elif (test $leffile = "osu035_stdcells.lef") then
    echo "OK (AMI 0.35um)"
    techfile=ami035
    sourceDirMagic=$osucells_dir/source/magic
    sourceDirPads="$osucells_dir/source/pads_ami035"
    sourceDirSue="$osucells_dir/ami035/sue"
elif (test $leffile = "osu05_stdcells.lef") then
    echo "OK (AMI 0.5um)"
    techfile=ami05
    sourceDirMagic=$osucells_dir/source/magic
    sourceDirPads="$osucells_dir/source/pads_ami05"
    sourceDirSue="$osucells_dir/ami05/sue"
elif (test $leffile = "osu05_stdcells.stacks.lef") then
    echo "OK (AMI 0.5um stacks)"
    techfile=ami05
    sourceDirMagic=$osucells_dir/source/magic
    sourceDirPads="$osucells_dir/source/pads_ami05"
    sourceDirSue="$osucells_dir/ami05/sue"
else
    echo "FAILED"
    echo "Unknown .lef file used for P&R. Sorry."
    exit 1
fi

/bin/rm -rf magic
mkdir magic
cp final.gds2 magic/final.strm 
cd magic

/bin/echo "Importing standard cells.............\c"
/bin/cp $sourceDirMagic/*.mag .
echo "OK"

if (test $sourceDirPads != "noPads") then 
  /bin/echo "Importing Pads.......................\c"
  /bin/cp $sourceDirPads/*.mag .
  echo "OK"
fi

/bin/echo "Converting from gds to mag...........\c"
(
ls -1 *.mag | awk -F. '{printf "load %s\n",$1}';
echo calma read final; 
echo writeall force; 
echo quit; 
echo yes) | magic -noc -T $techfile -d NULL  > NULL
#echo yes) | magic -noc -T $techfile -d NULL > NULL 2> magic.log
echo "OK"

 
/bin/rm -f NULL
/bin/rm -f final.strm
cd ..

if (test $sourceDirSue != "noSue") then 
  /bin/rm -rf sue
  mkdir sue
  grep -v "PADFC" final.v | grep -v "PADNC" | grep -v "PADGND" | grep -v "PADVDD" | sed "s/1'b1/vdd/g" | sed "s/1'b0/gnd/g" > sue/final.v
  cd sue

  /bin/echo "Importing standard cell schematics...\c"
  cp $sourceDirSue/*.sue .
  echo "OK"

  /bin/echo "Creating Sue schematic...............\c"
  v2sue -y 300 final.v
  cd ..
fi

echo "Good by."

