#!/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

  
  echo "gunzip -c $cdpath/$fnam | tar -xf -"

  if [ "$confirm" = "y" ]
  then
    echo >>install.log
    echo "gunzip -c $cdpath/$fnam | tar -xf -" >> install.log 2>&1
    gunzip -c $cdpath/$fnam | tar -xf - 
  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
needgui=y

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"
echo "3 - HPUX"
echo "4 - Linux"
echo "5 - NCR"
echo "6 - SCO"
echo "7 - Solaris"
echo "8 - Unixware"
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"
      osnam=dgx
      byteorder=ris
      ;;
  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
      ;;
  8*) osname="Unixware"
      osnam=unw
      byteorder=int
      ;;
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 your 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.prelinked"
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

##
##  Install GUI Client files?
##

echo
echo "Most unix systems can run APPX applications in a client/server model"
echo "with Microsoft Windows PCs connected to a TCP/IP network.  This gives"
echo "Windows users the option of running unix based APPX applications using"
echo "a Windows based GUI Interface.  To use this option, you will need to"
echo "download the executable file from the $putpath/winclient unix"
echo "directory to each PC, set it up to run with a '-c' option, have the"
echo "GCS option on your unix APPX registration, and configure/run the"
echo "$putpath/tools/start-appxd.sh script."
echo
echo "*** WARNING: If you are upgrading, you must upgrade the client"
echo "             software on all of your APPX client PCs.  Failure to"
echo "             do this could cause your client software to fail."
echo
echo "Will you need the Windows GUI Client software?"
echo "=============================================="
echo "( enter a 'y' or 'n', Default=$needgui )"
read a
if [ ! "$a" = "" ]
then
  needgui=$a
fi

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

##
##  Summarize and confirm
##

echo "########################################################################"
echo "##               APPX INSTALL SUMMARY AND CONFIRMATION                ##"
echo "########################################################################"
echo
echo "Installing APPX files for: $osname"
echo "                from path: $cdpath"
echo "                into path: $putpath"
echo "                  as user: `id|cut -f1 -d\ `"
echo
echo "          main APPX files: $needappx"
echo "                 linkable: $needlink"
echo "               GUI Client: $needgui"
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
if [ "$needgui" = "yes" ]
then
  APPX_VALIDATE "Presentation Client Files" "Xtra_gui.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 "               GUI Client: $needgui" >>install.log
  echo >>install.log
fi

##
##  Check for GNUzip and isntall 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

##
##  Install GUI Client
##

if [ "$needgui" = "yes" ]
then
  APPX_INSTALL "Xtra_gui.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*"
  echo "chown root tools/appxd ; chmod u+s tools/appxd"

  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 777 appx*" >>install.log
    chmod 777 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

    echo >>install.log
    echo "chown root tools/appxd" >>install.log
    chown root tools/appxd 2>&1 |tee -a install.log

    echo >>install.log
    echo "chmod u+s tools/appxd"
    chmod u+s tools/appxd

  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"
##  06/24/02  KAD   ECR #6680, fixed use of $PWD, now setting owner and permissions
##                  for appxd, changed gunzip/tar to use a pipe instead of temp file
##
#################################################################################