#!/bin/sh
#
# This script uses a number of existing shell scripts, awk scripts and
# data files to generate a script that can be used to generate another
# file that can be fed to a tar command for extracting one or more
# files off the tape.
#
# The audit-file is used to keep track of what scripts have run
# to completion and specifically, what they did.
#
#

Usage="Usage: $0 <installdir> <destdir> <packages> <sizes> [log] [media]"

ScriptTag="BUILD_INSTALL_LIST"

if [ $# -lt 4 ]
then
    echo $Usage; exit 1
fi

INSTALLDIR=$1
DESTDIR=$2
PACKAGE_LIST=$3
PACKAGE_SIZES=$4
AUDITLOG=$5
MEDIAMODE=$6

if [ "$AUDITLOG" ]
then
    if [ ! -f "$AUDITLOG" ]
    then
    	echo "WARNING: Audit file $1 does not exist..."
    	AUDITLOG=
    fi
fi

#
# If we are reading an audit file that indicates that we have gotten
# this far before, then skip execution and go on
#

if [ "$AUDITLOG" ]
then
    PREVRUN=`grep $ScriptTag $AUDITLOG`

    if [ "$PREVRUN" ]
    then
    	echo "Using file list generated by previous install"
    	echo ""
    	echo $PREVRUN
    	exit 1
    fi
fi

#
# Use this file to build the selection part of the install proceedure
#

$INSTALLDIR/sel_file_pkgs $INSTALLDIR $DESTDIR $PACKAGE_LIST $PACKAGE_SIZES "$MEDIAMODE"

if [ $? -ne 0 ]
then
    echo "No files were selected for installation" >> $AUDITLOG
    exit 1
fi

#
# Put the appropriate entries into the audit-log 
#
if [ "$AUDITLOG" ]
then
#   put entry into the audit log, this not only indicates that the
#   script has been run, but can be executed to see what files were
#   selected.
#
    echo ""					>> $AUDITLOG
    echo "BUILD_INSTALL_LIST: "`date`		>> $AUDITLOG
    echo ""					>> $AUDITLOG
    echo "Specifically Selected Packages..."	>> $AUDITLOG
    echo ""					>> $AUDITLOG
    cat $INSTALLDIR/TEKXP_INST.LST 		>> $AUDITLOG
fi

exit 0