#! /bin/sh
#set -xv
#=========================================================================
# Copyright (C) VMware, Inc. 1986-2011.  All Rights Reserved..
#
# Name - fixman.sh
#
# Purpose - Make GemStone man page whatis database, or whatever else
#	    may be appropriate for the target system...
#
# This script assumes it is in $GEMSTONE/doc, so it can find help.sh, etc.
#
# $Id: fixman.sh,v 1.6 2008-01-09 22:50:52 stever Exp $
#
#=========================================================================

if [ "a$GEMSTONE" = "a" ]; then
  echo "ERROR: GemStone scripts require a GEMSTONE environment variable."
  echo "       Please set it to the directory where GemStone resides."
  exit 1
fi

# make sure of a minimum path
PATH=:/bin:/usr/bin:/usr/ucb:$PATH; export PATH

comid="fixman"                           #   this script's name

. $GEMSTONE/bin/misc.sh

# error control is special!
trap ' echo "${comid}[Error]:   ^C termination."; \
    chmod u-rw $manRoot $GEMSTONE; exit 1' 2
trap 'echo "${comid}[Error]:   kill termination."; \
    chmod u-rw $manRoot $GEMSTONE; exit 1' 15
trap "chmod u-rw $manRoot $GEMSTONE; exit 1" 1

usageStr="fixman [-h]\n\
Post-process GemStone man pages for on-line interactive use."

giveHelp "$1" "$usageStr"

initConfig ""

# This script takes at most one argument, "-c", to suppress "catman" creation
doCats=y
case "x$1" in
  x-c) doCats=n ;;
  x) ;;
  *) # say shell.peculiar $comid "$1"
     echo "${comid}[Error]:   Peculiar argument \"%s\" "
     exit 1
     ;;
esac

# Set up all GemStone ancillary options
appName=""
. $GEMSTONE/bin/setconfig.sh

manRoot=$GEMSTONE/doc; export manRoot
chmod u+rw $GEMSTONE $manRoot

# cleanup
rm -f $manRoot/whatis
if [ ! -d $manRoot ]; then
  # what? no man directory?
  # say fixman.mandir $manRoot
  echo "fixman[Error]:    Cannot find man directory $manRoot "
  exit 1
fi
cd $manRoot

#=========================================================================
# Do the whatis database...
case "$HOSTTYPE.$OSTYPE" in
  sparc.Solaris) # Sun SPARC, Solaris
    PATH=$PATH:/usr/ucb		# for /usr/ucb/hostname.  Yuccch.
    export PATH
    doMakeWhatis() { /usr/lib/makewhatis $manRoot ; status=$? ; }
    ;;
  hppa.hpux*) # HP9000/HPUX
    # /usr/lib/mkwhatis unsatisfactory, since it attempts to write
    # /usr/lib/whatis.
    # So we use a public domain makewhatis package, instead.
    doMakeWhatis() { $GEMSTONE/doc/makewhatis $manRoot ; status=$? ; }
    ;;
  RISC6000.AIX) # IBM RIOS/AIX 
    # man page doesn't say that distributed whatis database is supported,
    # but the makewhatis command supports it.  What the heck....
    doMakeWhatis() { /usr/lbin/mkwhatis $manRoot ; status=$? ; }
    ;;
  i686*inux)
    doMakeWhatis() { /usr/sbin/makewhatis $manRoot ; status=$? ; }
    ;;
  *)
    echo "fixman: WARNING:  unknown hosttype $HOSTTYPE.$OSTYPE encountered"
    doMakeWhatis() { status=1 ; }
    ;;
esac

doMakeWhatis

if [ -f $manRoot/whatis ]; then
  chmod 444 $manRoot/whatis
fi

if [ $status -ne 0 ]; then
  exit $status
fi

#=========================================================================

# Expand (i.e. "cat1") the man pages

if [ $doCats = n ]; then
  exit 0
fi
 
case "$HOSTTYPE.$OSTYPE" in
  sparc.Solaris) # SPARC Solaris
    /usr/bin/catman -n -M $manRoot
    status=$?
    ;;
  hppa.hpux*) # HP9000/HPUX
    # catman uses directories in MANPATH to do its work...
    MANPATH=$manRoot; export MANPATH
    /etc/catman -n
    status=$?
    ;;
  RISC6000.AIX) # IBM RIOS/AIX
    # redirected stderr to fix bug 12285
    /usr/sbin/catman -n -M $manRoot 1358 2>/dev/null
    status=$?
    if [ $status -ne 0 ]; then
      echo "fixman: WARNING: '/usr/sbin/catman -n -M $manRoot 1358' failed"
    fi
    ;;
  i686*inux)
    # say fixman.nocatman
    echo "fixman[Warning]: This architecture does not have a catman command."
    status=0
    ;;
  *)
    echo "fixman: WARNING:  unknown hosttype $HOSTTYPE.$OSTYPE encountered"
    status=0
    ;;
esac

chmod u-w $manRoot $GEMSTONE
exit $status
