#!/bin/sh
###############################################################################
##  install.log  ##  This shell script will install APPX from CD-Rom         ##
###############################################################################

##
##  APPX_SAVE( "relative path/filename" )
##

APPX_SAVE()
{
  if [ -r "$putpath/$1" ] 
  then
    dosave="y"

    if [ -r "$putpath/$1.$savemask" ]
    then
      echo ""
      echo "** Saving a copy of $1 to $1.$savemask"
      echo "** but $1.$savemask already exists."
      echo "**"
      echo "** Replace $1.$savemask?"
      echo "** ( enter a 'y' or 'n', Default=n )"
      read a
      if [ ! "$a" = "" ]
      then
        dosave="n"
      fi

      if [ "$dosave" = "y" ]
      then
        echo "rm $1.$savemask"
        if [ "$confirm" = "y" ]
        then
          echo >>install.log
          echo "rm $1.$savemask" >>install.log
          rm $1.$savemask 2>&1 |tee -a install.log
        fi
      fi

    fi

    if [ "$dosave" = "y" ] 
    then
      echo "cp $1 $1.$savemask"
      if [ "$confirm" = "y" ]
      then
        echo >>install.log
        echo "cp $1 $1.$savemask" >>install.log
        cp $1 $1.$savemask 2>&1 |tee -a install.log
      else
        echo >>install.log
        echo "** autosave of $1 bypassed by user" >>install.log
      fi
    fi

  fi
}

##
##  APPX_VALIDATE( "Description" "Filename" )
##

APPX_VALIDATE()
{
  fnam="null"
  
  if [ "$fscase" = "upper" ]
  then
    fnam=`echo $2|tr "[a-z]" "[A-Z]"`
  fi
  if [ "$fscase" = "lower" ]
  then
    fnam=`echo $2|tr "[A-Z]" "[a-z]"`
  fi
  if [ "$fscase" = "version" ]
  then
    fnam=`echo $2|tr "[a-z]" "[A-Z]"`\;1
  fi
  if [ "$fscase" = "mixed" ]
  then
    fnam=$2
  fi
  if [ "$fnam" = "null" ]
  then
    fnam=$1
  fi

  if [ -r "$cdpath/$fnam" ]
  then
    if [ -s "$cdpath/$fnam" ]
    then
      echo "$1 ok        $cdpath/$fnam"
    else
      echo "$1 ZERO SIZE $cdpath/$fnam"
    fi
  else
    echo   "$1 NOT FOUND $cdpath/$fnam"
  fi
}

##
##  APPX_INSTALL( "Tarfilename.tgz" )
##

APPX_INSTALL()
{
  fnam="null"
  
  if [ "$fscase" = "upper" ]
  then
    fnam=`echo $1|tr "[a-z]" "[A-Z]"`
  fi
  if [ "$fscase" = "lower" ]
  then
    fnam=`echo $1|tr "[A-Z]" "[a-z]"`
  fi
  if [ "$fscase" = "version" ]
  then
    fnam=`echo $1|tr "[a-z]" "[A-Z]"`\;1
  fi
  if [ "$fscase" = "mixed" ]
  then
    fnam=$1
  fi
  if [ "$fnam" = "null" ]
  then
    fnam=$1
  fi

  if [ -s temp.tar ]
  then
    rm -f temp.tar
  fi
  
  echo "gunzip <$cdpath/$fnam >temp.tar"
  echo "tar -xf temp.tar ; rm -f temp.tar"
  if [ "$confirm" = "y" ]
  then
    echo >>install.log
    echo "gunzip <$cdpath/$fnam >temp.tar" >>install.log 2>&1
    echo "tar -xf temp.tar" >>install.log 2>&1
    echo "rm -f temp.tar" >>install.log 2>&1
    gunzip <"$cdpath/$fnam" >temp.tar
    tar -xf temp.tar 2>&1 |tee -a install.log
    rm -f temp.tar
  fi
}

##
##  APPX_COPY( "filename" "destination" )
##

APPX_COPY()
{
  fnam="null"
  
  if [ "$fscase" = "upper" ]
  then
    fnam=`echo $1|tr "[a-z]" "[A-Z]"`
  fi
  if [ "$fscase" = "lower" ]
  then
    fnam=`echo $1|tr "[A-Z]" "[a-z]"`
  fi
  if [ "$fscase" = "version" ]
  then
    fnam=`echo $1|tr "[a-z]" "[A-Z]"`\;1
  fi
  if [ "$fscase" = "mixed" ]
  then
    fnam=$1
  fi
  if [ "$fnam" = "null" ]
  then
    fnam=$1
  fi

  echo "cp $cdpath/$fnam $2"

  if [ "$confirm" = "y" ]
  then
    echo >>install.log
    echo "cp $cdpath/$fnam $2" >>install.log
    cp "$cdpath/$fnam" $2 2>&1|tee -a install.log
  fi
}

##
##  Initialize variables
##

savemask=34save
fscase=none
ostype=none
osname=none
osnam=none
byteorder=ris
putpath=none

needappx=y
needlink=n
needvis=n

cdpath=$PWD

gunzip="`type gunzip|cut -d\  -f3`"
if [ -x "$gunzip" ]
then
  zipfound="y"
else
  zipfound="n"
fi
 
##
##  Present instructions
##

clear
echo "This script will allow you to install various components of the APPX"
echo "software on your unix system.  You should really be the 'root' user"
echo "for this script to run properly."
echo
  
##
##  Check to make sure we are ROOT
##

echo "(checking user id)"
id |cut -f1 -d\  |grep root
if [ $? -eq 1 ]
then
	echo "*** You really need to be 'root' to run this install! ***"
fi
echo
##
##  Check if the CD filesystem is mounted as UPPER or LOWER case
##

if [ -r "install.sh" ] 
then
	fscase=lower
fi
if [ -r "INSTALL.SH" ] 
then
	fscase=upper
fi
if [ -r "INSTALL.SH;1" ] 
then
	fscase=version
fi
if [ -r "Install.sh" ] 
then
	fscase=mixed
fi
if [ "$fscase" = "none" ] 
then
	echo "*** Install can't determine file system case! ***"
	exit 1
fi

##
##  Pick which platform to install
##

echo "What Platform will you be installing?"
echo "====================================="
echo "1 - AIX"
echo "2 - DGUX-Intel"
echo "3 - HPUX"
echo "4 - Linux"
echo "5 - NCR"
echo "6 - SCO"
echo "7 - Solaris"
echo
echo "Select a platform by number (1-7):"
read ostype

case "$ostype" in
  1*) osname="AIX"
      osnam=aix
      byteorder=ris
      ;;
  2*) osname="DG-UX Intel"
      osnam=dgi
      byteorder=int
      ;;
  3*) osname="HP-UX"
      osnam=hpx
      byteorder=ris
      ;;
  4*) osname="Linux"
      osnam=lin
      byteorder=int
      ;;
  5*) osname="NCR"
      osnam=ncr
      byteorder=int
      ;;
  6*) osname="SCO"
      osnam=sco
      byteorder=int
      ;;
  7*) osname="Solaris"
      osnam=sol
      byteorder=ris
      ;;
esac
if [ "$osname" = "none" ]
then
  echo "*** Invalid platform selected! ***"
  exit 1
fi

##
##  Select the install path
##

echo
echo "Enter the path that you would like to install into"
echo "=================================================="
echo "( example: /usr/local/appx )"
read putpath
if [ ! -d "$putpath" ]
then
	echo "*** Sorry, this is not a valid path! ***"
	exit 1
fi

##
##  Will you be installing APPX?
##

echo
echo "This script will allow you to install the main APPX release on your"
echo "system as well as other optional packages.  You don't have to"
echo "re-install the main APPX environment if you already have this release"
echo "installed and just want to install some of the optional packages."
echo
echo "Will you be installing/upgrading the main APPX environment?"
echo "==========================================================="
echo "( enter a 'y' or 'n', Default=$needappx )"
read a
if [ ! "$a" = "" ]
then
  needappx=$a
fi


if [ "$needappx" = "y" ]
then
  needappx=yes
else
  needappx=no
fi

##
##  Will we be installing Linkable files?
##

echo
echo "If you will be using special external databases or writing you own 'C'"
echo "functions, you may need to re-link the appx executable on your system."
echo "Most installations can just use the appx or the appx.oracle"
echo "executable that we ship with the product."
echo
echo "Will you need to custom link your own APPX executable?"
echo "====================================================="
echo "( enter a 'y' or 'n', Default=$needlink )"
read a
if [ ! "$a" = "" ]
then
  needlink=$a
fi

if [ "$needlink" = "y" ]
then
  needlink=yes
else
  needlink=no
fi

##
##  Summarize and confirm
##

echo "########################################################################"
echo "##               APPX INSTALL SUMMARY AND CONFIRMATION                ##"
echo "########################################################################"
echo
echo "Installing APPX files for: $osname"
echo "                from path: $PWD"
echo "                into path: $putpath"
echo "                  as user: `id|cut -f1 -d\ `"
echo
echo "          main APPX files: $needappx"
echo "                 linkable: $needlink"
echo
echo "Description               Status    File Required For Installation"
echo "------------------------- --------- ------------------------------------"

if [ "$zipfound" = "n" ]
then
  APPX_VALIDATE "GNU Unzip Utility        " "Gunzip.$osnam"  
fi
if [ "$needappx" = "yes" ]
then
  APPX_VALIDATE "Common Files and Scripts " "Comm_unx.tgz"
  APPX_VALIDATE "Binary Executable Files  " "Exec_$osnam.tgz"
  APPX_VALIDATE "Appx Runtime Files       " "Data_$byteorder.tgz"
fi
if [ "$needlink" = "yes" ]
then
  APPX_VALIDATE "Linkable Engine Files    " "Link_$osnam.tgz"
fi
echo
echo "Proceed with install (Yes/No/Show commands only)? (y or n or s):"
read confirm
echo

if [ ! "$confirm" = "y" ] && [ ! "$confirm" = "s" ]  
then
  exit 1
fi

##
##  Capture the current path and change to the install path
##

echo "cd $putpath"
if [ "$confirm" = "y" ]
then
  cd $putpath
  rm install.log >/dev/null 2>&1
  date >>install.log
  echo "########################################################################" >>install.log
  echo "##               APPX INSTALL SUMMARY AND CONFIRMATION                ##" >>install.log
  echo "########################################################################" >>install.log
  echo >>install.log
  echo "Installing APPX files for: $osname" >>install.log
  echo "                from path: $cdpath" >>install.log
  echo "                into path: $putpath" >>install.log
  echo "                  as user: `id`" >>install.log
  echo >>install.log
  echo "          main APPX files: $needappx" >>install.log
  echo "                 linkable: $needlink" >>install.log
  echo >>install.log
fi

##
##  Check for GNUzip and install a copy if needed
##

if [ "$zipfound" = "n" ]
then
  APPX_COPY "Gunzip.$osnam" "/tmp/gunzip"

  echo "chmod 777 /tmp/gunzip" 
  echo "PATH=/tmp:\$PATH"

  if [ "$confirm" = "y" ]
  then
    echo >>install.log
    echo "chmod 777 /tmp/gunzip" >>install.log 2>&1
    echo "PATH=/tmp:\$PATH" >>install.log 2>&1
    chmod 777 /tmp/gunzip
    PATH=/tmp:$PATH
  fi
fi

##
##  Install common unix files
##

if [ "$needappx" = "yes" ]
then
  APPX_SAVE "data/appx_print"
  APPX_SAVE "data/appx_submit"
  APPX_SAVE "data/aixterm.gdef"
  APPX_SAVE "data/ansi.gdef"
  APPX_SAVE "data/ibm3151.gdef"
  APPX_SAVE "data/vt100.gdef"
  APPX_SAVE "data/vt320.gdef"
  APPX_SAVE "data/vt320-k.gdef"
  APPX_SAVE "data/appx.env"
  APPX_SAVE "tools/start-appxd.sh"
  APPX_INSTALL "Comm_unx.tgz"
fi

##
##  Install the executables
##

if [ "$needappx" = "yes" ]
then
  APPX_INSTALL "Exec_$osnam.tgz"
fi

##
##  Install Data files
##

if [ "$needappx" = "yes" ]
then
  APPX_INSTALL "Data_$byteorder.tgz"
fi

##
##  Install Linkable files
##

if [ "$needlink" = "yes" ]
then
  APPX_INSTALL "Link_$osnam.tgz"
fi

##
##  Fix ownerships
##

if [ "$needappx" = "yes" ]
then
  echo "rm -rf data/00/0*/Em data/00/0*/Dbg"
  echo "chown -R appx * ; chmod 777 appx* ; chmod u+s appx*"

  if [ "$confirm" = "y" ]
  then
    echo >>install.log
    echo "rm -rf data/00/0*/Em data/00/0*/Dbg" >>install.log
    rm -rf data/00/0*/Em data/00/0*/Dbg 2>&1 |tee -a install.log

    echo >>install.log
    echo "chown -R appx *" >>install.log
    chown -R appx * 2>&1 |tee -a install.log

    echo >>install.log
    echo "chmod a=rx appx*" >>install.log
    chmod a=rx appx* 2>&1 |tee -a install.log 

    echo >>install.log
    echo "chmod u+s appx*" >>install.log
    chmod u+s appx* 2>&1 |tee -a install.log

  fi
fi

if [ "$confirm" = "y" ]
then
  echo
  echo "A log file $putpath/install.log of this install session has been created."
fi

echo

#################################################################################
##  Modification History:
##
##  DATE      BY    Description
##  --------  ---   -------------------------------------------------------------
##  06/07/99  KAD   Checking in latest revision, changing "tar -xvf" to "tar -xf"
**  11/09/00  KAD   Fixed chmod commands, a few typos.
##
#################################################################################
