#!/bin/@SHL@
#created by configure from hpfc.in
#written by Zheng Gengbin

function usage
{
echo 'Usage:  hpfc [-r] [-i] [-O] [-s] [-c] [-d] [-D] [--prefetch]'
echo '    [-o obj] [-I include_path] [-L library_path] [-l library] [-S in-core-size]'
echo '    [-M memory] [-R record-size] [-n processor-number] [-m processor-dimension]'
echo '    [--proc processor-number] [--f90] [--profile] [--major=r|c] [-io=c|f]'
echo "    [--pcrcf] [--fixed] [--clear] [--silent] [--tof77] "
echo "    [--fe] \'file1.f\' \'file2.f\' ..."
echo 'options: '
echo '-r)       	run the node program'
echo '-i )      	ignore the error of last pass.'
echo '-O )      	flag to enable optimize'
echo '--pcrcf)   	compile the node program'
echo '--fixed )  	fixed format of fortran '
echo '--clear )  	clear the intermediate files '
echo '--silent)  	clear the intermediate files '
echo '-I )      	specify include path'
echo '-L )      	specify library path'
echo '-l )      	specify library '
echo '--tof77)   	add tof77 pass '
echo '--fe )     	only frontend are performed.'
echo '-s )      	serialize HPF to F77 program for test.'
echo '-o )      	specify the output file'
echo '-c )      	compile only , donot link'
echo '-d1 )     	hpf2dep debug flag'
echo '-d2 )     	hpfsc  debug flag'
echo '-d3 )     	trans0  debug flag'
echo '-S )      	specify IN CORE ARRAY size'
echo '-M )      	specify dynamic stack size'
echo '-R )      	specify fortran file record size'
echo '-D )      	enable the runtime debug.'
echo '-n )      	specify the number of processor'
echo '-m )      	specify the dimension of processor'
echo '--proc)    	specify the processor number when call mpirun'
echo '--f90)     	unparse f90 output, instead of f77'
echo '--profile)    	runtime profiling enabled.'
echo '--major=[r|c] 	specify the storage pattern.'
echo '--io=[c|f] 	specify the IO mode (C style or Fortran style).'
echo '--prefetch)       enable prefetch io'
}

function exist
{
if [ -x $HPFHOME/src/$exec/$exec ]
then
	path=$HPFHOME/src/$exec/$exec
	test -z "$incpath" && incpath="-I$ADLIBHOME/src/pcrcFortran"
elif [ -x $HPFHOME/src/tools/$exec/$exec ]
then
	path=$HPFHOME/src/tools/$exec/$exec
elif [ -x $bindir/$exec ]
then
	path=$bindir/$exec
        test -z "$incpath" && incpath="-I$includedir/"
else
	echo "$exec not found !"
	exit 1
fi
}

function exist_adlib
{
if [ -x $ADLIBHOME/src/script/$exec ]
then
	path=$ADLIBHOME/src/script/$exec
elif [ -x $bindir/$exec ]
then
	path=$bindir/$exec
else
	echo "$exec not found !" &&  exit 1
fi
}

function clearall
{
#clear up
if test $clear_flag = 1
then
/bin/rm -f $alldeps $depfile $bdfile $next0 $passfile $gfile $lstf $alllsts
fi
}

trap 'clearall; exit 1'  1 2 3 9 15 19

prefix=@prefix@
exec_prefix=@exec_prefix@
bindir=@bindir@
includedir=@includedir@
HPFHOME=@HPFHOME@
ADLIBHOME=@ADLIBHOME@

if [ $# -lt 1 ]
then
	usage
        exit 1
fi

incpath=
hpf2dep_flag=
hpfsc_flag=
fe_flag=0
trans0_flag=
trans1_flag=
ignore_flag=0
add_tof77=1		# add tof77 by default
tof77_flag=
pcrcf_flag=
need_pcrcf=0
run_flag=0
proc_number=4
clear_flag=0
seq_flag=0
libpath=
libfile=
proj_name=
separate_compile=0
unparse_flag='-f77'
objfilelist=

#decide the path
exec=hpf2dep ;  exist ; hpf2dep=$path
exec=hpfsc ;  exist ; hpfsc=$path
exec=trans2 ;  exist ; trans2=$path
exec=trans1 ;  exist ; trans1=$path
exec=tof77 ;  exist ; tof77=$path
exec=trans0 ;  exist ; trans0=$path
exec=unparse;  exist ; unparse=$path

exec=pcrcf;  exist_adlib ; pcrcf=$path
exec=prun;  exist_adlib ; prun=$path

# parse the flag
for ac_option
do
	case "$ac_option" in
	--fixed )  hpf2dep_flag=${hpf2dep_flag}" -fixed" ; shift   ;;
	--clear)   clear_flag=1; shift;;
	-o*)      test $1 = '-o' && proj_name=$2 && shift || proj_name=`echo $1 | sed 's/^-o//'`; shift ;;
	-c )      hpfsc_flag=${hpfsc_flag}" "$1 ; separate_compile=1; pcrcf_flag=${pcrcf_flag}" "$1; shift ;;
	-d1 )     hpf2dep_flag=${hpf2dep_flag}" -d"; shift ;;
	-d2 )	  hpfsc_flag=${hpfsc_flag}" -d" ;  shift;;
	-d3 )	  trans0_flag=${trans0_flag}" -d" ; shift ;;
	-I*)      test $1 = '-I' && incpath=${incpath}" "$1" "$2 && shift || incpath=${incpath}" "$1 ; shift ;;
	-L*)      test x$1 = 'x-L' && libpath=${libpath}" "$1" "$2 && shift || libpath=${libpath}" "$1 ; shift ;;
	-l*)      test x$1 = 'x-l' && libfile=${libfile}" "$1" "$2 && shift || libfile=${libfile}" "$1 ; shift ;;
	-i )	  ignore_flag=1 ; shift ;;
	-s )      seq_flag=1; clear_flag=1; add_tof77=1; tof77_flag='-s'; shift ;;
        -p )      trans0_flag=${trans0_flag}" "$1 ; shift ;;
        -t )      trans0_flag=${trans0_flag}" "$1 ; shift ;;
        -D )      trans0_flag=${trans0_flag}" "$1 ; shift ;;
        --major=*) trans0_flag=${trans0_flag}" "$1 ; shift ;;
        --io=*)   trans0_flag=${trans0_flag}" "$1 ; shift ;;
        --prefetch) trans0_flag=${trans0_flag}" "$1 ; shift ;; 
        -S*)      test $1 = '-S' && trans0_flag=${trans0_flag}" "$1" "$2 && shift || trans0_flag=${trans0_flag}" "$1 ; shift ;;
        -M*)      test $1 = '-M' && trans0_flag=${trans0_flag}" "$1" "$2 && shift || trans0_flag=${trans0_flag}" "$1 ; shift ;;
        -R*)      test $1 = '-R' && trans0_flag=${trans0_flag}" "$1" "$2 && shift || trans0_flag=${trans0_flag}" "$1 ; shift ;;
        -O*)      trans0_flag=${trans0_flag}" "$1 ; 
		  pcrcf_flag=${pcrcf_flag}" -O" ; shift ;;
        -n*)      test $1 = '-n' && trans1_flag=${trans1_flag}" "$1" "$2 && shift || trans1_flag=${trans1_flag}" "$1 ; shift ;;
        -m*)      test $1 = '-m' && trans1_flag=${trans1_flag}" "$1" "$2 && shift || trans1_flag=${trans1_flag}" "$1 ; shift ;;
	--tof77)   add_tof77=1; shift ;;
	--pcrcf)   need_pcrcf=1; shift ;;
	-r)	  run_flag=1; shift;;
	--fe)	  fe_flag=1; unparse_flag= ;shift;;
	--proc)    proc_number=$2; shift ;shift;;
	--f90)     unparse_flag= ; shift;;
	--profile) pcrcf_flag=${pcrcf_flag}" "$1 ; shift ;;
	--silent)  trans0_flag=${trans0_flag}" -z"; trans1_flag=${trans1_flag}" -z"; trans2_flag=${trans2_flag}" -z"; tof77_flag=${tof77_flag}" -z"; shift ;;
	-*)	  echo "Unknown option: $1" ; exit 1 ;;
	esac
done

if [ $# -lt 1 ]
then
	usage
        exit 1
fi

#proj_name default to be the first name in file lists.
if [ -z "$proj_name" ]
then
	proj_name=`echo $1 | sed 's/\.f.*$/\.dep/' `
fi
#normalize proj_name if no suffix
proj_name=`echo $proj_name | sed '/\./!s/$/\.dep/' `
hpfsc_flag=${hpfsc_flag}" -o "$proj_name

# record all the .dep files and .lst files names
alldeps=
alllsts=

for ac_option
do

suffix=`echo $1 | @AWK@ ' BEGIN {FS="."} {print $NF } '`
if test x$suffix = x'a' || test x$suffix = x'o' 
then
objfilelist=${objfilelist}' '$1
shift; continue
fi

depfile=`echo $1 | sed 's/\.[^.]*$/\.dep/' `
alldeps=${alldeps}" "$depfile
lstf=`echo $1 | sed '/\./s/\.[^.]*$/\.lst/' `
alllsts=${alllsts}" "$lstf

if [ ! -f $1 ]
then
        echo  Error: "$1" not exist !
        exit 1
fi

if [ $1 = $depfile ]
then
	echo Invalide file name "$1"
	exit 1
fi

echo
echo --------  HPF hpf2dep "$1" ----------
        	echo "++ $hpf2dep  $hpf2dep_flag $incpath $1 ++"
        	$hpf2dep $hpf2dep_flag $incpath $1

status=$?
if test $ignore_flag = 0
then
	test $status -ne 0  && clearall && exit 1
fi

shift 

done

# if objfilelist is not zero, means we also have objfiles to link.
test x"$objfilelist" != 'x' && hpfsc_flag=$hpfsc_flag' -c'

set `echo $proj_name | sed 's/\.dep$/\.f/' `

depfile=`echo $1 | sed 's/\.[^.]*$/\.dep/' `
bdfile=`echo $1 | sed 's/\.[^.]*$/_2\.dep/' `
bdf=`echo $1 | sed '/\./s/\.[^.]*$/_2\.f/' `
passfile=`echo $1 | sed '/\./s/\.[^.]*$/_1\.dep/' `
passf=`echo $1 | sed '/\./s/\.[^.]*$/_1\.f/' `
gfile=`echo $1 | sed '/\./s/\.[^.]*$/_0\.dep/' `
tof77f=`echo $1 | sed '/\./s/\.[^.]*$/_r\.dep/' `
tof77src=`echo $1 | sed '/\./s/\.[^.]*$/_r\.f/' `
if test x"$alldeps" != x
then
f77f=`echo $1 | sed '/\./s/\.[^.]*$/f\.f/' `
fi
f77frec=`echo $1 | sed '/\./s/\.[^.]*$/f\.f.rec/' `
if test $separate_compile = 0
then
runable=`echo $1 | sed '/\./s/\.[^.]*$//' `
else
runable=`echo $1 | sed '/\./s/\.[^.]*$/\.o/' `
fi
seqf=`echo $1 | sed '/\./s/\.[^.]*$/\_s.f/' `

if test x"$alldeps" != x
then
echo
echo -------- HPF hpfsc "$alldeps" ----------
	for afile in $alldeps
	do
	  if [ ! -f $afile ]
	  then
		echo  $afile not found !
		clearall
		exit 1
	  fi
	done
	echo "++ $hpfsc $hpfsc_flag $alldeps ++"
	$hpfsc $hpfsc_flag $alldeps
	

status=$?
if test $ignore_flag = 0
then
	test $status -ne 0  && clearall && exit 1
fi

fi   # end of hpfsc

# only frontend
if test $fe_flag = 1
then
	$unparse $unparse_flag $depfile > $f77f
	clearall ; exit 0
fi

if test x"$alldeps" != x
then
echo
echo -------- HPF Trans2 $depfile ----------
	if [ -f $depfile ]
	then
		echo "++ $trans2 $depfile ++"
		$trans2 $trans2_flag $depfile
	else
		echo  $depfile not found !
		clearall
		exit 1
	fi

status=$?
if test $ignore_flag = 0
then
	test $status -ne 0  && clearall && exit 1
fi

test $clear_flag = 0 && $unparse $bdfile >  $bdf

fi  # end of trans2

if test x"$alldeps" != x
then
echo
echo -------- HPF Trans1 $bdfile ----------
	if [ -f $bdfile ]
	then
		echo "++ $trans1 $trans1_flag $bdfile ++"
		$trans1 $trans1_flag $bdfile
	else
		echo  $bdfile not found !
		clearall
		exit 1
	fi

status=$?
if test $ignore_flag = 0
then
	test $status -ne 0  && clearall && exit 1
fi
test $clear_flag = 0 && $unparse $passfile >  $passf

fi     # end of trans1


if test x"$alldeps" != x
then
if test $add_tof77 -eq  1 
then
echo
echo -------- HPF tof77 $passfile ----------
	if [ -f $passfile ]
	then
		echo "++ $tof77 $tof77_flag $passfile ++"
		$tof77 $tof77_flag $passfile
		test $? -ne 0 && clearall && exit 1
		test $clear_flag = 0 && $unparse $tof77f > $tof77src
		next0=$tof77f
	else
		echo "Abnormal!"
		clearall
		exit 1
	fi
else
	next0=$passfile
fi

fi  # end of tof77

#make serialized F77 program
if test $seq_flag = 1
then
	$unparse $unparse_flag $tof77f > $seqf && echo && echo "+++ Serialized F77 program - $seqf generated +++" && echo
	clearall
	exit 0
fi

if test x"$alldeps" != x
then
echo
echo -------- HPF Trans0 $next0 ----------
	if [ -f $passfile ]
	then
#		echo ++++++++++ Trans0 ++++++++++
		echo "++ $trans0 $trans0_flag $next0 ++"
		$trans0   $trans0_flag  $next0
		test $? -ne 0 && clearall && exit 1
	else
		echo  hpfc:error: $passfile not found !
		exit 1
	fi

$unparse $unparse_flag $gfile > $f77f

fi #end of trans0

if test $run_flag = 1 &&  test $need_pcrcf = 0
then
	need_pcrcf=1
fi

# pcrcf
if test $need_pcrcf = 1 
then
	echo
	echo ---------- HPF pcrcf $f77f ----------
	echo "++ $pcrcf $pcrcf_flag -o $runable $libpath $libfile $f77f $objfilelist ++"
	$pcrcf $pcrcf_flag -o $runable $libpath $libfile $f77f $objfilelist
fi

status=$?
if test $ignore_flag = 0
then
	test $status -ne 0  && clearall && exit 1
fi

# run
if test $run_flag = 1 
then
      echo
      echo ---------- HPF prun $runable ----------
      if [ -f $runable ]
      then
	echo "++ $prun $proc_number $runable ++"
	$prun $proc_number  $runable 
      else
		echo  hpfc:error: $runable not found !
		clearall
		exit 1
      fi
fi


#clear up
clearall

#byebye
exit 0

