#!/bin/csh -fb
#
# Creates ASCII manual pages in ASCMAN/man/man{1,...} sudirectories from
# nroff manual pages in SEDMAN/man/man{1,...} subdirectories.
# The ASCMAN/man/man{1,...} sudirectories are created if they do not exist.
# Also can create HTML version of the manpages.
#
# Requires groff to be installed and accessible.
#
# $1 = directory where aimk is installed
# $2 = "ge" "geee" "sge" or "sgeee"
# $3 = 0 or 1 - flag if man pages in ASCII format should be created
# $4 = 0 or 1 - flag if man pages in HTML format  should be created
# $5 = 0 or 1 - checkout man pages with "-kv" flag
# $6 = name of the cvs module which will be checked out (default gridengine/doc/man)
# $7 = directory where the man pages will be built (default  <source dir>/MANSBUILD_<product name>)
#
#___INFO__MARK_BEGIN__
##########################################################################
#
#  The Contents of this file are made available subject to the terms of
#  the Sun Industry Standards Source License Version 1.2
#
#  Sun Microsystems Inc., March, 2001
#
#
#  Sun Industry Standards Source License Version 1.2
#  =================================================
#  The contents of this file are subject to the Sun Industry Standards
#  Source License Version 1.2 (the "License"); You may not use this file
#  except in compliance with the License. You may obtain a copy of the
#  License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
#
#  Software provided under this License is provided on an "AS IS" basis,
#  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
#  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
#  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
#  See the License for the specific provisions governing your rights and
#  obligations concerning the Software.
#
#  The Initial Developer of the Original Code is: Sun Microsystems, Inc.
#
#  Copyright: 2001 by Sun Microsystems, Inc.
#
#  All Rights Reserved.
#
##########################################################################
#___INFO__MARK_END__

umask 022

set MANBUILDDIR = $1/MANSBUILD_$2
set MANMODE = $2
set MANASC  = $3
set MANHTML = $4
set KVMAN   = $5

if ( $#argv > 5 ) then
  set CVS_MODULE = $6
else
  set CVS_MODULE = gridengine/doc/man
endif

if ( $#argv > 6 ) then
  set MANBUILDDIR = $7
endif

echo "Removing old manpages in $MANBUILDDIR"
\rm -rf $MANBUILDDIR
mkdir -p $MANBUILDDIR

# Take CVSROOT from current CVS direcotry
#
setenv CVSROOT `cat CVS/Root`

setenv CVSBRANCH ""
setenv REVISION ""
if ( $KVMAN == 1 ) then
   if ( -f CVS/Tag ) then
      setenv CVSBRANCH `sed 's/^[TN]//' CVS/Tag`
      echo "==> Using CVS branch: ${CVSBRANCH}"
      #setenv CVSBRANCH "-r $CVSBRANCH"
      setenv REVISION "-r $CVSBRANCH"
   endif
endif

cd $MANBUILDDIR

if ( $KVMAN == 1 ) then
   set MANSRCDIR = $MANBUILDDIR/MANSRC
   echo "Checking out manpage sources"
   cvs -q co $REVISION -P -kv -d MANSRC $CVS_MODULE
else
   set MANSRCDIR = $1/../doc/man
endif

# 
# Changes magic strings in the checked-in man page versions into SGE
# SGEEE, etc. as appropriate.
# Creates the changed man pages in SEDMAN/man/man{1,...} subdirectories,
# which are created if they do not exist.
#
# Requires sed to be installed and accessible.
if ( $MANMODE == "ge" ) then
   set xxQS_NAMExx = "Grid Engine" 
   set xxQS_NAME_Sxx = "GE" 
   set xxqs_name_sxx = "ge" 
   set xxRELxx = "GE 6.2u5"
else if ( $MANMODE == "sge" ) then
   set xxQS_NAMExx = "Sun Grid Engine"
   set xxQS_NAME_Sxx = "SGE"
   set xxqs_name_sxx = "sge"
   set xxRELxx = "SGE 6.2u5"
else
   echo error wrong MANMODE=$MANMODE. Exit
   exit 1
endif

# Build the list of the available sections
set SECTION_LIST = ""
foreach n ( 1 2 3 4 5 6 7 8 )
   if ( -d $MANSRCDIR/man${n} ) then
     if ( "$SECTION_LIST" == "" ) then
        set SECTION_LIST = $n
     else
        set SECTION_LIST = "$SECTION_LIST $n"
     endif
   endif
end

echo "Building SEDMAN pages"
foreach n ( $SECTION_LIST )
   mkdir -p $MANBUILDDIR/SEDMAN/man/man${n} >& /dev/null

   if ( ! -d $MANBUILDDIR/SEDMAN/man/man${n} ) then
      echo "Error: Couldn't create subdirectory SEDMAN/man/man${n}."
      exit 1
   endif
  
   foreach f ( $MANSRCDIR/man${n}/*.${n} )
      echo Processing $f
      sed -e "s/xxQS_NAMExx/$xxQS_NAMExx/g" \
          -e "s/xxQS_NAME_Sxx/$xxQS_NAME_Sxx/g" \
          -e "s/xxqs_name_sxx/$xxqs_name_sxx/g" \
          -e "s/xxRELxx/$xxRELxx/g" $f >! $MANBUILDDIR/SEDMAN/man/man${n}/`basename $f`
   end
end


#
# process the seded man pages with groff
#
if ( "$MANASC" == 1 ) then
   echo "Building ASCMAN from sedman pages"
   foreach n ( $SECTION_LIST )
      mkdir -p $MANBUILDDIR/ASCMAN/man/man${n} >& /dev/null

      if ( ! -d $MANBUILDDIR/ASCMAN/man/man${n} ) then
         echo "Error: Couldn't create subdirectory ASCMAN/man/man${n}."
         exit 1
      endif
           
      cd $MANBUILDDIR/SEDMAN/man     
      foreach f ( man${n}/*.${n} )
         echo Processing $f
         groff -man -t -Tascii $f >! $MANBUILDDIR/ASCMAN/man/man${n}/`basename $f .${n}`
      end
   end
endif

#
# build html man pages from sedman with man2html 
#
if ( "$MANHTML" == 1 ) then
   set PATHREV = ""
   if ( "$CVSBRANCH" != "" ) then
      set PATHREV = "?pathrev=$CVSBRANCH"
   endif

   echo "Building HTML from sedman pages"
   foreach n ( $SECTION_LIST )
      mkdir -p $MANBUILDDIR/HTMLMAN/htmlman${n}
      foreach i ($MANBUILDDIR/SEDMAN/man/man${n}/*)
         echo Processing $i
         set bn = `basename $i .${n}`
         # man -M$MANBUILDDIR/SEDMAN/man/ $bn | man2html -solaris > $MANBUILDDIR/HTMLMAN/htmlman${n}/${bn}.html
         set secopt = -s
         if ( Linux == `uname` ) then
            set secopt = ""
         endif   
         man -M$MANBUILDDIR/SEDMAN/man/ $secopt $n $bn | man2html -cgiurl "../htmlman"'$section/$title'".html$PATHREV" -solaris | sed '2s/BODY/BODY BGCOLOR=white/' > $MANBUILDDIR/HTMLMAN/htmlman${n}/${bn}.html
      end
   end   

   #
   # generate an index file 
   #
   cp $1/scripts/template.html $MANBUILDDIR/HTMLMAN/index.html 
   foreach n ( $SECTION_LIST )
      echo "<H3>Section $n</H3>" >> $MANBUILDDIR/HTMLMAN/index.html
      echo '<P><UL>' >> $MANBUILDDIR/HTMLMAN/index.html
      foreach f ($MANBUILDDIR/HTMLMAN/htmlman${n}/*)
         set base = `basename $f`
         set title = `basename $base .html`
         echo '<LI><A HREF=' >> $MANBUILDDIR/HTMLMAN/index.html
         echo "htmlman${n}/$base$PATHREV" target="PAGE">> $MANBUILDDIR/HTMLMAN/index.html
         echo ">$title</A>" >> $MANBUILDDIR/HTMLMAN/index.html 
      end
      echo '</UL></P>' >> $MANBUILDDIR/HTMLMAN/index.html
   end
   echo '</BODY></HTML>' >> $MANBUILDDIR/HTMLMAN/index.html

   #
   # generate the manual.html file
   #
   echo '<HTML>' > $MANBUILDDIR/HTMLMAN/manuals.html
   echo ' <HEAD><TITLE>SGE Manual Pages</TITLE></HEAD>' >> $MANBUILDDIR/HTMLMAN/manuals.html
   echo ' <frameset cols="230,*" frameborder="NO" framespacing="0" border="3">' >> $MANBUILDDIR/HTMLMAN/manuals.html
   echo '  <frame src=' >> $MANBUILDDIR/HTMLMAN/manuals.html
   echo "   index.html$PATHREV" >> $MANBUILDDIR/HTMLMAN/manuals.html
   echo '   marginwidth="0" marginheight=0 name="TOC" noresize>' >> $MANBUILDDIR/HTMLMAN/manuals.html
   echo '  <frame src=' >> $MANBUILDDIR/HTMLMAN/manuals.html
   echo "   htmlman1/qacct.html$PATHREV" >> $MANBUILDDIR/HTMLMAN/manuals.html
   echo '   marginwidth="0" marginheight=0 name="PAGE" noresize>' >> $MANBUILDDIR/HTMLMAN/manuals.html
   echo ' </frameset>' >> $MANBUILDDIR/HTMLMAN/manuals.html
   echo '</HTML>' >> $MANBUILDDIR/HTMLMAN/manuals.html
endif
