#!/bin/sh

THISDIR=`dirname "$0"`
DIRNAME=`readlink -f "$THISDIR/.."`
PROGNAME=`basename "$0"`

if [ "$1" = '--help' ]; then
    cat 1>&2 <<EOF
Usage: $PROGNAME <l4-directory> [ --clean ]

Copy the distributed packages into the "pkg" hierarchy found within the
specified directory. Also copy the distributed configuration examples into the
"conf" hierarchy.

The indicated directory need not be called "l4", but it must at least have a
"conf" directory and a "pkg" directory inside it.

Specifying --clean removes copied objects.
EOF
    exit 1
fi

# Test for a directory argument and whether it exists.

if [ ! "$1" ] || [ ! -e "$1" ] ; then
    cat 1>&2 <<EOF
Please indicate the l4 directory containing a pkg directory hierarchy.
EOF
    exit 1
fi

# Test for an appropriate directory.

if [ ! -e "$1/pkg" ] ; then
    cat 1>&2 <<EOF
The indicated directory does not appear to contain a pkg directory hierarchy.
EOF
    exit 1
fi

L4DIR=$1

# Determine the mode.

if [ "$2" = '--clean' ] ; then
    CLEAN=$2
else
    CLEAN=
fi

# Check the configuration, if possible.

"$THISDIR/checkconfig.sh" -q "$L4DIR"

# Generate binaries if appropriate.

if [ ! "$CLEAN" ] ; then
    if ! "$THISDIR/makefonts.sh" -q ; then
        cat 1>&2 <<EOF

Install halted due to font generation problem.
EOF
        exit 1
    fi
fi

# Copy (or remove) each of the objects.

for OBJTYPE in 'conf' 'pkg' ; do
    TARGETDIR=`readlink -f "$L4DIR"`/$OBJTYPE
    SOURCEDIR="$DIRNAME/$OBJTYPE"

    # If cleaning, remove all objects.

    if [ "$CLEAN" ] ; then
        for OBJECT in "$SOURCEDIR/"* ; do
            OBJNAME=`basename "$OBJECT"`
            ORIGIN=`readlink -f "$OBJECT"`
            TARGET="$TARGETDIR/$OBJNAME"

            if [ -d "$TARGET" ] ; then
                rm -r --one-file-system "$TARGET"
            else
                rm "$TARGET"
            fi
        done

        continue
    fi

    # Make directories.

    for OBJECT in `find "$SOURCEDIR" -type d -print` ; do
        ORIGIN=`readlink -f "$OBJECT"`
        RELPATH=${ORIGIN#$SOURCEDIR/}

        # Skip top-level directories.

        if [ "$RELPATH" = "$ORIGIN" ]; then
            continue
        fi

        TARGET="$TARGETDIR/$RELPATH"

        if [ ! -e "$TARGET" ]; then
            mkdir "$TARGET"
        fi
    done

    # Copy new files.

    for OBJECT in `find "$SOURCEDIR" -type f -not -name '.*' -not -name '*.orig' -not -name '*.rej' -print` ; do
        ORIGIN=`readlink -f "$OBJECT"`
        RELPATH=${ORIGIN#$SOURCEDIR/}
        TARGET="$TARGETDIR/$RELPATH"

        if [ ! -e "$TARGET" ] || [ "$ORIGIN" -nt "$TARGET" ] ; then
            cp "$ORIGIN" "$TARGET"
        fi
    done

    # Remove obsolete files.

    for OBJECT in "$SOURCEDIR/"* ; do
        OBJNAME=`basename "$OBJECT"`
        ORIGIN=`readlink -f "$OBJECT"`

        # Examine the target object directory for files that are not provided
        # by the distribution.

        TARGET="$TARGETDIR/$OBJNAME"

        for FILENAME in `find "$TARGET" -type f -not -name '.*' -not -name '*.orig' -not -name '*.rej' -print` ; do
            PATHNAME=`readlink -f "$FILENAME"`
            RELPATH=${PATHNAME#$TARGET}
            SOURCE="$ORIGIN/$RELPATH"

            if [ ! -e "$SOURCE" ] ; then
                rm "$PATHNAME"
            fi
        done

        # Remove obsolete directories.

        for FILENAME in `find "$TARGET" -depth -type d -print` ; do
            PATHNAME=`readlink -f "$FILENAME"`
            RELPATH=${PATHNAME#$TARGET}
            SOURCE="$ORIGIN/$RELPATH"

            if [ ! -e "$SOURCE" ] ; then
                rmdir "$PATHNAME"
            fi
        done
    done
done
