#!/bin/ksh

#
# Copyright  Digital Equipment Corporation, 1995, 1996. All Rights Reserved.
#
# Restricted Rights: Use, duplication, or disclosure by the U.S. Government is
# subject to restrictions as set forth in subparagraph (C)(1)(ii) of DFARS
# 252.227-7013, or in FAR 52.227-14 Alt. III, as applicable. Unpublished
# rights reserved under applicable copyright laws.
#
# This software is proprietary to and embodies the confidential technology of
# Digital Equipment Corporation.  Possession, use, or copying of this software
# and media is authorized only pursuant to a valid written license from
# Digital or an authorized sublicensor.
#

abort()
{
    echo
    echo Installation aborted at user request.
    rm -f $TMP1 $TMP2
    exit 1
}

#
# Returns whether the target has enough diskspace to hold the source
# file.
# Args:
#   $1 - the target disk
#   $2 - the file to be put on the target
#
enufdisk()
{
    DISKFREE=$(df -nk $1 | awk 'NR==2 {print $4}')
    DISKREQD=$(ls -s $2 | awk '{print $1}')
    [ $DISKFREE -ge $DISKREQD ]
}

#
# Obtain from the installer the name of the directory to install the AWI
# components.
# Args:
#   $1 - name of variable to hold the returned directory name
#
getdir()
{
    echo 'Into which directory do you want the AWI sub-directory placed?'
    $ECHONNL ": $NNL"
    read ANS
    until [ -d "$ANS" ]; do
        if [ x$ANS != x ]; then
            echo "Directory $ANS does not exist."
            echo "You must specify an existing directory."
        fi
        echo 'Into which directory do you want the AWI sub-directory placed?'
        $ECHONNL ": $NNL"
        read ANS
    done
    eval $1="$ANS"
}

#
# Obtain from the installer the name of the directory to install the AWI
# components. The directory will hold enough diskspace for the components.
# Args:
#   $1 - name of variable to hold the returned directory name
#
getdest()
{
    getdir CMPTARG
    until enufdisk $CMPTARG $TARFILE ; do 
        echo "There is not enough diskspace on $CMPTARG"
        echo "to hold all the AWI components. Please choose another directory."
        echo
        getdir CMPTARG
    done
    eval $1="$CMPTARG"
}

#
# Main installation code
#

# initialize
trap "abort" 1 2 15
TARFILE="${PWD:-`pwd`}/AWIDEC010.tar"
TMP1=/tmp/awi.1.$$
TMP2=/tmp/awi.2.$$

# determine where the AWI should be placed
echo '
The ALL-IN-1 Web Interface (AWI) components are installed in their own separate
sub-directory. This sub-directory must be accessible by your Web server.
'
GO=0
until [ $GO -eq 1 ]; do
    getdest TARG
    echo
    echo "Install AWI sub-directory in $TARG"
    $ECHONNL "Is this correct? (y/n) $NNL"
    read ANS
    [ "$ANS" = "y" -o "$ANS" = "Y" ] && GO=1
done

# Get defaults for the awi.conf file
# tbs...

# Now do the actual installation.
echo '
Installing files... '

# create AWI subdir
AWIDIR="$TARG/awi"
[ -d $AWIDIR ] || mkdir $AWIDIR

# untar into aws subdir
(cd $AWIDIR; tar xpf $TARFILE)

# Update aws.conf with defaults
# tbs...

# Update services
grep -q "[ 	]$SRVPORT" /etc/services
[ $? -eq 1 ] && {
	echo "$SRVNAME	$SRVPORT	$SRVDESC" >> /etc/services
}

#
echo '
The AWI components have now been installed on your system. Before you can use
the AWI, you have to configure your Web server to map a URL (e.g. /awi) to
run the AWI image:'
echo "	$TARG/awi/bin/awi"
echo "as a CGI program."

exit
