#!/bin/bash
#set -x -v 

# MW. Lip / Param parsing program for spice

# -n normal, -f full (keep everything), -r replace original file

export TMPLP=/tmp/LibPrm.$$-

function trapper()
    {
     echo User break! 
     echo Exiting . . .
     rm -f -v ${TMPLP}*
     unset TMPLP 
     exit 1
    }
    
trap trapper SIGINT SIGQUIT 

function operror()
    {
     echo Incorrect parameters: $*, $#
     unset TMPLP 
     exit 2
    }

function repnormpl()
    {
     mslib $1 ${TMPLP}1
     sed -n -e 'p' -e "1r ${TMPLP}1" $1 >${TMPLP}2
     spiceprm ${TMPLP}2 $2
    }

function keepall()
    {
     mslib $1
     sed -n -e 'p' -e "1r $1.lib" $1 >${TMPLP}2
     spiceprm ${TMPLP}2 $2
    }
    

# Main body
if test $# -lt 2 -o $# -gt 3; then operror $*; fi

case $1$# in
-r3) operror $*;;
-n2) repnormpl $2 ${2%.cir}.ckt;;
-n3) repnormpl $2 $3;;
-r2) repnormpl $2 $2;;
-f2) keepall $2 ${2%.cir}.ckt;;
-f3) keepall $2 $3;;
esac

rm -f ${TMPLP}*
unset TMPLP 
exit 0

