#! /usr/bin/env sh
#
#
#  A script to generate the Makefile for the API Reference

if ! test -r ./dir_list ; then
    cat <<EOF 
ERROR: cannot read the directory list -- please verify that this 
  script is run in MITGCM_ROOT/doc/api_reference and that the 
  "dir_list" file is readable.
EOF
    exit 1
fi

cat ./dir_list | grep -v '^[ ]*#' | grep -v '^[ ]*$' > ./.dir_list_nocomments

name=$0
cat <<EOF > Makefile
#
#  ================================================
#
#  CREATED BY "$name" -- DO NOT EDIT !!!
#
#  ================================================
#

.SUFFIXES:

all: all_protex

EOF

rm -rf ./.targets ./.all_clean
cat ./.dir_list_nocomments | while read line ; do

    echo >> Makefile
    base_target=`echo $line | awk '{print $1}' | sed -e 's|/|__|g'`
    target=$base_target".tex"
    printf " $target" >> ./.all_clean
    echo "Finding files for \"$target\" in: $line"
    tex_files=""
    h_files=""
    F_files=""
    c_files=""
    for i in $line ; do
	tex_files="$tex_files"`ls -1 ../../$i/*.tex  2>/dev/null`
	h_files="$h_files"`ls -1 ../../$i/*.h  2>/dev/null`
	F_files="$F_files"`ls -1 ../../$i/*.F  2>/dev/null`
	c_files="$c_files"`ls -1 ../../$i/*.c  2>/dev/null`
    done

    # *.tex
    printf '%s' $base_target"_tex = " >> Makefile
    for i in $tex_files ; do
	printf '\\\n%s' " $i " >> Makefile
    done
    printf '\n' >> Makefile

    # *.h
    printf '%s' $base_target"_h = " >> Makefile
    for i in $h_files ; do
	printf '\\\n%s' " $i " >> Makefile
    done
    printf '\n' >> Makefile

    # *.F
    printf '%s' $base_target"_F = " >> Makefile
    for i in $F_files ; do
	printf '\\\n%s' " $i " >> Makefile
    done
    printf '\n' >> Makefile

    # *.c
    printf '%s' $base_target"_c = " >> Makefile
    for i in $c_files ; do
	printf '\\\n%s' " $i " >> Makefile
    done
    printf '\n' >> Makefile
    d_tex="\$("$base_target"_tex)"
    d_h="\$("$base_target"_h)"
    d_F="\$("$base_target"_F)"
    d_c="\$("$base_target"_c)"
    cat <<EOF >> Makefile
$target: $d_tex $d_h $d_F $d_c
	-rm -f $target
EOF
    if test ! "x$tex_files" = x ; then
	printf '\t%s\n' "-cat $d_tex >> $target" >> Makefile
    fi
    if test ! "x$h_files" = x ; then
	printf '\t%s\n' "-./protex -b7f $d_h >> $target" >> Makefile
    fi
    if test ! "x$F_files" = x ; then
	printf '\t%s\n' "-./protex -b7f $d_F >> $target" >> Makefile
    fi
    if test ! "x$c_files" = x ; then
	printf '\t%s\n' "-./protex -bCf $d_c >> $target" >> Makefile
    fi

    if test ! "x$target" = x ; then
	printf '%s' " $target" >> ./.targets
    fi
done

alltex=`cat ./.targets`
printf '\n%s' "ALLTEX = " >> Makefile
for i in $alltex ; do
    printf '\\\n%s' " $i " >> Makefile
done
printf '\n\n' >> Makefile

cat <<EOF >> Makefile

makefile:
	$0

api_main.tex: introduction.tex \$(ALLTEX)
	-./build_main \$(ALLTEX)

api_main.dvi:  api_main.tex
	latex api_main
	bibtex api_main
	latex api_main
	latex api_main

api_main.ps: api_main.dvi
	dvips -Pcmz -Pamz -o api_main.ps api_main.dvi

PDFOPTS = -dMaxSubsetPct=100 -dCompatibilityLevel=1.2 -dSubsetFonts=true -dEmbedAllFonts=true 
api_main.pdf: api_main.ps
	ps2pdf \$(PDFOPTS) api_main.ps api_main.pdf

all_protex: api_main.pdf

distclean:
	@make clean
	-rm -f Makefile

install: all
	-rm -rf ./api_ref
	mkdir ./api_ref
	-cp api_main.pdf ./api_ref/api_ref.pdf
	scp -r ./api_ref mitgcm.org:/u/u0/httpd/html/

clean:
	-rm -f *.aux *.dvi *.log *.toc *.out
	-rm -f api_main.*
EOF

printf "\t-rm -f " >> Makefile
cat ./.all_clean >> Makefile

rm -f ./.dir_list_nocomments
rm -f ./.targets
rm -f ./.all_clean
