#! /bin/sh
#
# @(#)initdir	1.7	LPS_UNX_COM	02/19/95
#
# Copyright 1994   Digital Equipment Corporation, Maynard, MA
#
# initdir
#
# Shell script to create and set the ownership and protection of a target
# production directory, and copy all related files from a given source
# directory.  If the directory already exists, then the target directory
# ownership and protection are assumed to be already properly set.
#
#    NOTE:  This script is designed to be invoked by one of the many
#	    "inst.*" installation scripts.  As such, to make things
#	    a lot cleaner in the calling script, the exit values are
#	    inverted:  0 implies failure, and 1 implies success!
#
# Parameters:
#     $1 - Target directory path
#     $2 - Mode number (protection settings) of the Target directory
#     $3 - Owner id (name or uid) of target directory and associated files.
#     $4 - Group id (name or uid) of target directory and associated files.
#     $5 - Source directory path (can be a null string to indicate no source
#	   directory or files, only that the target dir is to be inited).
#     $6 - Quoted list of source base filenames to install; this parameter
#          can be null, but if $5 is null, this parameter must also be null.
#     $7 - Mode number (protection settings) of the installed files
#     $8 - String describing the purpose of the directory
#
# A formatted display of the actions is output on stdout.
#
# Note the restriction that the directory and all associated files
# receive the same owner/group ownership.
#
# Global variables:
#    ECHON
#    INITDIRPROC
#    LINKCHECKOPTS
#    LPSDEBUG
#    TMPDIR
#
# Exit values:
#    0 - Failure, either the directory creation failed, or the installation
#	 of one of the files failed.
#    1 - Success, the directory was initialized and all files copied to it.
###

: ${TMPDIR:="/tmp"}

DSTDIR=$1
DIRMODE=$2
OWNER=$3
GROUP=$4
SRCDIR=$5
FILELIST="$6"
FILEMODE=$7
DESC="$8"

logfile=$TMPDIR/initdir.$$
rerun="Please try to repair this problem, then re-run this script."

: ${SAMEDIR:="    [source and destination directories are the same]"}

ABORT="kill -1 $$"

# Setup activities to be executed upon encountering an error condition

trap 'echo ; cat $logfile ; echo $rerun ; echo ; exit 0'  1 2 3 15

echo
$ECHON "  $DSTDIR ($DESC): "

# Create the target production directory and set its mode/group

if [ -d $DSTDIR ]   # Directory already exists?
then
    echo "[exists]"

    # If a source directory is specified, then check to see if the
    # source and target directory paths actually reference the same
    # directory.

    if [ "$SRCDIR" ]      
    then
	if linkcheck $LINKCHECKOPTS "$DSTDIR" "$SRCDIR"
	then
	    echo "$SAMEDIR"
	fi
    fi
else
    if installdir $DSTDIR $DIRMODE $OWNER $GROUP > $logfile 2>&1
    then
	echo "[created]"
    else
	$ABORT
    fi
fi

# Install all files from the source directory to the target directory
# with the specified mode.

if [ "$LPSDEBUG" ] ; then $ECHON "    Files: " ; fi

for f in $FILELIST
do
    file=$SRCDIR/$f

    if [ -d $file ]    # Don't descend into subdirectories
    then
       continue
    fi

    if installfile $file $DSTDIR $FILEMODE $OWNER $GROUP STRIP > $logfile 2>&1
    then
	if [ "$LPSDEBUG" ] ; then $ECHON " $f" ; fi
    else
	echo
	echo "UNABLE TO INSTALL $file in $DSTDIR !!"
	echo
	$ECHON "Reason: "
	$ABORT
    fi
done


if [ "$LPSDEBUG" ]
then
    if [ -z "$FILELIST" ]
    then
	echo "[none]"
    else
	echo
    fi
fi

rm -f $TMPDIR/*.$$

exit 1
