#! /bin/csh

set spice = ~cad/bin/spice

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

cat $1 ckt.spi > tmp.spi

ed - <<end tmp.spi >& /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
