#!/bin/csh
#####################################################
if( $#argv >= 1 ) then
  if( "$1" == "-h" ) then
    echo "mkmf [-options] [-o dir] [module1] [module2] [...]"
    echo "  generate documentation for all Sophya"
    echo "  or only for module [module...]"
    echo "  **** [-options] :"
    echo "  -libmod : generate doc for library modules (default)"
    echo "  -prgmod : generate doc for program modules"
    echo "  -allmod : generate doc for library+program modules"
    echo "  -html : generate HTML doc"
    echo "  -latex : generate LATEX doc"
    echo "  -man : generate MAN doc"
    echo "  -rtf : generate RTF doc"
    echo "  default is : html + latex"
    echo "  **** [-o dir] : directory where doc have to be put"
    echo "  default is ."
    exit -1
  endif
endif
#####################################################

##################################
######## Decode arguments ########
##################################
set libmodules = ( \
  BaseTools HiStats NTools Samba SkyMap SUtils SysTools TArray \
  SkyT FitsIOServer IFFTW LinAlg XAstroPack \
  )
set prgmodules = ( PMixer PrgMap PrgUtil ProgPI )

set allmodules = ( $libmodules $prgmodules )

unset modules; unset latex; unset html; unset man; unset rtf
set outdir = "."
if( $#argv >= 1 ) then
  while ( $#argv > 0)
    if( "$1" == "-o" ) then
      shift
      set outdir = $1
    else if( "$1" == "-html" ) then
      set html
    else if( "$1" == "-latex" ) then
      set latex
    else if( "$1" == "-man" ) then
      set man
    else if( "$1" == "-rtf" ) then
      set rtf
    else if( "$1" == "-libmod" ) then
      set modules = ( $libmodules )
    else if( "$1" == "-prgmod" ) then
      set modules = ( $prgmodules )
    else if( "$1" == "-allmod" ) then
      set modules = ( $allmodules )
    else
      if( ! $?modules ) set modules
      set modules = ( $modules $1 )
    endif
    shift
  end
endif
if( ! $?html  && ! $?latex  && ! $?man  && ! $?rtf ) then
  set html
  set latex
endif
if( ! $?modules ) then
  set modules = ( $allmodules )
endif

##########################################
######## Test de la configuration ########
##########################################

#------------
# mydoxy.conf
#------------
cp dox_sophya.conf mydoxy.conf

#---------------
# doxygen path ?
#---------------
which doxygen >! /dev/null
if( $status != 0 ) then
  echo 'ERROR: doxygen is not installed...'
  exit -2
else
    set s = `which doxygen`
  echo 'doxygen is installed in : ' $s:h
  echo 'mkmf will automatically update the config file.'
    echo "BIN_ABSPATH = $s:h" >>  mydoxy.conf
    grep 'BIN_ABSPATH' mydoxy.conf | grep -v ^\#
endif

#-----------------
# Version number ?
#-----------------
set f = ../BaseTools/sversion.h
if( -e $f ) then
  set v = `grep 'SOPHYA_VERSION'  $f | awk '{print $3}'`
  set r = `grep 'SOPHYA_REVISION' $f | awk '{print $3}'`
  set t = `grep 'SOPHYA_TAG'      $f | awk '{print $3}'`
  echo "PROJECT_NUMBER = V${v}_R${r}_${t}" >>  mydoxy.conf
else
  echo "PROJECT_NUMBER = Not_Defined" >>  mydoxy.conf
endif
grep 'PROJECT_NUMBER = ' mydoxy.conf | tail -1

#---------------
# What modules ?
#---------------
set lf =
foreach f ( $modules )
  echo "... Generating doc for ../$f"
  set lf = ( ../$f $lf )
end
echo "INPUT = $lf" >>  mydoxy.conf
grep 'INPUT = ' mydoxy.conf | tail -1

set lf =
foreach f ( $allmodules )
  set lf = ( ../$f $lf )
end
echo "EXAMPLE_PATH = $lf" >>  mydoxy.conf

#-------------------
# What kind of doc ?
#-------------------
if( $?html ) then
  echo "GENERATE_HTML = YES" >>  mydoxy.conf
  echo "...... generating HTML"
else
  echo "GENERATE_HTML = NO" >>  mydoxy.conf
endif
if( $?latex ) then
  echo "GENERATE_LATEX = YES" >>  mydoxy.conf
  echo "...... generating LATEX"
else
  echo "GENERATE_LATEX = NO" >>  mydoxy.conf
endif
if( $?man ) then
  echo "GENERATE_MAN = YES" >>  mydoxy.conf
  echo "...... generating MAN"
else
  echo "GENERATE_MAN = NO" >>  mydoxy.conf
endif
if( $?rtf ) then
  echo "GENERATE_RTF = YES" >>  mydoxy.conf
  echo "...... generating RTF"
else
  echo "GENERATE_RTF = NO" >>  mydoxy.conf
endif

#------------------------
# What output directory ?
#------------------------
if( ! -d $outdir ) then
  echo ERROR: Not existing directory : $outdir
  exit -1
endif
echo "OUTPUT_DIRECTORY = $outdir" >>  mydoxy.conf
grep 'OUTPUT_DIRECTORY = ' mydoxy.conf | tail -1

#--------------------------
# Configuration file update
#--------------------------
doxygen -u mydoxy.conf >! /dev/null
rm -f mydoxy.conf.bak

#########################################
######## Creation de la Makefile ########
#########################################

rm -f Makefile
touch Makefile
chmod +x Makefile
cat >> Makefile << EOF_

#-------------------------------------------------------
all: doc sophya pmixer piapp

#-------------------------------------------------------
clean:
	rm -rf *.o dox_filter
	if [ -d ${outdir} ] ; then cd ${outdir}/ ; rm -rf html latex man rtf; fi

#-------------------------------------------------------
copy: 
	if [ ! -d ${outdir}/latex ] ; then mkdir ${outdir}/latex; fi
	cp -p *.tex *.sty *.eps *.inc ${outdir}/latex/.

#-------------------------------------------------------
doc: filter
	doxygen mydoxy.conf

#-------------------------------------------------------
filter: dox_filter

dox_filter : dox_filter.o

dox_filter.o : dox_filter.c

#-------------------------------------------------------
sophya: copy ${outdir}/latex/sophya.ps

${outdir}/latex/sophya.ps: sophya.tex defsophya.sty blue_sophya_400.eps ex1.inc
	cd ${outdir}/latex/; \
	latex sophya.tex; \
	makeindex sophya; \
	latex sophya; \
	dvips -o sophya.ps sophya.dvi

piapp: copy ${outdir}/latex/piapp.ps

${outdir}/latex/piapp.ps: piapp.tex piahelp.tex defsophya.sty
	cd ${outdir}/latex/; \
	latex piapp.tex; \
	makeindex piapp; \
	latex piapp; \
	dvips -o piapp.ps piapp.dvi

pmixer: copy ${outdir}/latex/pmixer.ps

${outdir}/latex/pmixer.ps: pmixer.tex
	cd ${outdir}/latex/; \
	latex pmixer.tex; \
	latex pmixer; \
	dvips -o pmixer.ps pmixer.dvi

EOF_

exit 0
