#! /bin/csh

set spice = /spice3f4/bin/spice3

if( $#argv != 2 ) then
	echo "usage: getres model_file dev_file"
	exit 1
endif

if( ! -f $1 ) then
	echo "can't open spice model file '"$1"'"
	exit 1
endif

if( -f $argv[2] ) then
	source $argv[2]
else
	echo "can't open device configuratin file '"$argv[2]"'"
	exit 1
endif

set nmos = `grep -i "\.MODEL" $1 | grep -i NMOS`
set pmos = `grep -i "\.MODEL" $1 | grep -i PMOS`

if( $#nmos == 0 ) then
	echo "can't find nmos .MODEL card in '"$1"'"
	exit 1
endif
if( $#pmos == 0 ) then
	echo "can't find pmos .MODEL card in '"$1"'"
	exit 1
endif

echo "** SPICE file created by getres "  > tmp.spi
cat $1 >> tmp.spi
echo "** " >>  tmp.spi
cat ckt.spi >> tmp.spi

#ed - <<end tmp.spi >& /dev/null  

/bin/ed  tmp.spi   <<end  >& /dev/null
g/N_FET/s/N_FET/$nmos[2]/g
g/P_FET/s/P_FET/$pmos[2]/g
g/C_LOAD/s/C_LOAD/$cap/g
g/N_WITH/s/N_WITH/$nwith/g
g/N_LEN/s/N_LEN/$nlen/g
g/P_WITH/s/P_WITH/$pwith/g
g/P_LEN/s/P_LEN/$plen/g
w
q
end


$spice -n -b -r spi.out tmp.spi

if( $status != 0 ) then
	echo "something went wrong with spice, look at 'tmp.spi'"
	exit 1
endif

./findr -c $cap -n $nwith,$nlen -p $pwith,$plen spi.out |& tee resists
