echo "-----------------------------------------------------------------------"
echo "          Sunbelt install UNIX software procedure (20 Mar 2003)"
echo "-----------------------------------------------------------------------"
echo "You have initiated the installation procedure for Sunbelt software."
echo "This procedure may be used to update previously installed software as"
echo "it is not destructive to non-installation related files."
echo
echo "The installation prompts for site specific information, with defaults"
echo "indicated within square brackets.  The installation can be aborted at"
echo "any prompt by entering a 'q' to quit."
echo
echo "This script should be run under 'sh'."
echo
echo "YOU MUST BE SUPER-USER TO RUN THIS PROCEDURE.  Enter (q) if you're not."
read answer
case "$answer" in
    [Qq] ) echo
        echo "Installation aborted."
        exit 1 ;;
esac

DATAMODE=${DATAMODE:=644}
DATAMODEALL=${DATAMODEALL:=666}
EXECMODE=${EXECMODE:=755}
#
# establish default commands.
#
SECRET="/.invisible"
if [ "$1" = "loaded" ]
 then
  SYSTYPE="NONE"
  shift
elif [ -f /bin/dosread ]        # AIX RT or RS/6000
 then
  SYSTYPE="AIX"
elif [ -f /usr/bin/doscp ]      # SCO/386
 then
  SYSTYPE="SCO"
elif [ -f /usr/bin/dosget ]     # Interactive/386
 then
  SYSTYPE="INTERACT"
else
  answer="x"
  if [ ! -d $SECRET ]
   then
    echo "Directory $SECRET must be created and all distribution files"
    echo "copied to that directory before running this installation program"
    exit 1
  elif [ ! -w $SECRET ]
   then
    echo "Directory $SECRET is not writable by the current login name."
    exit 1
  elif [ ! -f $SECRET/files.txt -a ! -f $SECRET/FILES.TXT ]
   then
    echo "The files have not been copied to $SECRET."
    exit 1
  fi
  while [ "$answer" != "Y" -a "$answer" != "y" ]
   do
    echo "Have all the files been read into directory $SECRET? (y,n,q)"
    read answer
    case "$answer" in
      [Qq] ) echo
             echo "installation aborted."
             exit 1 ;;
      [Nn] ) echo
             echo "You must first copy all of the files to $SECRET."
             exit 1 ;;
      [Yy] ) SYSTYPE="NONE";;
    esac
  done
fi
#echo System type=$SYSTYPE
if [ $SYSTYPE = "SCO" -o $SYSTYPE = "INTERACT" ]
 then
  echo "-----------------------------------------------------------------------"
  EXITLOOP=0
  while [ "$EXITLOOP" != 1 ]
   do
    echo "The software is to be loaded from what drive? ([a],b)"
    read DEVICE
    DEVICE=${DEVICE:=a}
    case "$DEVICE" in
      [AaBb] ) EXITLOOP=1 ;;
      [Qq] ) echo
             echo "Installation aborted."
             exit 1 ;;
        *  ) echo
             echo "\007Please select one of the displayed options." ;;
    esac
  done
fi
# 
# Create the receiving directory if it does not exist.
#
if [ ! -d $SECRET ]
 then
  echo creating $SECRET
  mkdir $SECRET >/dev/null 2>/dev/null
  if [ $? != 0 ]
   then
    echo
    echo "Unable to create $SECRET.  Aborting process."
    echo
    exit 1
  fi
  chmod 0777 $SECRET
 else
  if [ ! -w $SECRET ]
   then
    echo
    echo "Insufficient access privileges to $SECRET.  Aborting process."
    echo
    exit 1
  fi
fi
#==============================================================================
# Start copying the software if we need to do that.
#
cd $SECRET
if [ "$SYSTYPE" = "NONE" ]
 then
  EXITLOOP=1
else
  echo "-----------------------------------------------------------------------"
  echo "Insert diskette 1 and confirm with CR."
  read answer
  if [ "$answer" = q -o "$answer" = Q ]
   then
    echo
    echo "Installation aborted."
    exit 1
  fi
  EXITLOOP=0
fi
#
while [ "$EXITLOOP" != 1 ]
 do
  case "$SYSTYPE" in
    "AIX" )
      echo "Reading the software using AIX format."
      for i in `dosdir`
       do
        if [ $i = "Free" -o $i = "There" ]
         then
          break
        fi
        if [ "$1" = "debug" ]
         then
          echo dosread $i $i
        fi
        dosread $i $i
      done;;
    "SCO" )
      echo "Reading the software using SCO format."
      for i in `dosls $DEVICE:`
       do
        if [ "$1" = "debug" ]
         then
          echo doscp $DEVICE.$i .
        fi
        doscp $DEVICE:$i .
      done;;
    "INTERACT" )
      echo "Reading the software using INTERACTIVE format."
      dosdir $DEVICE: > directory
      exec < directory
      while read name ext line
       do
        if [ "$name $ext" = "Volume in" ]
         then
          continue
        elif [ "$name $ext" = "Directory of" ]
         then
          continue
        elif [ "$name" = "" ]
         then
          continue
        elif [ "$ext" = "File(s)" ]
         then
          break
        fi
        dosget $DEVICE:$name.$ext . >/dev/null 2>&1
        if [ -f $name.$ext ]
         then
          if [ "$1" = "debug" ]
           then
            echo dosget $DEVICE:$name.$ext .
          fi
         else
          dosget $DEVICE:$name . >/dev/null 2>&1
          if [ -f $name ]
           then
            if [ "$1" = "debug" ]
             then
              echo dosget $DEVICE:$name .
            fi
           else
            echo Unable to read either $name.$ext or $name.
          fi
        fi
      done
      rm directory
      exec </dev/tty;;
  esac
  echo "Insert next diskette & tap enter.  Enter (f) if finished."
  read answer
  answer=${answer:=x}
  case "$answer" in
    [Ff] ) EXITLOOP=1 ;;
    [Qq] ) echo
           echo "Installation aborted."
           exit 1 ;;
  esac
done
#=============================================================================
# Finished reading in files at this point.
#=============================================================================
# Since the number and names of supplied utilities changes regularly, the move
# is just wild-carded for simplicity of maintenance.  This avoids messages
# from copying of plbcmp, debugger, editor for customers not purchasing these
# optional items.
#
echo "Setting up proper file names."
#
# Convert all file names to lower case
#
for i in `ls *`
do
  j=`echo $i | tr '[A-Z]' '[a-z]'`
  if [ ${i} != ${j} ]
  then
    mv $i $j>/dev/null 2>&1
  fi
done
#=============================================================================
# Convert *.me & *.rfm file names to upper case
#
for i in `ls *.me *.rfm 2>/dev/null`
do
  j=`echo $i | tr '[a-z]' '[A-Z]'`
  if [ ${i} != ${j} ]
  then
    mv $i $j>/dev/null 2>&1
  fi
done
#=============================================================================
# Get Company Name and Authorization Number from user.  Keep trying until
# the user enters the correct name and number or they quit.
#
echo "-----------------------------------------------------------------------"
EXITLOOP=0
chmod $EXECMODE unixinst
while [ "$EXITLOOP" != 1 ]
 do
  echo "Company Name from Invoice or Ship List:"
  read CONAME
  CONAME=${CONAME:=/usr/plc}
  if [ "$CONAME" = q -o "$CONAME" = Q ]
   then
    echo
    echo "Installation aborted."
    exit 1
  fi

  echo "Serial Number from Invoice or Ship List:"
  read SERIAL
  SERIAL=${SERIAL:=/usr/plc}
  if [ "$SERIAL" = q -o "$SERIAL" = Q ]
   then
    echo
    echo "Installation aborted."
    exit 1
  fi

  ./unixinst """$CONAME""" $SERIAL >/dev/null 2>&1
  RETVAL=$?
  if [ "$RETVAL" != "0" ]
   then
    echo
    echo "Invalid Company Name or Serial Number.  Please try again."
    echo
  else
    EXITLOOP=1
  fi
done
#==============================================================================
# Find out where to put the software.
#
if [ -f ./plb ]
 then
  HELP="PLB"
  DEFAULT="/usr/plc"
  answer=`ps -ef 2>/dev/null | grep " plb " | wc -l`
  if [ $answer -gt 1 ]
   then
    echo "\nWARNING!:"
    echo "'plb' is being used and, therefore, it cannot be copied"
    echo "over.  The software must be installed in a different directory"
    DEFAULT=""
  fi
elif [ -f ./sunfm ]
 then
  HELP="SUNFM"
  DEFAULT="/usr/sunfm"
  answer=`ps -ef 2>/dev/null | grep " sunfm " | wc -l`
  if [ $answer -gt 1 ]
   then
    echo "\nWARNING!:"
    echo "'sunfm' is being used and, therefore, it cannot be copied"
    echo "over.  The software must be installed in a different directory"
    DEFAULT=""
  fi
elif [ -f ./plbserve ]
 then
  HELP="PLBSERVE"
  DEFAULT="/usr/plbserve"
  answer=`ps -ef 2>/dev/null | grep " plbserve " | wc -l`
  if [ $answer -gt 1 ]
   then
    echo "\nWARNING!:"
    echo "'plbserve' is being used and, therefore, it cannot be copied"
    echo "over.  The software must be installed in a different directory"
    DEFAULT=""
  fi
fi
# End of $1 debug conditional
echo "-----------------------------------------------------------------------"
echo "Specify the directory for the Sunbelt software. [$DEFAULT]:"
read INSTALL
INSTALL=${INSTALL:=$DEFAULT}
if [ "$INSTALL" = q -o "$INSTALL" = Q ]
 then
  echo
  echo "Installation aborted."
  exit 1
fi
if [ ! -d $INSTALL ]
 then
  mkdir $INSTALL >/dev/null 2>/dev/null
  if [ $? != 0 ]
   then
    echo
    echo "Unable to create $INSTALL.  Aborting process."
    echo "Verify naming convention given is within UNIX allowances."
    echo
    exit 1
  fi
  chmod 0777 $INSTALL
 else
  if [ ! -w $INSTALL ]
   then
    echo
    echo "Insufficient access privileges to $INSTALL.  Aborting process."
    echo "Re-verify destination directory's access privileges."
    echo
    exit 1
  fi
fi
#==============================================================================
# Save old configuration file if present.
#
if [ -f $INSTALL/sunfm.cfg ]
 then
  mv $INSTALL/sunfm.cfg $INSTALL/sunfm.cfg.sav
fi
#==============================================================================
# Save old configuration file if present.
#
if [ -f $INSTALL/plb.ini ]
 then
  mv $INSTALL/plb.ini $INSTALL/plb.ini.sav
fi
#==============================================================================
# Ask for optional screen definition files to be copied.
#
if [ -f ./ansi.def -a -f $INSTALL/ansi.def ]
 then
  echo "-----------------------------------------------------------------------"
  echo "Install the screen definition files? (y,[n]):"
  read installdef
  installdef=${installdef:=n}
  if [ "$installdef" = q -o "$installdef" = Q ]
   then
    echo
    echo "Installation aborted."
    exit 1
  fi
  if [ $installdef = "Y" ]
   then
    installdef="y"
  fi
 else
  installdef=y
fi
#============================================================================
# Uncompress or Unpack the library if it exists.
#
if [ -f plbsys.zc ]
 then
  mv plbsys.zc plbsys.Z
elif [ -f plbsys.zp ]
 then
  mv plbsys.zp plbsys.z
fi
if [ -f plbsys.Z ]
 then
  if [ -f /usr/bin/uncompress ]
   then
    uncompress plbsys
    mv plbsys plbsys.lib
  else
    echo "\007Library files are compressed and 'uncompress' is not available."
  fi
elif [ -f plbsys.z ]
 then
  if [ -f /usr/bin/unpack ]
   then
    unpack plbsys >/dev/null 2>&1
    mv plbsys plbsys.lib
  else
    echo "\007Library files are packed and 'unpack' is not available."
  fi
fi
if [ -f cxglobal.o -a -f plbsys.lib -a -f /bin/ar ]
 then
  ar r plbsys.lib cxglobal.o
  rm cxglobal.o
fi
#
# Check for object modules that need linking.
#
if [ -f hexdump.o ]
 then
  if [ -f /bin/cc -o -f /usr/bin/cc ]
   then
    echo "Linking the object modules."
    cc -s $LFLAGS -o getkey getkey.o >/dev/null
    cc -s $LFLAGS -o hexdump hexdump.o >/dev/null
    cc -s $LFLAGS -o makedef makedef.o >/dev/null
    cc -s $LFLAGS -o objmatch objmatch.o >/dev/null
    cc -s $LFLAGS -o pxlat pxlat.o >/dev/null
    cc -s $LFLAGS -o revterm revterm.o >/dev/null
    cc -s $LFLAGS -o plb plbsys.lib >/dev/null
    cc -s $LFLAGS -o sunaamdx sunaamdx.o >/dev/null
    cc -s $LFLAGS -o sunindex sunindex.lib >/dev/null
    cc -s $LFLAGS -o sunsort sunsort.lib >/dev/null
    cc -s $LFLAGS -o tsetup tsetup.o >/dev/null
  else
    echo "Unable to link the needed files. Contact Sunbelt customer support."
    exit 1
  fi
fi
#==============================================================================
# Serialize all files.
#
./unixinst """$CONAME""" $SERIAL """$INSTALL""" * >/dev/null 2>&1
#
# For some reason, the file name 'PLBCMP' cannot be read on RS/6000 machines.
# It was renamed to 'PLBCMP.BAT' and now must be named correctly.  At any
# rate this is a special script and is different from all the other scripts
# that were just created.
#
if [ -f plbcmp.bat ]
 then
  mv plbcmp.bat plbcmp
fi
#
chmod $DATAMODE * >/dev/null 2>&1
for i in `ls *.ME *.RFM *.plc profile.* *.src 2>/dev/null`
 do
  echo "Copying PL/B code files to directory $INSTALL."
  mv *.ME $INSTALL >/dev/null 2>&1 # the READ.ME information file
  mv *.RFM $INSTALL >/dev/null 2>&1 # all pertinent Release ForMs
  mv *.plc $INSTALL >/dev/null 2>&1 # all the .plc utilities
  mv profile.* $INSTALL >/dev/null 2>&1 # the sample .profile script
  chmod $DATAMODEALL *.src >/dev/null 2>&1
  mv *.src $INSTALL >/dev/null 2>&1 # all the .src samples
  break
done
#
if [ $installdef = "y" ]
 then
#
# all the tic source files
#
  for i in `ls dpt82*.ti 2>/dev/null`
   do
    echo "Copying term info files."
    chmod $DATAMODE dpt82*.ti
    mv dpt82*.ti $INSTALL >/dev/null 2>&1
    break
  done
#
# all the screen definition files
#
  for i in `ls *.def 2>/dev/null`
   do
    echo "Copying screen definition files."
    chmod $DATAMODE *.def
    mv *.def $INSTALL >/dev/null 2>&1
    break
  done
#
# all the 82xx .dll files
#
  for i in `ls *.dll 2>/dev/null`
   do
    echo "Copying 8200 dll files."
    chmod $DATAMODE *.dll
    mv *.dll $INSTALL >/dev/null 2>&1
    break
  done
 else
  rm *.def *.dll *.ti >/dev/null 2>&1
fi
#
# Now the library
#
if [ -f plbsys.lib ]
 then
  echo "Copying library files."
  mv plbsys.lib $INSTALL
fi
#
# Now the executable files
#
echo "Copying executable files."
chmod $EXECMODE * 2>/dev/null
OWNER=`grep :0: /etc/group | cut -f4 -d: | cut -f1 -d,`
GROUP=`grep :0: /etc/group | cut -f1 -d:`
for i in `ls plb plbrl sunaamdx sunindex sunsort sunfm 2>/dev/null`
 do
  chown $OWNER $i
  chgrp $GROUP $i
  chmod 5$EXECMODE $i
done
mv * $INSTALL >/dev/null
#
# remove several files that are no longer needed.
#
cd $INSTALL
if [ -f qkaamdex ]
 then
  rm qkaamdex
fi
if [ -f rm015032 ]
 then
  rm rm015032
fi
if [ -f rm015014 ]
 then
  rm rm015014
fi
#
rmdir $SECRET
echo "-----------------------------------------------------------------------"
if [ "$HELP" = "PLB" ]
 then
  if [ ! -f /usr/dbserial ]
   then
    echo "The software has been installed.  The following needs to be done to"
    echo "finish the installation."
    echo
    echo "  1. Be sure the following command is in the .profile or a .INI file"
    echo "     for each user:"
    echo
    echo "       PLB_SYSTEM=$INSTALL;export PLB_SYSTEM"
    echo
    echo "  2. Define the location of any search paths by using the following"
    echo "     command in the .profile or a .INI file for each user:"
    echo
    echo "       PLB_PATH=/usr/datapath1:/usr/datapath2;export PLB_PATH"
    echo
    echo "     where each path is seperated by ':'"
    echo
    echo "  3. If $INSTALL is not currently in the PATH variable, then be sure"
    echo "     the PATH variable is modified in the .profile for each user."
   else
    echo "The software has been updated.  The following needs to be done to"
    echo "finish the installation."
    echo
    echo "  1. Run makedef for each terminal type on your system and update as"
    echo "     necessary."
    echo
    echo "  2. Read any additional documentation or READ.ME type files."
  fi
fi
if [ "$HELP" = "SUNFM" ]
 then
  echo "The software has been installed.  The following needs to be done to"
  echo "finish the installation."
  echo
  echo "  1. Read any additional documentation or READ.ME type files."
  echo
  echo "  2. Modify the 'sunfm.cfg' file to include the correct IP address,"
  echo "     PORT number and any other parameters as needed."
fi
if [ "$HELP" = "PLBSERVE" ]
 then
  echo "The software has been installed.  The following needs to be done to"
  echo "finish the installation."
  echo
  echo "  1. Read any additional documentation or READ.ME type files."
  echo
  echo "  2. Modify the 'plbserve.ini' file to include the correct IP address,"
  echo "     PORT number and any other parameters as needed."
fi
echo "-----------------------------------------------------------------------"
