#!/bin/sh
#
#  MKPROTO -- Generate the VOS prototype definitions.


vos_dir="sys"
math_dir=""
xtools_dir="pkg/xtools"
ds_dir="pkg/images/tv/display pkg/images/tv/wcslab"

#vosdirs="$vos_dir $math_dir $ds_dir"
vosdirs="$vos_dir $math_dir $xtools_dir $ds_dir"

curdir=$(pwd)
tproto=/tmp/_vosproto.$$

# Initialize the $iraf and environment.
if [ -z "$iraf" ]; then
  if [ -e "$HOME/.iraf/setup.sh" ]; then
    . "$HOME/.iraf/setup.sh"
  else
    . unix/hlib/setup.sh
  fi
else
    . "$iraf/unix/hlib/setup.sh"
fi


for d in $vosdirs; do

    # Process each of the subdirectories.
    #
    sdirs=$(find "$d" -type d -print)
    for sd in $sdirs; do

	# Skip the IMFORT library and any documentation directories
	fname=${sd##*/}
	if [ "$fname" != "doc" ] && \
	       echo "$sd" | grep -qv imfort && \
	       echo "$sd" | grep -qv osb && \
	       echo "$sd" | grep -qv memdbg &&  \
	       echo "$sd" | grep -qv nspp; then

	    cd "$sd"					# go to subdirectory

	    nf=$(ls -1 -- *.f | wc -l)	# nfiles to process
	    nx=$(ls -1 -- *.x | wc -l)	# nfiles to process

	    echo "Processing: $nx $nf $sd"
	    if [ "$nf" -gt 0 ] || [ "$nx" -gt 0 ]; then

		# Skip any files beginning with the letters 'zz', e.g. test
		# files like zzdebug.x

	 	fils=$(ls -1 -- [a-z][a-y]*.[xf])
	 	for f in $fils; do
		    if [ "$f" != "intrp.f" ] && \
		       [ "$f" != "xtpmmap.x" ] && \
		       [ "$f" != "ytpmmap.x" ]; then
	                    xc -c -/P "$f" >> /dev/null 2>&1	# make prototype
		    else
		        echo "    Skipping $f"
		    fi
		done

	        grep -h ^extern -- *.P >> $tproto		# save prototypes
	        rm -f -- *.[oP]				# clean up
	    fi
	    cd $curdir				# back to top
	else
            echo "    Skipping  $sd"
	fi
    done
done

# Sort the prototype file, exclude certain symbols which are duplicates 
# in the VOS but harmless because they occur in places that never conflict,
# e.g. the stdgraph and imd kernels.

cat "$tproto" 		| \
    grep -v arbpix_ 	| \
    grep -v imdgeg_ 	| \
    grep -v stxset_ 	| \
    grep -v stxpas_ 	| \
    grep -v dgt_ 	| \
    grep -v sgt_ 	| \
    grep -v asider_ 	| \
    grep -v asifit_ 	| \
    grep -v bndsol_ 	| \
    grep -v ffa_ 	| \
    grep -v ffs_ 	| \
    grep -v fft842_ 	| \
    grep -v avdrs_ 	| \
    grep -v asigrl_ 	| \
    grep -v smooth_ 	| \
    grep -v sigl2 	| \
    grep -v sigm2 	| \
    grep -v impcom_ 	| \
    grep -v intrp 	| \
    grep -v srch 	| \
    grep -v codim 	| \
    grep -v mrsole 	| \
    grep -v mreval 	| \
    grep -v \ icg 	| \
    grep -v iclisd 	| \
    grep -v gscr 	| \
    grep -v imdcom 	| \
    grep -v getrot 	| \
    grep -v gtplot 	| \
    grep -v xev 	| \
    grep -v ceps 	| \
    grep -v sgt 	| \
    grep -v sgt 	| \
    grep -v U_fp 	| \
    sort >$iraf/unix/hlib/libc/vosproto.h	

#  sigl2* and sigm2* removed because of multiple uses in apps
#  impcom removed because imio$dbc/impcom.x conflicts with plot$t_implot.x 
#	use of 'impcom' as a common
#  intrp*  removed because it's use is xtools$ was commented out
#  srch*  removed because it's use is xtools$ was commented out
#  mrsole/mreval removed because of duplicate use (splot$deblend.x and
#  	xtools$numrecipes)
#  icguag removed duplicate use in noao$imred/dtoi
