#!//usr/bin/ksh

# OPTIND=0   # modified: R.B. 18-FEB-93: getopts fails if OPTIND<>0
cmd=`basename $0`
usage="usage: $cmd [-inhs] crafile "
debug=0
install=0
psuppress=0
devflg=''
allopt=$@

while getopts nihsDO xopt ; do
    case $xopt in
    i) install=1;;
    n) devflg='-n';;
    O) devflg='-o';;
    h) # help message:
	 echo $cmd program makes fortran file from the patchy cradle given in the argument.
	 echo 'The cradle "crafile.cra" can be specified with or without the .cra extention.'
	 echo 'The cradle should specify all the pam files neccessary for ypatchy execution.'
	 echo 'The pam files should be specified like     +PAM,11,T=A. ROPE312 '
	 echo 'if the +pam spec contains full path names (e.g.  /u/ws/opal/pro/pam/rope312)'
	 echo no subsitution is performed and an attempt will be done to find the file.
	 echo The +USE,IBM and/or +USE,VAX switches are mapped to +USE,APOLLO.
	 echo 'The fortran output is named after the first argument (e.g. crafile.f)'
	 echo 'and it is created in the local directory.'
	 echo
	 echo 'The directories in propath are searched to find the pam or car files specified'
	 echo \(see the whicho command for details\).
	 echo
	 echo switches:
	 echo -h  this help
	 echo -i  install the fortran file in the src directory \(propath\), if it exists
	 echo '-n  use the devpath for pam file search and instalation \(def: propath\)'
	 echo '-O  use the oldpath for pam file search and instalation \(def: propath\)'
	 echo '-s  suppress patchy replacement flag'
	 echo '------------------------------------------------------------------------'
	 exit 9;;
    D) debug=1 ;;
    s) psuppress=1 ;; #suppress patchy replacement
    \?) echo unknown option $xopt; echo $usage; exit 9;;
    esac
done
shift `expr $OPTIND - 1 `

if [ $# -le 0 ]; then
    echo $usage
    exit 9
fi

#log=`date`" $USER $cmd $allopt" ;  echo $log | tee -a '/tmp/.'$cmd'.log'

patchyflag='decs,unix'
YPATCHY='nypatchy'
if [ -z "$HARDWARE" ]; then HARDWARE="`machine`"; fi
if [ $HARDWARE = 'mips' ]; then patchyflag='APOLLO,SGI'; fi
if [ $HARDWARE = 'decs' ]; then patchyflag='DECS,UNIX'; fi
if [ $HARDWARE = 'pentium' ]; then patchyflag='PCLINUX,UNIX'; fi
if [ $HARDWARE = 'R6000' ]; then patchyflag='IBMRT'; YPATCHY='ypatchy'; fi
if [ $HARDWARE = 'RS6000' ]; then patchyflag='IBMRT'; YPATCHY='ypatchy'; fi
if [ $HARDWARE = 'apollo' ]; then patchyflag='APOLLO'; fi

curdir=`pwd`
tmpdir=${TMPDIR:-/tmp}/ytofort$$

if [ $debug = 1 ]; then echo curdir = $curdir tmpdir = $tmpdir; fi
#
#  locate the cradle in the search path
fcra=`basename $1 '.cra'`
pcra=`dirname $1`
crafile=${pcra}/${fcra}.cra
if [ ! -f $crafile ]; then
	if [ $pcra != '.' ]; then echo file $1 not found. Exit.; exit 1; fi
	xf=`whicho $devflg -i -s .cra $fcra`
	if [ $? != 0 ]; then echo file $1 not found. Exit.; exit 1; fi
	echo found file $1 as $xf;
	fcra=`basename $xf '.cra'`
	pcra=`dirname $xf`
	crafile=${pcra}/${fcra}.cra
fi
ccra=$tmpdir/$fcra
tcra=$ccra.cra

mkdir $tmpdir

trap '/bin/rm -r -f $tmpdir 2>/dev/null; echo Interrupt! ; stat=1; exit 2' 1 2 15

      if [ $debug = 1 ]; then echo copy $crafile to $tcra; fi
#	sed '/+pam/s/\. */\. /;/+PAM/s/\. */\. /' <$crafile >$tcra  #put space after .
#	cp $crafile $tcra 
	# don't add suffix to names ending with !
	gawk '{ if ($0 ~ /^\+PAM|^\+pam/ ){ n=sub(/T=ATTACH|T=A/,"T=ATTACH,CARDS") };
		print $0;
		} ' $crafile > $tcra

	pams=`gawk -F',' '/^\+PAM|^\+pam/ {	
		xf=substr($NF,index($NF,".")+1);   # get the comment field
		n=split(xf,f," ");   
		if(n == 0){ next; } # no file information 
		fn=f[n];
		k=split(fn,g,"."); 
		if(k > 1){ printf "%s ",fn; next; }
		m=split(fn,h,"!");
		if(m > 1){ printf "%s.",h[1]; next; }

		if(fn ~ /pam|PAM/ && n > 1){ fn=f[n-1]; printf "%s.pam ",fn; next; }
		if(fn ~ /car|CAR/ && n > 1){ fn=f[n-1]; printf "%s.car ",fn; next; }

		s=".pam"; 
		optflag=0;
		for(i=2;i<=NF;i++){ 
			peq=index($i,"=");
			if(peq != 0){
				topt=substr($i,1,peq-1);
				if(topt ~ /T|t/ ){ $i=substr($i,peq+1,1); optflag=1; }
				else optflag=0;
			}
			if(optflag == 1){
				x=substr($i,1,1)
				if(x == "c" || x == "C"){ s=".car"; break;}
			}
		}
		printf "%s%s ",fn,s; }' $tcra `
#
#	end gawk
#
	if [ -z "$pams" ]; then 
		echo Warning: cradle does not have pam file information. 
	else
		if [ $debug = 1 ]; then echo pams = $pams; fi
#OB#061099		pams=`echo $pams | tr "[A-Z]" "[a-z]"`

		for xpam in $pams; do #find the pam file
			found=0
			suffix=`echo $xpam | gawk -F"." '{print $NF}'`
			filpam=`basename $xpam ".$suffix"`
			dirpam=`dirname $xpam`
			if [ "$dirpam" != . ]; then continue; fi  # absolute load path is specified
			if [ -n "$suffix" ]; then wsuffix='-s '$suffix; else wsuffix=''; fi
			if [ $debug = 1 ]; then echo "opam=whicho $devflg -i $wsuffix $filpam"; fi
			opam=`whicho $devflg -i $wsuffix $filpam`
                        Stat=$?
			if [ $Stat != 0 ]; then
                                opam=`echo $filpam | sed 's/[0-9]*$//g'`;
			        opam=`whicho $devflg -i $wsuffix $opam`
                                Stat=$?;
                        fi
			if [ $Stat != 0 ]; then
				echo pam file \($dirpam/\)$filpam not found. exit
				/bin/rm -r -f $tmpdir
				exit 3
			else
				echo found $filpam.$suffix as $opam
				/bin/ln -s $opam $tmpdir/$filpam.$suffix
			fi
		done
	fi
#
cd $tmpdir
	if [ $debug = 1 ]; then echo in $tmpdir:; pwd; ls -l; fi
	if [ $psuppress = 0 ]; then
	sed  "/+use/s/ibm/$patchyflag/g;/+USE/s/ibm/$patchyflag/g;
		/+use/s/IBM/$patchyflag/g;/+USE/s/IBM/$patchyflag/g;
     		/+use/s/vax/$patchyflag/g;/+USE/s/vax/$patchyflag/g;
		/+use/s/VAX/$patchyflag/g;/+USE/s/VAX/$patchyflag/g;
     		/+use/s/hpux/$patchyflag/g;/+USE/s/hpux/$patchyflag/g;
		/+use/s/HPUX/$patchyflag/g;/+USE/s/HPUX/$patchyflag/g" <$tcra >${tcra}_ 
	mv ${tcra}_ $tcra
	fi
   if [ $debug = 1 ]; then echo $YPATCHY - $tcra $tcra $tcra .go ; fi
   $YPATCHY - $ccra $ccra $ccra .go 
   if [ $debug = 1 ]; then echo $YPATCHY status = $?; fi
   if [ ! -s $ccra.lis ]; then 
       echo "$YPATCHY ran out of tmp-disk space -- exit"
       /bin/rm -r -f $tmpdir
       exit 5 
   fi
cd $curdir
   mv $ccra.lis $fcra.lis
   tail -18 $fcra.lis
   if (tail -18 $fcra.lis | fgrep '**********' >/dev/null ); then
	 echo patchy error;
       grep -i 'missing' $fcra.lis
	/bin/rm -r -f $tmpdir
	 exit 4
   fi
if [ $HARDWARE = 'mips'  ] || [ $HARDWARE = 'decs' ] || [ $HARDWARE = 'alpha' ] || [ $HARDWARE = 'pentium' ]; then
      mv $ccra.f $fcra.f
#	sed '1,$s/16#/\$/g' <$ccra.f >$fcra.f
#	sed '1,$s/\\/\\\\/g;1,$s/16#/\$/g' <$ccra.f >$fcra.f
elif [ $HARDWARE = 'RS6000' ]; then
        sed '1,$s/\\/\\\\/g' <$ccra.f >$fcra.f
        rm $ccra.f
else
      mv $ccra.ftn $fcra.f
fi
if [ $install = 1 ]; then
	instpath=`whicho $devflg -i -d src`
	if [ $? = 0 ]; then
		mv $fcra.f $instpath/$fcra.f
		echo $fcra.f is installed in $instpath
	fi
fi
/bin/rm -r -f $tmpdir
exit 0
