#!/bin/sh 
#
# hw130_install
#
#       HW130 Host Software Installation
#
# Copyright (c) 1996 Xilinx, Inc.
#
# This file contains proprietary and confidential information and
# remains the unpublished property of Xilinx, Inc. Use, disclosure,
# or reproduction is prohibited except as permitted by express
# written license agreement with Xilinx, Inc.
#

#set -x

PATH=.:/bin:/usr/bin:/usr/ucb
#export PATH
#umask 0
 
prog_name=hw130_install      # install program name
hostProgName=hw130           # host program executable name

errorFlag=0                  # initialize to no error.
destinationPath=$HOME        # initialize to user's home directory
kbBuffer=
currentFile=
destinationFile=
listFile=hwfile.lst
dirExistsFlag=0

trap 'echo "Abnormal Termination."; errorFlag=1' 1 2 15

#
# Check and Display OS revision
#
os_revision ()
{
   case "`uname -sr`" in
        SunOS\ 4.*)             echo "Operating system: SunOS 4.1.X";;
        SunOS\ 5.*)             echo "Operating system: SunOS 5.X";;
        HP-UX*)                 echo "Operating system: HPUX";;
        AIX*)                   echo "Operating system: AIX";;
        *)                      echo "Unsupported host operating system."; errorFlag=1;;
   esac
}
#
# Get YES or NO answer from the user
#
yesNo=0
yesNoMessage="Directory exists. Overwrite files? (y/n)"
yesNoPrompt ()
{
   while :
   do
     echo $yesNoMessage
     read yesNo
     if test $errorFlag = 0
     then 
        case $yesNo in
           [Yy])             yesNo=0;return 0;;
           [Nn])             yesNo=1;return 1;;
           *)                echo "Answer y on n.";;
        esac
     else
        return 0
     fi
   done
}
 
#
# Copy files from current directory to destination directory
#
copy_files ()
{
   if test $errorFlag = 0
   then
      # create destination directory  if needed
      if test $dirExistsFlag = 0
      then
         mkdir $destinationPath 
         errorFlag=$? # get result of mkdir
      fi
     
      if test $errorFlag = 0
      then 
         listFile=$destinationPath/$listFile 

         if find . -name "*" -print > $listFile
         then 
            while read currentFile
            do
               if test -f $currentFile  # copy regular files only.
               then
                  currentFile=`basename $currentFile` 
                  destinationFile=$destinationPath/$currentFile
                  echo "  Copying $currentFile."
                  if cp -p $currentFile $destinationFile # preserve file attributes
                  then
                     :  # do nothing
                  else
                     exit 1
                  fi
               fi 
            done < $listFile
            errorFlag=$?  #get result of redirected i/o loop (in subshell)
         else
            errorFlag=1  #find error
         fi
      else
         errorFlag=1  #mkdir error
      fi
   fi 
}
#
# Display termination message
#
display_end_message ()
{
   echo ""
   if test $errorFlag = 0
   then
      echo "Installation is successful."
   else
      echo "Installation is NOT successful."
   fi 
   
   if test -w $listFile   # delete list file if needed
   then
     rm -f $listFile
   fi 
   exit $errorFlag
}

#
# Get destination path from user. Set default to user's home directory
#
get_destination_path ()
{
   if test $errorFlag = 0
   then
      echo "Default destination directory: $destinationPath/$hostProgName"
      echo "Press Return to accept the default directory."
      echo "Or enter new destination directory."

      read kbBuffer 
      if test $errorFlag = 0
      then
         if test "$kbBuffer" = ""
         then
            destinationPath=$destinationPath/$hostProgName
            echo "Using default directory: $destinationPath"
         else
            destinationPath=$kbBuffer
            echo "Using new directory: $destinationPath"
         fi
      fi 
   fi 
}

#
# Check destination directory. Prompt user if directory exists.
#
check_destination_path ()
{
   if test $errorFlag = 0
   then
      #check if directory already exists
      if test -d $destinationPath
      then 
        dirExistsFlag=1
        yesNoPrompt 
        if test $yesNo = 0
        then
           errorFlag=0
        else
           errorFlag=1
        fi
      fi
   fi
}


echo ""
echo "HW-130 Installation Program"
os_revision 
get_destination_path
check_destination_path
copy_files
display_end_message 
# End of script
