#!/bin/sh
# 
# $Copyright
# Copyright 1995  Intel Corporation
# INTEL CONFIDENTIAL
# The technical data and computer software contained herein are subject
# to the copyright notices; trademarks; and use and disclosure
# restrictions identified in the file located in /etc/copyright on
# this system.
# Copyright$
# 
# This script is used to install the system acceptance tests.  It assumes
# that the compressed tar file for the SATs resides on a remote system and
# prompts the user to get information for retrieving it. 

DEFAULTS_FILE=/etc/defaults/install
DIST_ADDRESS=0.0.0.0
DIST_NODENAME=unknown
DIST_PATH=/distribution
ASK_QUESTIONS=0


###########################################################################
# FUNCTION:     GetTarFiles
#
#       This function will go to the distribution system and retrieve the
#       compressed tar file containg the system acceptance tests.  If a failure 
#       happens, the user will be given the chance to change the information.
###########################################################################
GetTarFiles()
{
        Done=0
        while [ $Done -eq 0 ]; do
                echo "Username for FTP'ing files from $DIST_NODENAME:[anonymous] \c"
                read FTP_USER
                if [ -z "$FTP_USER" ]; then
                        FTP_USER=anonymous
                fi
                ftp -n -v $DIST_ADDRESS <<EOF1
                     user $FTP_USER
                     binary
                     lcd /home
                     cd $DIST_PATH
                     get sat.tar.Z
		     get sat.doc.tar
                     quit
EOF1


# Check to make sure the compress tar file has been copied down to the target
# machine.  If the file was not retrieved, give the user a change to change
# the information. 
                Fail=0
                if [ ! -f /home/sat.tar.Z ]; then
                        echo "ERROR: sat.tar.Z was not retrieved."
                        Fail=1
                fi
                if [ ! -f /home/sat.doc.tar ]; then
                        echo "ERROR: sat.doc.tar was not retrieved."
                        Fail=1
                fi

                if [ $Fail -eq 0 ]; then
# Install the system acceptance tests.
		   echo "Uncompressing sat.tar.Z..."
		   uncompress /home/sat.tar.Z
                   if [ $? -ne 0 ]; then
                           Fail=1
                   	   rm -f /home/sat.tar.Z
		   else
		      echo "Installing SATs..."
                      cd /
		      tar xf /home/sat.tar 
                      if [ $? -ne 0 ]; then
                              Fail=1
                      fi
                      rm -f /home/sat.tar
                   fi
		   if [ $Fail -eq 0 ]; then
		      echo "Installing SAT documentation..."
			PWD=`pwd`
			cd /usr/share
			tar xpf /home/sat.doc.tar	
                        if [ $? -ne 0 ]; then
                              Fail=1
                        fi
			cd $PWD
                        rm -f /home/sat.doc.tar
		   fi
                fi
	        if [ $Fail -eq 0 ]; then
                   Done=1
                else 
                   echo "There were errors retrieving, uncompressing, or"
		   echo "untarring the system acceptance tests."
		   ASK_QUESTIONS=0
                   AskQuestions
                fi
        done
}

###########################################################################
# FUNCTION:     AskQuestions()
#
#       Ask questions to get all the info
###########################################################################
AskQuestions()
{
        Qdone=0
        while [ $Qdone -eq 0 ]; do

             if [ $ASK_QUESTIONS -eq 1 ]; then
                   echo "Distribution Node Name [$DIST_NODENAME]: \c"; read ans
                   if [ ! -z "$ans" ]; then
                        DIST_NODENAME=$ans
                   fi
                   echo "Distribution IP Address [$DIST_ADDRESS]: \c"; read ans
                   if [ ! -z "$ans" ]; then
                        DIST_ADDRESS=$ans
                   fi
                   echo "Distribution Path [$DIST_PATH]: \c"; read ans
                   if [ ! -z "$ans" ]; then
                        DIST_PATH=$ans
                   fi

		fi
		ASK_QUESTIONS=1
                echo "\n"
                echo "================= RESPONSE SUMMARY ==============================="
                echo "Distribution Node:                       "$DIST_NODENAME
                echo "Distribution IP Addr:                    "$DIST_ADDRESS
                echo "Distribution Path:                       "$DIST_PATH
                echo "------------------------------------------------------------------"
                echo "Is the above information correct? (y/n/q)[n] \c "
                read ans
                if [ -z "$ans" ]; then
                        ans="N"
                fi
                if [ $ans = "y" -o $ans = "Y" ]; then
                        Qdone=1
                fi
                if [ $ans = "q" -o $ans = "Q" ]; then
                        Qdone=1
                        Done=1
                fi
        done
}
###########################################################################
#                      S T A R T  O F  I N S T A L L A T I O N
###########################################################################

OLD_UMASK=`umask`
umask 022
if [ -f $DEFAULTS_FILE ]; then
	DIST_ADDRESS=`grep DIST_ADDRESS $DEFAULTS_FILE | cut -f2 -d' '`
	DIST_NODENAME=`grep DIST_NODENAME $DEFAULTS_FILE | cut -f2 -d' '`
	DIST_PATH=`grep DIST_PATH $DEFAULTS_FILE | cut -f2 -d' '`
fi
if [ -z "$DIST_ADDRESS" ]; then
	DIST_ADDRESS=0.0.0.0
	ASK_QUESTIONS=1
fi
if [ -z "$DIST_NODENAME" ]; then
	DIST_NODENAME=unknown
	ASK_QUESTIONS=1
fi
if [ -z "$DIST_PATH" ]; then
	DIST_PATH=/distribution
	ASK_QUESTIONS=1
fi

AskQuestions
if [ $ans != "q" -a $ans != "Q" ]; then
   GetTarFiles
fi
umask $OLD_UMASK
exit 0
