#!/bin/ksh
# ------------------------------------------------------------------------- #
#                                                                           #
#IBM SecureWay Firewall Version 4.1                                         #
#Licensed Materials - Property of IBM                                       #
#                                                                           #
#5697-F48 (C) Copyright IBM Corp. 1985, 1994, 1999                          #
#All Rights Reserved                                                        #
#                                                                           #
#US Government Users Restricted Rights - Use, duplication or                #
#disclosure restricted by GSA ADP Schedule Contract with IBM Corp.          #
#                                                                           #
# ------------------------------------------------------------------------- #


#**********************************************************
#This function will attempt to create a directory
#Return Code:
#   0 successful or already present
#   1 failure
#**********************************************************
create_dir() {
   DIR_TO_CREATE=$1
   rc=0;
   if [ ! -d $DIR_TO_CREATE ]
    then
      mkdir $DIR_TO_CREATE >/dev/null
      rc=$?
      [ $rc -ne 0 ] && echo "Unable to create the directory $DIR_TO_CREATE"
   fi

   return $rc
}

#**********************************************************
#This function print a certain amount of echos
#It takes one argument which is the number of echos to display on the screen
#**********************************************************
clear_or_echo() {  
   max_echos=$1
   echo_count=0
    while [ $echo_count -lt $max_echos ]
    do
       echo ""
       (( echo_count += 1 ))   
    done

}


#*****************************
#*  MAIN *
#*****************************
CDROM_DIRECTORY=/fwcdrom
TEMP_INSTALL_DIRECTORY=/usr/sys/inst.images/FW3.3.0_on_AIX4.3.3

if [ "`whoami`" != "root" ]
then
        echo "You must be root to run this program."
        echo "Suggestion: Type su - "
        exit 1
fi

if [ `pwd | grep -c $CDROM_DIRECTORY` -eq 1 ]
 then
        echo "This program can not be run from within the"
        echo "$CDROM_DIRECTORY directory"
        exit 1
fi



[ "$FALSE" = "" ] && FALSE=0
[ "$TRUE" = ""  ] && TRUE=1

errors=FALSE

#***********************************************
#Explain to the user what this script will do
#***********************************************
clear_or_echo 26
echo "This script will convert the .toc file which ships with IBM's"
echo "SecureWay FireWall version 3.3.0, so that it can be installed on an" 
echo "AIX Machine running Version 4.3.3."
echo "Insert the FW 3.3.0 Release CD into the cdrom player."
echo ""
echo "Press any Key to begin or Control+C to terminate"
echo ""

read input



#***********************************************
#Create the directory that will be used for the 
#CD-ROM mount point if it doesn't exist 
#***********************************************
create_dir $CDROM_DIRECTORY
[ $? -ne 0 ] && errors=TRUE

#***********************************************
#Create the directory that will be used temporarily
#store the files copied from the CD-ROM
#***********************************************
[ "$errors" = "FALSE" ] && {
  [ -d $TEMP_INSTALL_DIRECTORY ] && rm -rf $TEMP_INSTALL_DIRECTORY
  create_dir $TEMP_INSTALL_DIRECTORY
  [ $? -ne 0 ] && errors=TRUE
}


#***********************************************
#Check to see if the user already has 
#the /fwcdrom filesystem created.
#if not then create one.
#***********************************************
[ "$errors" = "FALSE" ] && {
  if [ `cat /etc/filesystems | grep -c "^\$CDROM_DIRECTORY:$"` -eq 0 ]
    then
      crfs -v cdrfs -p ro -d'cd0' -m${CDROM_DIRECTORY} -A''`locale nostr | awk -F: '{print $1}'`' '
      rc=$?
      [ $rc -ne 0 ] && echo "Unable to create the cdrom filesystem."
      [ $rc -ne 0 ] && errors=TRUE
  fi
}

#***********************************************
#Mount the CD-ROM filesystem
#if its not already mounted
#***********************************************
[ "$errors" = "FALSE" ] && {
   if [ `mount | grep -c "^.* $CDROM_DIRECTORY "` -eq 0 ]
    then
     #********************************************************
     #Make sure that another filesystem is not mounted to the
     #CD-ROM before we attempt our mount.
     #********************************************************
     CDRFSTOUNMOUNT=`mount | grep /dev/cd0 | awk '{print $2}'`

     if [ ! -z "$CDRFSTOUNMOUNT" ]
      then
         if [ "$CDRFSTOUNMOUNT" != "$CDROM_DIRECTORY" ]
          then
            #**********************************************
            #Another Filesystem is mounted to the cd-rom.
            #Attempt to unmount the filesystem so that the
            #Firewall's CD-ROM mount point can be used.
            #**********************************************
            unmount $CDRFSTOUNMOUNT
            if [ $? -ne 0 ]
               then
               echo "Error: The CD-ROM mount point $CDRFSTOUNMOUNT must be unmounted"
               echo "       Type unmount $CDRFSTOUNMOUNT and re-run this script."
               errors=TRUE
            else
               #**********************************************
               #* We were successful at unmounting  the
               #* user's cd mount point. Now set a flag to
               #* remember that we did so.
               #**********************************************
               UNMOUNTEDCDRFS="YES"
           fi
         fi
     fi

      #***********************************************
      #Mount the Firewall's cd-rom filesystem
      #***********************************************
      [ "$errors" = "FALSE" ] && {
        mount $CDROM_DIRECTORY >/dev/null 
        if [ $? -ne 0 ]; then
               clear_or_echo 3
               echo "Sorry No CDROM in Drive. "
               echo "Place the 3.3.0 Firewall Release CD into the CDROM drive"
               echo "Then Restart this Script to complete Installation " 
               errors=TRUE
         fi
     }
   fi
}

#***********************************************
#Determine if there is enough space to copy the files from the CD-ROM
#***********************************************
[ "$errors" = "FALSE" ] && {
   TOTAL_FW_IMAGE_SIZE=`du -s ${CDROM_DIRECTORY}/aix/usr/sys/inst.images | awk '{print $1}'`
   [ "$TOTAL_FW_IMAGE_SIZE" = "" ] && TOTAL_FW_IMAGE_SIZE=0

   FREESPACE_IN_USR=`df | grep "^.* /usr$" | awk '{print $3}'`
   [ "$FREESPACE_IN_USR" = "" ] && FREESPACE_IN_USR=0


   #Check to see if the filesystem is low on space.
   if [ $FREESPACE_IN_USR -lt $TOTAL_FW_IMAGE_SIZE ] 
     then
        additional_space=0
        (( additional_space = $TOTAL_FW_IMAGE_SIZE - $FREESPACE_IN_USR ))

        echo "There is not enough space in the /usr filesystem to copy"
        echo "the firewall installations files."
        echo "The /usr filesystem needs to be grown by an addtional $additional_space in 512 byte blocks"
        errors=TRUE
   fi

   #check to see if there was a problem determining the firewall installation
   #sizes
   if [ $TOTAL_FW_IMAGE_SIZE -eq 0 ]
      then
         echo "An error occured while attempting to access the Firewall CD-ROM"
         echo "to determine how much space the /usr filesystem will need".
         echo "\nPlease make sure the the CD-ROM in the drive is a Firewall CD-ROM".
         errors=TRUE
   fi
}

#***********************************************
#Copy the files
#***********************************************
[ "$errors" = "FALSE" ] && {
           echo "Please wait while files are transfered"
           cp ${CDROM_DIRECTORY}/aix/usr/sys/inst.images/* $TEMP_INSTALL_DIRECTORY
           [ $? -ne 0 ] && errors=TRUE
           cp ${CDROM_DIRECTORY}/aix/usr/sys/inst.images/.toc $TEMP_INSTALL_DIRECTORY
           [ $? -ne 0 ] && errors=TRUE
           [ "$errors" = "TRUE" ] && "Unable to copy Firewall Installation files"
                
}


#***********************************************
#Update the Table Of Contents file with the
#correct Java information
#***********************************************
OLDDIR=`pwd`
LOGFILE=/tmp/fw_install.log
[ -f $LOGFILE ] && rm -f $LOGFILE


[ "$errors" = "FALSE" ] && {
  
  cd $TEMP_INSTALL_DIRECTORY
  chmod 777 .toc
  chmod 744 *
  echo "Please wait .toc file is being modified"
  TARGET=.toc
  awk '{
       if ($2 ~ "Java.rte" ) {
              print "\*prereq Java.rte.bin 1.1.2.0"
              print "\*prereq Java.rte.classes 1.1.2.0"
    }
       else { print $0 }
       }' $TARGET > $TARGET.tmp && \
   mv -f $TARGET.tmp $TARGET

   #***************************************************
   #modify the oslevel script so that install will pass
   #***************************************************
   GREATESTOSLEVEL=`oslevel -q 2>/dev/null | sort -u | tail -1`
   OSLEVEL=`echo $GREATESTOSLEVEL | sed 's/\.//g'`
   [ -z $OSLEVEL ] && OSLEVEL=0

   if [ $OSLEVEL -ge 4310 ]
    then
      oscommand=`which oslevel`
      if [ ! -z $oscommand ]
       then
         if [ ! -f $oscommand.bak ]
           then
           cp $oscommand $oscommand.bak
           if [ $? -eq 0 ]
             then
              echo "echo $GREATESTOSLEVEL" > $oscommand
              chmod +x $oscommand
           fi
         fi
      fi
   fi
 

   clear_or_echo 8

   echo  "Now Installing FireWall 3.3 for AIX version 4.3.3 Please Wait "
   /usr/lib/instl/sm_inst installp_cmd -e $LOGFILE -a -Q -d $TEMP_INSTALL_DIRECTORY -f 'FW'  '-c' '-N' '-g' '-X' 
   rc=$?
   [ $rc -ne 0 ] && echo "\n####################################################"
   [ $rc -ne 0 ] && echo "An error occured while attempting to install the Firewall."
   [ $rc -ne 0 ] && echo "View the file $LOGFILE for more details."
   [ $rc -ne 0 ] && echo "####################################################"
   [ $rc -ne 0 ] && errors=TRUE
}

cd $OLDDIR


#*************************************
#Now....attempt to clean up everything we did
#to the user's system
#*************************************
clear_or_echo 8

#*************************************
#put back the original oslevel command
#if we modified it.
#*************************************
if [ ! -z "$oscommand" ]
 then
    rm -f $oscommand && mv $oscommand.bak $oscommand 
fi

#*************************************
#unmount the CD-ROM filesytem
#*************************************
[ `mount | grep -c "^.* $CDROM_DIRECTORY "` -gt 0 ] && unmount $CDROM_DIRECTORY

#*************************************
#remove the CD-ROM filesystem that we created
#*************************************
[ `cat /etc/filesystems | grep -c "^\$CDROM_DIRECTORY:$"` -eq 0 ] && rmfs    $CDROM_DIRECTORY
[ -d $CDROM_DIRECTORY ] && rm -rf $CDROM_DIRECTORY


#*************************************
#Put back user's CD-ROM mount point if 
#we had to unmount it.
#*************************************
[ ! -z "$UNMOUNTEDCDRFS" ] && mount $CDRFSTOUNMOUNT


#*************************************
#remove the temporary directory that held the firewall files
#*************************************
[ -d $TEMP_INSTALL_DIRECTORY ] && chmod -R 744 $TEMP_INSTALL_DIRECTORY
[ -d $TEMP_INSTALL_DIRECTORY ] && rm -rf $TEMP_INSTALL_DIRECTORY


if [ "$errors" = "FALSE" ]   
 then
   echo  "Installation Complete Please restart your Machine "
   exitcode=0
else
   exitcode=1
fi


exit $exitcode



