#! /bin/sh
#
# @(#)installman	1.10	LPS_UNX_COM	03/23/95
#
# Copyright 1995   Digital Equipment Corporation, Maynard, MA
#
# installman <rootsrc> <rootdst> <sectype>
#
# This script either installs or deinstalls all LPS "man page" files
# on the local system.
#
# The root of the destination tree and all associated subdirectories are
# initialized if they don't already exist.
#
# This script expects to be invoked by the "lpsinstall" script; as such,
# a running display of file actvitity is written to stdout.
#
# Parameters:
#    $1 - Path of a file to receive a description of the installation results,
#         or "-d" to request deinstallation.
#
# Global variables:
#    ENV_LPSKIT
#    ENV_LPSROOT
#    LINKCHECKOPTS
#    MANROOT
#    TMPDIR
#    WHOAMI
#
# Exit values:
#    0 - Success, at least some files were installed/deinstalled.
#
#    1 - Failure, either a destination directory could not be initialized, or
#	 a file could not be installed/deinstalled, or a cmdline error was
#	 encountered.  Script-produced error messages always go to stdout.
###

# Determine whether this is an install or deinstall situation

if [ -z "$1" ]
then
    echo "`basename $0`: no command line parameters specified!"
    exit 1
fi

if [ $1 = "-d" ]
then
    LPSDEINSTALL=true
else
    OUTFILE=$1
    LPSDEINSTALL=false
fi

: ${TMPDIR:=/tmp}

MANROOTSRC=$ENV_LPSKIT/man

MANROOTFILE=$ENV_LPSROOT/manroot
MANMAPFILE=$ENV_LPSROOT/manpages.map


results ()
{
    cat > $OUTFILE << EOF
The online manual page files were $*
EOF
    return 0
}

cantdeinstall ()
{
    echo "    Unable to deinstall the online manual page files."

    if [ "$*" ]
    then
	echo "    Reason:  $*"
	echo
    fi

    pause

    exit 1	# Exit right now with a failure indication
}


# Ensure the $MANMAPFILE exists; if it doesn't then we're outa luck

if [ ! -f $MANMAPFILE ]
then
    if $LPSDEINSTALL
    then
	cantdeinstall "manual page mapping file ($MANMAPFILE) is missing."
    fi

    cat << EOF
    The online manual page mapping file is missing!

    Missing file path: $MANMAPFILE

    This file should be present with the software, but for some reason
    it is missing.

    If you want the online manual page files installed
    on your system, then you will have to reinstall the software from
    the distribution media.

    Otherwise, you may continue with the installation without the
    online manual page files.
EOF
    results "not installed due to a missing kit file ($MANMAPFILE)."

    pause

    exit 1
fi

# Check to see if the $MANROOTFILE exists, and if so, then fetch the path
# from that file and use that path as the root of the man directory tree.
# If the file doesn't exist, the use the path defined by the externally
# set variable $MANROOT.
#
# For deinstall, if this file doesn't exist, then there's nothing we can
# or SHOULD do, since we don't know where the man pages exist.  Besides,
# having a few innocuous man page files laying around shouldn't hurt
# anyone, anyway.

if [ -f $MANROOTFILE ]
then
    MANROOT=`cat $MANROOTFILE`
else
    if $LPSDEINSTALL
    then
 	cantdeinstall "manual page root file ($MANROOTFILE) is missing."
    fi
fi

# If the usual directory tree root ($MANROOT) exists, then we use that
# path...but only if it is writable by our process; afterall, the tree
# may be read-only via NFS for this system, in which case we would not
# be able to install files into that tree.
#
# If, on the other hand, the $MANROOT path does not exist, then we
# attempt to create it.
#
# If we can't use the path specified in $MANROOT for any reason (not
# writable or we can't create it), then we fall back to using (and creating)
# the tree starting at the "man" directory in the LPSROOT installation
# directory ($ENV_LPSROOT/man).
#
# Recall that to remove a file (in the case of de-installation), the
# directory must still be writable by us.

if [ -z "$MANROOT" ]
then
    if $LPSDEINSTALL
    then
	cantdeinstall "no path found in the man page root file ($MANROOTFILE)".
    else
	MANROOTLPS=$ENV_LPSROOT/man   # Default to within the LPSROOT tree
    fi
fi

manmsg="The man root directory ($MANROOT)"

if [ -d $MANROOT ]		# Directory path is set and it exists?
then
    if [ -w $MANROOT ]		# Directory root is writable?
    then
	:
    else
	if $LPSDEINSTALL
	then
	    cantdeinstall "man page root directory ($MANROOT) is not writable".
	fi

	echo "$manmsg exists, but is not writable by $WHOAMI."

	MANROOT=""    # Null value caught and handled below
    fi
else
    if $LPSDEINSTALL
    then
	cantdeinstall "man page root directory ($MANROOT) does not exist".
    fi

    echo "$manmsg does not exist."
    echo "Creating directory $MANROOT ..."

    if mkdir -p $MANROOT
    then
	:
    else
	MANROOT=""		  # Null value caught and handled below
    fi
fi

if [ -z "$MANROOT" ]		  # Can't use originally intended directory?
then
    MANROOTLPS=$ENV_LPSROOT/man   # Default to within the LPSROOT tree
fi

echo

if $LPSDEINSTALL
then
    echo "Removing online manual page files from $MANROOT ..."
else
    echo "Installing online manual page files in $MANROOT ..."
fi

# Copy all man page files into the proper location based on the
# contents of the mapping file.  If we're installing the files
# into $ENV_LPSROOT, then remove the file after it is copied
# so as to avoid unecessary wasted disk space...as long as the
# source and destination paths are not the same!
#
# Note an interesting wierdity here in that we must use temp files
# to handle state information concerning success/failure, as it is
# quite likely that a Bourne shell will execute the following "while"
# loop in a sub-shell and not our current shell; hence, any variables
# set in the sub-shell will not be available to us when the loop
# terminates.

somesuccess=$TMPDIR/success.$$
errors=$TMPDIR/errors.$$

rm -f $somesuccess $errors

while read srcfile dstfile
do
    srcpath=$MANROOTSRC/$srcfile
    dstpath=$MANROOT/$dstfile

    # If this is a deinstallation, then all we need is the resolved
    # destination path of the file; remove that path and continue
    # the loop without further delay.  Let the user see any errors
    # that might occur.

    if $LPSDEINSTALL
    then
	rm $dstpath
	continue	# Continue the loop
    fi

    # Installation continues here

    srcdir=`dirname $srcpath`
    dstdir=`dirname $dstpath`

    # Resolve the destination directory and create it if it doesn't exist;
    # if we can't create it, then complain and skip this file

    if [ ! -d $dstdir ]
    then
	if installdir $dstdir 755 bin bin
	then
	    :
	else
	    echo
	    echo "    Unable to create directory: $dstdir"
	    echo "    Unable to install man page file: $srcfile"

	    errors=true

	    continue		# Skip this line, go on to the next line
	fi
    fi

    if cp $srcpath $dstpath	# Let the user see/catch any errors
    then
	touch $somesuccess
    else
	touch $errors
    fi

done < $MANMAPFILE

# If this is a deinstallation, then we're done

if $LPSDEINSTALL
then
    exit 0
fi

# Create/update the "manroot" file that contains the location of
# the man page directory tree root path, then create the final
# results description in the target output file.

echo $MANROOT > $MANROOTFILE

if [ -f $somesuccess ]		# At least one file installed?
then
    if [ -f $errors ]		# Not all files installed?
    then
	MANRESULTS="partially installed in $MANROOT (errors were encountered)"
    else
	MANRESULTS="successfully installed in $MANROOT"
    fi
else
    MANRESULTS="were not installed due to errors."
fi

results "$MANRESULTS"

exit 0
