#!/bin/csh -f
#
# 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
#
#___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__

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

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 ""
if ( $KVMAN == 1 ) then
   if ( -f CVS/Tag ) then
      setenv CVSBRANCH `sed 's/^T//' CVS/Tag`
      echo "==> Using CVS branch: ${CVSBRANCH}"
      setenv CVSBRANCH "-r $CVSBRANCH"
   else
      setenv CVSBRANCH ""
   endif
endif

cd $MANBUILDDIR

if ( $KVMAN == 1 ) then
   set MANSRCDIR = $MANBUILDDIR/MANSRC
   echo "Checking out manpage source"
   cvs -q co $CVSBRANCH -P -kv -d MANSRC gridengine/doc/man
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_Cxx = "Grid Engine" 
   set xxQS_NAME_EExx = "Grid Engine Enterprise Edition" 
   set xxqs_namexx = "SHOULD_NO_LONGER_OCCUR" 
   set xxQS_NAME_Sxx = "SGE" 
   set xxqs_name_sxx = "sge" 
   set xxRELxx = "GE 5.3"
else if ( $MANMODE == "geee" ) then
   set xxQS_NAMExx = "Grid Engine Enterprise Edition" 
   set xxQS_NAME_Cxx = "Grid Engine" 
   set xxQS_NAME_EExx = "Grid Engine Enterprise Edition" 
   set xxqs_namexx = "SHOULD_NO_LONGER_OCCUR" 
   set xxQS_NAME_Sxx = "SGE" 
   set xxqs_name_sxx = "sge" 
   set xxRELxx = "GEEE 5.3"
else if ( $MANMODE == "sge" ) then
   set xxQS_NAMExx = "Sun Grid Engine"
   set xxQS_NAME_Cxx = "Sun Grid Engine"
   set xxQS_NAME_EExx = "Sun Grid Engine, Enterprise Edition"
   set xxqs_namexx = "SHOULD_NO_LONGER_OCCUR"
   set xxQS_NAME_Sxx = "SGE"
   set xxqs_name_sxx = "sge"
   set xxRELxx = "SGE 5.3"
else if ( $MANMODE == "sgeee" ) then
   set xxQS_NAMExx = "Sun Grid Engine, Enterprise Edition"
   set xxQS_NAME_Cxx = "Sun Grid Engine"
   set xxQS_NAME_EExx = "Sun Grid Engine, Enterprise Edition"
   set xxqs_namexx = "SHOULD_NO_LONGER_OCCUR"
   set xxQS_NAME_Sxx = "SGE"
   set xxqs_name_sxx = "sge"
   set xxRELxx = "SGEEE 5.3"
else
   echo error wrong MANMODE=$MANMODE. Exit
   exit 1
endif

echo "Building SEDMAN pages"
foreach n ( 1 3 5 8 )
   echo xxxx $MANSRCDIR/man${n}
   if ( ! -d $MANSRCDIR/man${n} ) then
      echo "Error: No man${n} subdirectory."
      exit 1
   endif

   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_namexx/$xxqs_namexx/g" \
          -e "s/xxQS_NAME_Sxx/$xxQS_NAME_Sxx/g" \
          -e "s/xxqs_name_sxx/$xxqs_name_sxx/g" \
          -e "s/xxQS_NAME_EExx/$xxQS_NAME_EExx/g" \
          -e "s/xxQS_NAME_Cxx/$xxQS_NAME_Cxx/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 ( 1 3 5 8 )
      if ( ! -d $MANBUILDDIR/SEDMAN/man/man${n} ) then
         echo "Error: No SEDMAN/man/man${n} subdirectory."
         exit 1
      endif

      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
   echo "Building HTML from sedman pages"
   foreach n ( 1 3 5 8 )
      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
         man -M$MANBUILDDIR/SEDMAN/man/ $bn | man2html -cgiurl '../htmlman$section/$title.html' -solaris > $MANBUILDDIR/HTMLMAN/htmlman${n}/${bn}.html
      end
   end   
   #
   # generate an index file 
   #
   cp $1/scripts/template.html $MANBUILDDIR/HTMLMAN/index.html 
   foreach n ( 1 3 5 8 )
      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" >> $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
endif
