#!/bin/sh
#
# native_install
# Release 5.0
#
# 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
TMP_STORAGE=/tmp
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:     DoExit 
#
#       This function restores the original umask and exits 
###########################################################################
DoExit()
{
umask $OLD_UMASK
exit  $#
}

###########################################################################
# FUNCTION:     GetDist
#
#       This function retrieves the ftp info from the last install
###########################################################################
GetDist()
{
if [ -f $DEFAULTS_FILE ]; then
        DIST_NODENAME=`grep DIST_NODENAME $DEFAULTS_FILE | cut -f2 -d' '`
        DIST_PATH=`grep DIST_PATH $DEFAULTS_FILE | cut -f2 -d' '`
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 [ $# -eq 0 ]; then
   INSTALL=3
   INSTALL_F77="y"
   INSTALL_C="y"
elif [ $# -eq 1 -a \( $1 = "C" -o $1 = "c" \) ]; then
   INSTALL=1
   INSTALL_C="y"
   INSTALL_F77="n"
elif [ $# -eq 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"
   DoExit 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()
{
        Done=0
        while [ $Done -eq 0 ]; do
                CGET=""
                CDGET=""
                FGET=""
                FDGET=""
                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" ]; then
                   CGET="get $NATC_TAR.Z"
                   CDGET="get $NATC_DOC.Z"
                fi
		if [  "$INSTALL_F77" = "Y" -o "$INSTALL_F77" = "y" ]; then
                   FGET="get $NATF_TAR.Z"
                   FDGET="get $NATF_DOC.Z"
                fi
 
                ftp -n -v $DIST_NODENAME <<EOF1
                     user $FTP_USER
                     binary
                     lcd $TMP_STORAGE
                     cd $DIST_PATH
                     $CGET
                     $CDGET
                     $FGET
                     $FDGET
                     quit
EOF1

# 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 $TMP_STORAGE/$NATC_TAR.Z  ]; then
                           echo "ERROR: $NATC_TAR.Z was not retrieved."
                           Fail=1
                   fi
                   if [ ! -f $TMP_STORAGE/$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 $TMP_STORAGE/$NATF_TAR.Z  ]; then
                           echo "ERROR: $NATF_TAR.Z was not retrieved."
                           Fail=1
                   fi
                   if [ ! -f $TMP_STORAGE/$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.

                   cd $TMP_STORAGE
                   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 $TMP_STORAGE
                        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
                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
                           DoExit 1
                        fi

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

   if [ "$answer" != "Y" -a "$answer" != "y" ];  then

      if [ $INSTALL -eq 3 ]; then
         f77_ok=0
         while [ $f77_ok -eq 0 ]
         do
            echo  "Install FORTRAN? [$INSTALL_F77]: \c"
            read answer
            test $answer
            if [ $? -eq 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 -eq 3 ]; then
         cc_ok=0
         while [ $cc_ok -eq 0 ]
         do 
            echo  "Install C? [$INSTALL_C]: \c"
            read answer
            test $answer
            if [ $? -eq 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 -eq 0 ]
      do  
         echo  "Enter root directory for compiler installation [$ROOT_DIR]: \c"
         read answer
         test $answer
         if [ $? -eq 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 [ $? -eq 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
            if [ $ROOT_DIR != "/" ]; then
               chown root $answer
               chgrp system $answer
               chmod 755 $answer
            fi
            root_ok=1
         fi
      done

      tmp_ok=0
      while [ $tmp_ok = 0 ]
      do  
         echo  "Enter temporary storage directory on Paragon [$TMP_STORAGE]: \c"
         read answer
         test $answer
         if [ $? -eq 1 ]; then
            tmp_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 [ $? -eq 0 ]; then
                  if [ $answer2 = "y" -o $answer2 = "Y" ]; then
                     mkdir $answer
                     if [ $? -ne 0 ]; then
                        echo "Unable to create $answer - error $?"
                     else
                        TMP_STORAGE=$answer
                        tmp_ok=1
                     fi
                  fi
               fi
            fi
         else 
            TMP_STORAGE=$answer
            tmp_ok=1
         fi
      done
      echo "Distribution Node Name [$DIST_NODENAME]: \c"; read ans
      if [ ! -z "$ans" ]; then
         DIST_NODENAME=$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 $TMP_STORAGE/$NATC_TAR ]; then
      echo "Installing Native C compiler..."
      tar xpf $TMP_STORAGE/$NATC_TAR
      if [ $? -ne 0 ]; then
         not_installed=1
      fi
      if [ $not_installed -eq 0 ]; then
         rm -f $TMP_STORAGE/$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 "$TMP_STORAGE/nat_c.tar does not exist"
      echo "Native C compiler was not installed"
      echo " "
   fi
   if [ -f $TMP_STORAGE/$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 $TMP_STORAGE/$NATC_DOC
      if [ $? -ne 0 ]; then
         not_installed=1
      else
         rm -f $TMP_STORAGE/$NATC_DOC
      fi
      cd $ROOT_DIR
   else
      not_installed=1
      echo "$TMP_STORAGE/$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 $TMP_STORAGE/$NATF_TAR ]; then
      echo "Installing Native FORTRAN compiler..."
      tar xpf $TMP_STORAGE/$NATF_TAR
      if [ $? -ne 0 ]; then
         not_installed=1
      fi
      if [ $not_installed -eq 0 ]; then
         rm -f $TMP_STORAGE/$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
         echo "Native FORTRAN compiler has been installed"
         rm -f $ROOT_DIR/mklinks_f
      fi
   else
      not_installed=1
      echo "$TMP_STORAGE/nat_ftn.tar does not exist"
      echo "Native FORTRAN compiler was not installed"
      echo " "
   fi
   if [ -f $TMP_STORAGE/$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 $TMP_STORAGE/$NATF_DOC
      if [ $? -ne 0 ]; then
         not_installed=1
      else
         rm $TMP_STORAGE/$NATF_DOC
      fi
      cd $ROOT_DIR
   else
      not_installed=1
      echo "$TMP_STORAGE/$NATF_DOC does not exist"
      echo "FORTRAN manual pages were not installed"
      echo " "
   fi
fi
echo " "
if [ $not_installed -eq 0 ]; then
   echo "Installation complete"
else
   echo "Installation was unsuccessful due to missing files in $TMP_STORAGE"
fi
}
#
###########################################################################
#                      S T A R T  O F  I N S T A L L A T I O N
###########################################################################
OLD_UMASK=`umask`
umask 022
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
DoExit 0
