#!/bin/sh 
#--------------------------- Shell MegaWave2 Macro --------------------------#
_sccsid="%Z%MegaWave %R%, %M% %I%, %G%";
_Func="Make a new MegaWave2 documentation (guide #3) consistent with the existing modules and macros"
_Prog="`basename $0`"
_Vers="2.17"
_Date="1996-2004"
_Auth="Jacques Froment";
_Usage="[-N] [-g group_directory] [-nobin] [+/-index] [+/-html] [-c] [-h] megawave2_directory"
#----------------------------------------------------------------------------#
# This file is part of the MegaWave2 system macros. 
# MegaWave2 is a "soft-publication" for the scientific community. It has
# been developed for research purposes and it comes without any warranty.
# The last version is available at http://www.cmla.ens-cachan.fr/Cmla/Megawave
# CMLA, Ecole Normale Superieure de Cachan, 61 av. du President Wilson,
#       94235 Cachan cedex, France. Email: megawave@cmla.ens-cachan.fr 
#-----------------------------------------------------------------------------

# Return 1 if the given named LaTeX package is found, 0 elsewhere

CheckLaTeXPackage()
{
  kw=`which kpsewhich`
  if [ "$kw" = "" ] || [ ! -x $kw ]; then
    return 0
  fi
  pack=`$kw $1.sty 2> /dev/null`
  if  [ "$pack" != "" ] && [ -r $pack ]; then
    return 1
  fi
  return 0
}

# Usage
if [ $# -lt 1 ] || [ $# -gt 9 ]; then
  . .mw2_help_lg_com
fi

Main=yes
mw_group=.
group=not_a_group

# Generate index if makeidx LaTeX package found
CheckLaTeXPackage makeidx
index=$?

# Generate html code if html/hthtml LaTeX packages found
CheckLaTeXPackage html
html=$?
if [ $html -eq 1 ]; then
  CheckLaTeXPackage hthtml
  html=$?
fi

compile=no
latex2html=no
opt=""
while [ "$1" != "" ]
do
    case "$1" in

    -c)   compile=yes;
          shift;
          ;;

    -h)   latex2html=yes;
          compile=yes;
          shift;
          ;;

    -N)   Main=no;
          shift;
          ;;

    -g)   shift;
          mw_group=$1;
	  shift;
          ;;

# Set if do not check consistency with binaries
    -nobin) NoBin=-nobin
            shift
            ;;

    +index) index=1
            opt="$opt $1"
            shift
            ;;


    -index) index=0
            opt="$opt $1"
            shift
            ;;


    +html) html=1
           opt="$opt $1"
           shift
            ;;

    -html) html=0
           opt="$opt $1"
           shift
            ;;

    -*)   . .mw2_help_lg_com;
	  ;;

    *) 	mw2_dir=$1;
        shift
	;;

    esac
done

if [ "${mw2_dir}" = "" ]; then
  . .mw2_help_lg_com
fi

doc_dir=${mw2_dir}/doc
doc_obj=${doc_dir}/obj
doc_src=${doc_dir}/src

# Name of the guide #3 (modules library)
doc_bname=guid3
doc_modules=${doc_bname}.tex
# Name of the list TeX file for the guide #3 (formerly named index)
doc_modules_list=${doc_bname}.list
# Name of the index TeX file
doc_modules_index=${doc_bname}.idx

bin=bin/${MW_MACHINETYPE?'environment not set'}

if [ ! -d ${mw2_dir} ] || [ ! -d ${doc_dir} ]; then
  echo "${_Prog} : Bad directory name for MegaWave2 : ${mw2_dir}" 1>&2
exit 1
fi

if [ ! -w ${mw2_dir} ] || [ ! -w ${doc_dir} ]; then
   echo "${_Prog} : Sorry, you're not allowed to generate doc of MegaWave2" 1>&2
   exit 1
fi

if [ ! -d ${MEGAWAVE2?'environment not set'}/doc ]; then
  echo "${_Prog} : original doc directory not found or bad MEGAWAVE2 path" 1>&2
  exit 1
fi

if [ $Main = "yes" ]; then
 if [ -f ${doc_dir}/${doc_modules} ]; then
   \rm -f ${doc_dir}/${doc_modules}
 fi
 if [ -f ${doc_dir}/${doc_modules_list} ]; then
   \rm -f ${doc_dir}/${doc_modules_list}
 fi
fi

if [ ! -d ${mw2_dir}/src/${mw_group} ]; then
  echo "${_Prog} : Invalid group directory ${mw_group}" 1>&2
  exit 1
fi

if [ $Main = "yes" ]; then
  cat ${MEGAWAVE2?'environment not set'}/sys/shell/data/mwmakedoc_header_latex2e.tex >> ${doc_dir}/${doc_modules}
  if [ $index -eq 1 ]; then
    mwecho -E "\usepackage{makeidx}" >> ${doc_dir}/${doc_modules}
  fi
  if [ $html -eq 1 ]; then
    mwecho -E "\usepackage{html,hthtml}" >> ${doc_dir}/${doc_modules}
  fi
  if [ $html -eq 1 ]; then
    cat ${MEGAWAVE2}/sys/shell/data/mwmakedoc_body_html.tex >> ${doc_dir}/${doc_modules}
    cat $MEGAWAVE2/sys/shell/data/mwmakedoc_macros_html.tex >> ${doc_dir}/${doc_modules}
    cat $MEGAWAVE2/sys/shell/data/mwmakedoc_list_html.tex >> ${doc_dir}/${doc_modules}
  else
    cat ${MEGAWAVE2}/sys/shell/data/mwmakedoc_body.tex >> ${doc_dir}/${doc_modules}
    cat $MEGAWAVE2/sys/shell/data/mwmakedoc_macros.tex >> ${doc_dir}/${doc_modules}
    cat $MEGAWAVE2/sys/shell/data/mwmakedoc_list.tex >> ${doc_dir}/${doc_modules}
  fi

  mwecho -E "\input{${doc_modules_list}}" >> ${doc_dir}/${doc_modules}
  mwecho -E "\newpage" >> ${doc_dir}/${doc_modules}
#    mwecho -E "\addcontentsline{toc}{section}{Reference}" >> ${doc_dir}/${doc_modules}

  if [ $html -eq 1 ]; then
   cat $MEGAWAVE2/sys/shell/data/mwmakedoc_reference_html.tex >> ${doc_dir}/${doc_modules}
  else
   cat $MEGAWAVE2/sys/shell/data/mwmakedoc_reference.tex >> ${doc_dir}/${doc_modules}
  fi
fi

for file in `ls ${mw2_dir}/src/${mw_group}`
do
  bfile=`basename ${file} .c`
  docfile=${bfile}.doc
  ftype=""
  if [ ! -d ${mw2_dir}/src/${mw_group}/${file} ]; then
    if [ "$bfile" != "$file" ]; then
# This file is a module (C source)
      ftype="m"
      if [ ! -f ${doc_obj}/${docfile} ]; then
        echo "${_Prog} : Cannot find file ${doc_obj}/${docfile}" 1>&2
        echo "There is an inconsistency between the source of the module ${bfile} and its doc" 1>&2
        echo "Please re-install this copy of MegaWave2" 1>&2
        exit 1
      fi
      if [ ! -f ${mw2_dir}/${bin}/${bfile} ] && [ "$NoBin" = "" ]; then
        echo "${_Prog} : Cannot find file ${mw2_dir}/${bin}/${bfile}" 1>&2  
        echo "There is an inconsistency between the source of the module ${bfile} and its binary file" 1>&2
        echo "Please re-install this copy of MegaWave2" 1>&2
        exit 1
      fi
    else
# Check for a macro
      tilde=`echo ${file}. | grep "~."`
      if [ -x ${mw2_dir}/src/${mw_group}/${file} ] && [ "$tilde" = "" ]; then
# Automatically identifies a file name as a macro when its name 
# begins with a capital letter.
        mwisupper $file > /dev/null
        if [ $? -eq 1 ]; then
# This file is a macro (shell)
          ftype="s"
          if [ ! -f ${doc_obj}/${docfile} ]; then
            echo "${_Prog} : Cannot find file ${doc_obj}/${docfile}" 1>&2
            echo "There is an inconsistency between the source of the macro ${bfile} and its doc" 1>&2
            echo "Please re-install this copy of MegaWave2" 1>&2
            exit 1
          fi
        fi
      fi
# end of if [ "$bfile" != "$file" ]; then
    fi
    if [ "$ftype" != "" ]; then
      mwecho -n "${bfile} "
      if [ ! -f ${doc_src}/${bfile}.tex ]; then
        echo "${_Prog}: Warning: module or macro ${bfile} is not documented (no ${bfile}.tex file)" 1>&2
        echo "Creating an empty file to avoid error during LaTeX compilation" 1>&2
        echo "Not documented. Please create a tex file for this module or macro as soon as possible" > ${doc_src}/${bfile}.tex
      fi

# Put the List

      old_group=$group
      group=`grep "%*** Group=" ${doc_obj}/${docfile} | \
	     sed "s/%\*\*\* Group=//"`
      name=`grep "%*** Name=" ${doc_obj}/${docfile} | \
	     sed "s/%\*\*\* Name=//"`

      if [ "$old_group" != "$group" ]; then
# Yes, as many \\... as these are needed on Solaris !
        atgroup=`echo $group | sed "s/_/\\\\\\_/g"`
        mwecho -E "\addcontentsline{toc}{subsection}{Group ${atgroup}}" >> ${doc_dir}/${doc_modules}
        mwecho -E "\vspace{10mm}" >> ${doc_dir}/${doc_modules_list}
        mwecho -E "{\large Group \verb+${group}+:}" >> ${doc_dir}/${doc_modules_list}
        echo " " >> ${doc_dir}/${doc_modules_list}
        mwecho -E "\vspace{5mm}" >> ${doc_dir}/${doc_modules_list}
      fi

      if [ $ftype = "m" ]; then
        grep "\\Name{" ${doc_obj}/${docfile} | sed "s/Name{/List{/g" >> ${doc_dir}/${doc_modules_list}   
      else
        grep "\\Macro{" ${doc_obj}/${docfile} | sed "s/Macro{/List{/g" >> ${doc_dir}/${doc_modules_list}   
      fi
      echo "{${name}}" >>  ${doc_dir}/${doc_modules_list}
      echo " " >>  ${doc_dir}/${doc_modules_list}

# Put the doc file

      if [ $html -eq 1 ]; then
       mwecho -E "\begin{htmlonly}" >> ${doc_dir}/${doc_modules}
       mwecho -E "\paragraph{${name}}" >> ${doc_dir}/${doc_modules}
       mwecho -E "\end{htmlonly}" >> ${doc_dir}/${doc_modules}
#         mwecho -E "\begin{rawhtml}" >> ${doc_dir}/${doc_modules}
#         echo "<HR>" >> ${doc_dir}/${doc_modules}
#         mwecho -E "\end{rawhtml}" >> ${doc_dir}/${doc_modules}
      fi

      mwecho -E "\input{obj/${docfile}}" >> ${doc_dir}/${doc_modules}
      mwecho -E "\newpage" >> ${doc_dir}/${doc_modules}   

# End of if [ "$ftype" != "" ]; then
    fi
# End of if [ ! -d ${mw2_dir}/src/${mw_group}/${file} ]; then
  fi
done

# Now, scan subdirectories

for file in `ls ${mw2_dir}/src/${mw_group}`
do
  if [ -d ${mw2_dir}/src/${mw_group}/${file} ]; then
# This file is a directory
   echo ""
   echo "---> Make doc for group ${mw_group}/${file} <---"
   mwmakedoc -N $NoBin -g ${mw_group}/${file} $opt ${mw2_dir}
   echo ""
  fi
done

if [ $Main = "yes" ]; then
  cat $MEGAWAVE2/sys/shell/data/mwmakedoc_bib.tex >> ${doc_dir}/${doc_modules}
  if [ $index -eq 1 ]; then
    cat $MEGAWAVE2/sys/shell/data/mwmakedoc_foot_index.tex >> ${doc_dir}/${doc_modules}
  else
    cat $MEGAWAVE2/sys/shell/data/mwmakedoc_foot.tex >> ${doc_dir}/${doc_modules}
  fi

  if [ "$compile" = "no" ]; then
# Print what to do in order to compile guid3.tex
   echo " "
   echo "Compile the guide #3 with the following command"
   if [ $index -eq 1 ]; then    
     echo "cd ${doc_dir}; latex ${doc_modules}; mwmodbibtex ..; makeindex ${doc_modules_index}; latex ${doc_modules}; latex ${doc_modules};"
   else
     echo "cd ${doc_dir}; latex ${doc_modules}; mwmodbibtex ..; latex ${doc_modules}; latex ${doc_modules};"
   fi
  else
# Compile guid3.tex
   echo "${_Prog} : Compiling ${doc_bname} using LaTeX..."
   cd ${doc_dir}
   latex ${doc_modules}
   if [ $? -ne 0 ]; then
    echo "${_Prog} : Cannot successfully run 'latex ${doc_modules}' (pass 1) ! Exit." 1>&2
    exit 1
   fi
   mwmodbibtex ..
   if [ $? -ne 0 ]; then
    echo "${_Prog} : Cannot successfully run 'mwmodbibtex' ! Exit." 1>&2
    exit 1
   fi
   if [ $index -eq 1 ]; then    
    makeindex ${doc_modules_index}
    if [ $? -ne 0 ]; then
     echo "${_Prog} : Cannot successfully run 'makeindex ${doc_modules_index}' ! Exit." 1>&2
     exit 1
    fi
   fi
   latex ${doc_modules}
   if [ $? -ne 0 ]; then
    echo "${_Prog} : Cannot successfully run 'latex ${doc_modules}' (pass 2) ! Exit." 1>&2
    exit 1
   fi
   latex ${doc_modules}
   if [ $? -ne 0 ]; then
    echo "${_Prog} : Cannot successfully run 'latex ${doc_modules}' (pass 3) ! Exit." 1>&2
    exit 1
   fi
   if [ "$latex2html" = "yes" ]; then
# Run latex2html
    echo "${_Prog} : Calling latex2html (this may take a while !)..."    
    latex2html -no_math -html_version 3.2,math ${doc_bname}
   fi
  fi
fi

exit 0



