#!/bin/sh
#
# native_install
# Release 4.5
#
# This script is used to install native C and/or FORTRAN compilers
# This install assumes that the related compiler tar files are on
# a remote system. It prompts the user to get the correct information.

DEFAULTS_FILE=/etc/defaults/install
DIST_ADDRESS=0.0.0.0
DIST_NODENAME=unknown
DIST_PATH=/distribution
NATC_TAR=nat_c.tar
NATF_TAR=nat_ftn.tar
NATC_DOC=icc.doc.tar
NATF_DOC=if77.doc.tar
INVALID="Invalid input"
ROOT_DIR="/"

###########################################################################
# FUNCTION:     GetDist
#
#       This function retrieves the ftp info from the last install
###########################################################################
GetDist()
{
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
fi
if [ -z "$DIST_NODENAME" ]; then
        DIST_NODENAME=unknown
fi
if [ -z "$DIST_PATH" ]; then
        DIST_PATH=/distribution
fi
}
###########################################################################
# FUNCTION:     GetParam
#
#       This function checks for arguments and sets the appropriate flags.
###########################################################################
GetParam()
{
if [ $# = 0 ]; then
   INSTALL=3
   INSTALL_F77="y"
   INSTALL_C="y"
elif [ $# = 1 -a \( $1 = "C" -o $1 = "c" \) ]; then
   INSTALL=1
   INSTALL_C="y"
   INSTALL_F77="n"
elif [ $# = 1 -a \( $1 = "F77" -o $1 = "f77" \) ]; then
   INSTALL=2
   INSTALL_C="n"
   INSTALL_F77="y"
else
   echo "usage:  native_install C   - install native C compiler"
   echo "        native_install F77 - install native FORTRAN compiler"
   echo "        native_install     - install native C and FORTRAN compilers"
   exit 1
fi
}
   
###########################################################################
# FUNCTION:     GetTarFiles
#
#       This function will go to the distribution system and retrieve the
#       tar files. If a failure happens, the user will be given the chance
#       to change the information.
###########################################################################
GetTarFiles()
{
        GET_COMPRESSED=1
        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
		if [ \( "$INSTALL_C" = "Y" -o "$INSTALL_C" = "y" \) -a \( "$INSTALL_F77" = "Y" -o "$INSTALL_F77" = "y" \) ]; then
                   ftp -n -v $DIST_ADDRESS <<EOF1
                        user $FTP_USER
                        binary
                        lcd /home
                        cd $DIST_PATH
                        get $NATC_TAR.Z
                        get $NATF_TAR.Z
                        get $NATC_DOC.Z
                        get $NATF_DOC.Z
                        quit
EOF1
		fi

		if [ \( "$INSTALL_C" = "Y" -o "$INSTALL_C" = "y" \) -a \( "$INSTALL_F77" = "N" -o "$INSTALL_F77" = "n" \) ]; then
                   ftp -n -v $DIST_ADDRESS <<EOF2
                        user $FTP_USER
                        binary
                        lcd /home
                        cd $DIST_PATH
                        get $NATC_TAR.Z
                        get $NATC_DOC.Z
                        quit
EOF2
                fi

		if [ \( "$INSTALL_C" = "N" -o "$INSTALL_C" = "n" \) -a \( "$INSTALL_F77" = "Y" -o "$INSTALL_F77" = "y" \) ]; then
                   ftp -n -v $DIST_ADDRESS <<EOF3
                        user $FTP_USER
                        binary
                        lcd /home
                        cd $DIST_PATH
                        get $NATF_TAR.Z
                        get $NATF_DOC.Z
                        quit
EOF3
                fi

# Check to make sure each of the files has been copied down to the target
# machine. If these files are not here, give the user a change to repeat.
                Fail=0
                if [ "$INSTALL_C" = "Y" -o "$INSTALL_C" = "y"  ]; then
                   if [ ! -f /home/$NATC_TAR.Z  ]; then
                           echo "ERROR: $NATC_TAR.Z was not retrieved."
                           Fail=1
                   fi
                   if [ ! -f /home/$NATC_DOC.Z  ]; then
                           echo "ERROR: $NATC_DOC.Z was not retrieved."
                           Fail=1
                   fi
                fi

                if [ "$INSTALL_F77" = "Y" -o "$INSTALL_F77" = "y"  ]; then
                   if [ ! -f /home/$NATF_TAR.Z  ]; then
                           echo "ERROR: $NATF_TAR.Z was not retrieved."
                           Fail=1
                   fi
                   if [ ! -f /home/$NATF_DOC.Z  ]; then
                           echo "ERROR: $NATF_DOC.Z was not retrieved."
                           Fail=1
                   fi
                fi

                if [ $Fail -eq 0 ]; then

# Uncompress the files we just retrieved.
                   if [ $GET_COMPRESSED -eq 1 ]; then
                           cd /home
                           Fail=0
                           if [ "$INSTALL_C" = "Y" -o "$INSTALL_C" = "y"  ]; then
                               	echo "Uncompressing $NATC_TAR.Z..."
                               	uncompress $NATC_TAR.Z
                               	if [ $? -ne 0 ]; then
                                 	Fail=1
                               	fi
                                if [ $Fail -eq 0 ]; then
                                   echo "Uncompressing $NATC_DOC.Z..."
                               	   uncompress $NATC_DOC.Z
                               	   if [ $? -ne 0 ]; then
                                 	Fail=1
                                   fi
                               	fi
                           fi
		 	   if [ $Fail -eq 0 -a \( "$INSTALL_F77" = "Y" -o "$INSTALL_F77" = "y" \)  ]; then
				cd /home
                                echo "Uncompressing $NATF_TAR.Z..."
                                uncompress $NATF_TAR.Z
                               	if [ $? -ne 0 ]; then
                                 	Fail=1
                               	fi
                                if [ $Fail -eq 0 ]; then
                                   echo "Uncompressing $NATF_DOC.Z..."
                               	   uncompress $NATF_DOC.Z
                               	   if [ $? -ne 0 ]; then
                                 	Fail=1
                                   fi
                               	fi
                            fi
                            if [ $Fail -eq 1 ]; then
                                   echo "ERROR: At least one of the tar files was corrupt!"
                            fi
                    fi
                fi
                if [ $Fail -eq 1 ]; then
                        echo "\n"
                        echo "There were errors retrieving or uncompressing the files."
                        echo "Continue  (y/n) ?\c"
                        read answer
                        echo " "
                        if [ "$answer" = "Y" -o "$answer" = "y" ];  then
                           AskQuestions
                        else
                           exit 1
                        fi

                else
                        Done=1
                fi
        done
}
###########################################################################
# FUNCTION:     AskQuestions
#
#       This function retrieves the installation info
###########################################################################
AskQuestions()   
{
#
accept=0
while [ $accept = 0 ] 
do
   clear
   echo
   echo "======================================================================="
   echo
   echo "                  Native Compiler Installation"
   echo
   echo "======================================================================="
   echo 
   if [ $INSTALL = 2 -o $INSTALL = 3 ]; then
      echo "Install FORTRAN? [y/n]:                           $INSTALL_F77"
   fi
   if [ $INSTALL = 1 -o $INSTALL = 3 ]; then
      echo "Install C? [y/n]:                                 $INSTALL_C"
   fi
   echo "Root directory for compiler installation [path]:  $ROOT_DIR"
   echo "Distribution Node:                                $DIST_NODENAME"
   echo "Distribution IP Addr:                             $DIST_ADDRESS"
   echo "Distribution Path:                                $DIST_PATH"
   echo "Is this correct? (y/n): \c"
   read answer
   echo " "
   if [ "$answer" != "Y" -a "$answer" != "y" ];  then

      if [ $INSTALL = 2 -o $INSTALL = 3 ]; then
      f77_ok=0
      while [ $f77_ok = 0 ]
      do
         echo  "Install FORTRAN? [$INSTALL_F77]: \c"
         read answer
         test $answer
         if [ $? = 1 ]; then
            f77_ok=1
         elif [ "$answer" != "N" -a "$answer" != "n" -a "$answer" != "Y" -a "$answer" != "y" ]; then
            echo $INVALID
         else
            f77_ok=1
            INSTALL_F77=$answer
         fi
      done
      fi
 
      if [ $INSTALL = 1  -o $INSTALL = 3 ]; then
      cc_ok=0
      while [ $cc_ok = 0 ]
      do 
         echo  "Install C? [$INSTALL_C]: \c"
         read answer
         test $answer
         if [ $? = 1 ]; then
            cc_ok=1
         elif [ "$answer" != "N" -a "$answer" != "n" -a "$answer" != "Y" -a "$answer" != "y" ]; then
            echo $INVALID
         else
            cc_ok=1
            INSTALL_C=$answer
         fi
      done
      fi

      root_ok=0
      while [ $root_ok = 0 ]
      do  
         echo  "Enter root directory for compiler installation [$ROOT_DIR]: \c"
         read answer
         test $answer
         if [ $? = 1 ]; then
            root_ok=1 
         elif  [ ! -d "$answer" ]; then
            echo "$answer is not a directory or does not exist"
            if [ ! -f "$answer" ]; then
               echo "Create $answer [n]? \c"
               read answer2
               test $answer2
               if [ $? = 0 ]; then
                  if [ $answer2 = "y" -o $answer2 = "Y" ]; then
                     mkdir $answer
                     if [ $? -ne 0 ]; then
                        echo "Unable to create $answer - error $?"
                     else
                        ROOT_DIR=$answer
                        chown root $answer
                        chgrp system $answer
                        chmod 755 $answer
                        root_ok=1
                     fi
                  fi
               fi
            fi
         else 
            ROOT_DIR=$answer
            root_ok=1
         fi
      done

      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
 

   else
      accept=1
   fi
done
}
###########################################################################
# FUNCTION:     DoInstall
#
#       This function installs the compilers
###########################################################################
DoInstall() 
{
not_installed=0
cd $ROOT_DIR
if [ $INSTALL_C = "Y" -o $INSTALL_C = "y" ]; then
   if [ -f /home/$NATC_TAR ]; then
      echo "Installing Native C compiler..."
      tar xpf /home/$NATC_TAR
      if [ $? -ne 0 ]; then
         not_installed=1
      fi
      if [ $not_installed -eq 0 ]; then
         rm -f /home/$NATC_TAR
         if [ ! -f $ROOT_DIR/usr/ccs/lib/iclib.a ]; then
            cd $ROOT_DIR/usr/ccs/lib
            ln -s libic.a iclib.a
            cd $ROOT_DIR/usr/lib
            ln -s ../ccs/lib/libic.a iclib.a
            cd $ROOT_DIR
         fi
         if [ $ROOT_DIR != "/" ]; then
            echo "Creating links for header files and libraries..."
            $ROOT_DIR/mklinks_c
         fi
         rm -f $ROOT_DIR/mklinks_c
         echo "Native C compiler has been installed"
      fi
   else
      not_installed=1
      echo "/home/nat_c.tar does not exist"
      echo "Native C compiler was not installed"
      echo " "
   fi
   if [ -f /home/$NATC_DOC ]; then
      echo "Installing C manual pages..."
      if [ $ROOT_DIR != "/" ]; then
         if [ !  -d $ROOT_DIR/usr/share ]; then
            mkdir $ROOT_DIR/usr/share
            chown root $ROOT_DIR/usr/share
            chgrp system $ROOT_DIR/usr/share
            chmod 755 $ROOT_DIR/usr/share
            cd $ROOT_DIR/usr
            ln -s  share/man man
         fi
      fi
      cd $ROOT_DIR/usr/share
      tar xpf /home/$NATC_DOC
      if [ $? -ne 0 ]; then
         not_installed=1
      else
         rm -f /home/$NATC_DOC
      fi
      cd $ROOT_DIR
   else
      not_installed=1
      echo "/home/$NATC_DOC does not exist"
      echo "C manual pages were not installed"
      echo " "
   fi
fi
if [ $INSTALL_F77 = "Y" -o $INSTALL_F77 = "y" ]; then
   if [ -f /home/$NATF_TAR ]; then
      echo "Installing Native FORTRAN compiler..."
      tar xpf /home/$NATF_TAR
      if [ $? -ne 0 ]; then
         not_installed=1
      fi
      if [ $not_installed -eq 0 ]; then
         rm -f /home/$NATF_TAR
         if [ ! -f $ROOT_DIR/usr/ccs/lib/iclib.a ]; then
            cd $ROOT_DIR/usr/ccs/lib
            ln -s libic.a iclib.a
            cd $ROOT_DIR/usr/lib
            ln -s ../ccs/lib/libic.a iclib.a
            cd $ROOT_DIR
         fi
         if [ $ROOT_DIR != "/" ]; then
            echo "Creating links for header files and libraries..."
            $ROOT_DIR/mklinks_f
         fi
         rm -f $ROOT_DIR/mklinks_f
      fi
   else
      not_installed=1
      echo "/home/nat_ftn.tar does not exist"
      echo "Native FORTRAN compiler was not installed"
      echo " "
   fi
   if [ -f /home/$NATF_DOC ]; then
      echo "Installing FORTRAN manual pages..."
      if [ $ROOT_DIR != "/" ]; then
         if [ !  -d $ROOT_DIR/usr/share ]; then
            mkdir $ROOT_DIR/usr/share
            chown root $ROOT_DIR/usr/share
            chgrp system $ROOT_DIR/usr/share
            chmod 755 $ROOT_DIR/usr/share
            cd $ROOT_DIR/usr
            ln -s  share/man man
         fi
      fi
      cd $ROOT_DIR/usr/share
      tar xpf /home/$NATF_DOC
      if [ $? -ne 0 ]; then
         not_installed=1
      else
         rm /home/$NATF_DOC
      fi
      cd $ROOT_DIR
   else
      not_installed=1
      echo "/home/$NATF_DOC does not exist"
      echo "FORTRAN manual pages were not installed"
      echo " "
   fi
fi
echo " "
if [ $not_installed = 0 ]; then
   echo "Installation complete"
else
   echo "Installation was unsuccessful due to missing files in /home"
fi
}
#
###########################################################################
#                      S T A R T  O F  I N S T A L L A T I O N
###########################################################################
GetParam $@
GetDist
AskQuestions
if [ "$INSTALL_C" = "Y" -o "$INSTALL_C" = "y" -o "$INSTALL_F77" = "Y" -o "$INSTALL_F77" = "y" ]; then
   GetTarFiles
   DoInstall
else
   echo "No installation required"
fi
