#!/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 "  -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 allmodules = ( \
  SysTools TArray NTools HiStats SkyMap Samba FitsIOServer PMixer SkyT \
  )
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( ! $?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 ?
#----------
which doxygen >! /dev/null
if( $status != 0 ) then
  echo 'ERROR: doxygen is not installed...'
  exit -1
endif

#-------------
# doxysearch ?
#-------------
which doxysearch >! /dev/null
if( $status != 0 ) then
  echo 'ERROR: doxysearch is not installed...'
  exit -2
else
    set s = `which doxysearch`
  echo 'doxysearch 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 = ../SysTools/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

#-------------------------------------------------------
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; latex sophya.tex; \
                             dvips -o sophya.ps sophya.dvi

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

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

EOF_

exit 0
