#!/bin/sh

if (test ${#} -ne 2) then
 echo "Error: Expecting 2 Parameters"
 echo ""
 echo "Usage:"
 echo "osucells_ext2sim filename lambda"
 echo ""
 echo "Example:"
 echo "osucells_ext2sim nrd_05 0.3"
 echo ""
 exit 127
fi

sourceFile=$1.mag
if (test ! -f $sourceFile ) then
    echo "FAILED"
    echo $sourceFile does not exist!
    exit 127
fi
echo "Found $sourceFile"

sourceFile=$1.ext
if (test ! -f $sourceFile ) then
    echo "FAILED"
    echo $sourceFile does not exist!
    exit 127
fi
echo "Found $sourceFile"

layer=`grep "label" $1.mag | grep "gnd" |awk '{print $2}'`
if (test $layer = "space") then
	echo "GND label is floating!"
	echo "Please check your magic layout."
        exit 127
fi
echo "GND label is okay"

layer=`grep "label" $1.mag | grep "vdd" |awk '{print $2}'`
if (test $layer = "space") then
	echo "VDD label is floating!"
	echo "Please check your magic layout."
        exit 127
fi
echo "VDD label is okay"

/usr/bin/rm -rf $1.sim

osucells_polyresfix *ext
ext2sim $1
mv $1.sim $1.sim.orig
echo "Created sim file"

osucells_scalesim -v "l=$2" $1.sim.orig > $1.sim
echo "Scaled sim file"

grep -v '^R' $1.sim | grep -v '^C' > $1_noRC.sim
echo "Created _noRC sim file".

/usr/bin/rm -rf $1.sim.orig

echo "Done!"

