
# This is the standard man command, enhanced for AIX.

# Function to setup a temporary file name
setuptemp () { if [ -z "$tempfile" ]
               then
                 tempdir=`pwd`
                 if [ -d /tmp ]
                 then tempdir='/tmp'
                 fi
                 tempfile=$tempdir/man
                 # get a unique name.
                 while :
                 do
                   if [ -f $tempfile ]
                   then tempfile=$tempfile'1'
                   else break
                   fi
                 done
               fi; }


# Change Activity:
#   06/23/87 - (jjs) Added "man ?" capability, displays helps.
#   06/23/87 - (jjs) Made system and local man base directories variable.
#   06/25/87 - (jjs) Fixed the way options are handled.
#   06/25/87 - (jjs) Added -d dir option
#

pgmname=`basename $0`
# Break out the options -n -e
parms='?ne:r:'
set -- `getopt $parms $* 2>/dev/null`

# Display helps if no params or "?".
[ $? != 0 -o $# = 1 -o -z "$1" -o "$1" = "-?" ] && { cat << END 1>&2; exit 1; }
Usage:  $pgmname [-e editor | -n] [<section>] <titles> ...
        "-e -" means do not use any editor.
        "-n" is like "-e -", except no pagination is done.
        "-r id" uses the TCPIP rexec to run man on another host.
        if neither -e nor -n is specified, the editor, if
        any, named by the EDITOR environment variable is used.
        Valid <section> numbers are:
        1 (Commands)            5 (File Formats)
        2 (System Calls)        6 (Games)
        3 (Subroutines)         7 (Miscellaneous Facilities)
        4 (Special Files)
        The <section> number may be changed before each <title>.
        If <section> is not specified or if <title> is not found
        in the specified <section>, then all sections are searched.
END

rc=0
# These directory variables should be tailored to your installation
mandir='/usr/man'
localmandir=$mandir/local
nroffmandir=$mandir/nroff

manpost='| pg'
remote=
s=

edit=

# process options.
while [ $1 != -- ]
do
  case "$1" in
          -e    ) edit=$2
                  shift 1;;
          -n    ) manpost=
                  edit='-';;
          -d    ) localmandir=$2; shift;;
          -r    ) remote=$2; shift;;
  esac
  shift 1
done
shift 1
names=$*

# Get remote info if -r
if [ -n "$remote" ]
then
  setuptemp
  rexec -n $remote $pgmname -n $names >$tempfile 2>&1
  if [ $? != 0 ]
  then exit 2
  fi
  set -- -
fi

# process parameters.
for arg
{
    case "$arg" in
      [1-8] ) s=$arg; continue;;
      -     ) file=$tempfile; match=$tempfile;;
      *     ) match=
              if [ -n "$s" ]
	      then match=`li -1 $nroffmandir/man$s/$arg.$s* $localmandir/man$s/$arg.$s* $mandir/man$s/$arg.$s* 2>/dev/null`
              fi

              if [ -z "$match" ]
	      then match=`li -1 $nroffmandir/man*/$arg* $localmandir/man*/$arg* $mandir/man?/$arg.[1-8]* 2>/dev/null`
              fi
    esac

    # Determine the editor to use if any.
    if [ -z "$edit" ]
    then
      if [ -n "$EDITOR" ]
      then edit=$EDITOR
      fi
    fi


    for file in ${match:-none}
    {
        if [ -n "$edit" -a "$edit" != "-" ]
        then
           # find a place for temporary files.
           setuptemp

           if [ "$file" != "$tempfile" ]
           then
             case "$file" in
                     *.z )   pcat $file >$tempfile;;
                     *.* )   cat $file  >$tempfile;;
		     * )     if test -s "$file"
			     then
				nroff $file >$tempfile
			     else
				echo "mann: \"$arg\" not found." 1>&2
				rc=2
				continue
			     fi;;
             esac
	   fi

	   echo $tempfile
           $edit $tempfile
        else
           case "$file" in
                   *.z )   if [ -z "$manpost" ]
                           then pcat $file
                           else pcat $file | pg
                           fi;;
                   *.* |$tempfile ) if [ -z "$manpost" ]
                                   then  cat $file
                                   else  cat $file | pg
                                   fi;;
		     * )   if test -s "$file"
			   then
			      nroff $file
			   else
			      echo "mann: \"$arg\" not found." 1>&2
			      rc=2
			   fi;;
           esac
        fi
       # Delete the file if it's a temp.
       if [ -n "$tempfile" ]
       then rm -f $tempfile
       fi
    }

}

exit $rc
