echo "------------------------------------------------------------------------"
echo "  Sunbelt install Mac OS software procedure    Version 10.5A 30 Jan 2023"
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 "The software is released as a tar file and should have already been"
echo "extracted."
echo
echo "    WARNING:" 
echo "         Do not locate the release 'product.tar' file in the same"
echo "         directory containing the extracted release files."
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

#==============================================================================
# Make sure the working directory being used is correct and writable.
#
WORKDIR="$PWD"
echo
echo "------------------------------------------------------------------------"
echo
echo "Using working directory shown as follows:"
echo
echo "     '$WORKDIR'"
echo
echo "QUIT IF WRONG DIRECTORY:  Enter (q)"
read answer
case "$answer" in
  [Qq] ) echo
    echo "Installation aborted."
    exit 1 ;;
esac

if [ ! -w $WORKDIR ]
 then
  echo
  echo "Insufficient access privileges to $WORKDIR.  Aborting process."
  echo
  exit 1
fi

#==============================================================================
# Make sure the default directory used for output is correct.
#
DEFAULTBASE=""
DEFUSER="/Users/$USER"
echo
echo "------------------------------------------------------------------------"
echo " The user must select an existing base directory that is to be used"
echo " when installing the Sunbelt software:"
echo
echo "1 - '$DEFUSER'"
echo
echo "2 - 'User Defined'"
echo
echo "Select Option:  Enter only values (1 or 2)"

read answer
case "$answer" in
  [1] ) DEFAULTBASE=$DEFUSER;;
esac

echo
echo "The base output directory is: '$DEFAULTBASE'"
echo

if [ "$DEFAULTBASE" != "" ]
 then
   if [ ! -w $DEFAULTBASE ]
    then
     echo
     echo "Insufficient access privileges to $DEFAULTBASE.  Aborting process."
     echo
     exit 1
   fi
 fi

#==============================================================================
# Get default directory to place software from "files.txt" file.
#
while read LINE
do
       if [ "$PRODUCT" = "" ]
        then
         PRODUCT=`echo $LINE | tr -dc [:alnum:] | tr '[A-Z]' '[a-z]'`
       else
         VERSION=`echo $LINE | tr -dc [:alnum:] | tr '[A-Z]' '[a-z]'`
         break
       fi
done < "files.txt"

if [ "$DEFAULTBASE" != "" ]
 then
     DEFAULT="$DEFAULTBASE/sunbelt/$PRODUCT.$VERSION"
     echo "The DEFAULT Output directory is set to '$DEFAULT'"
 else
     DEFAULT=""
     echo "The DEFAULT Output directory is to be entered by user! "
fi
echo

#==============================================================================
# Remove any Sunbelt release '.tar' files from the release working directory.
#
echo "------------------------------------------------------------------------"
echo
echo "Remove Sunbelt release 'product.tar' files from the  working directory."
echo

for x1 in `ls -1 cliu*.$VERSION.tar plbi*.$VERSION.tar sdmu*.$VERSION.tar\
 srvu*.$VERSION.tar 2>/dev/null`
 do
   if [ -f $x1 ]
    then
        echo "Removed: '$x1'"
        rm $x1
   fi
done

#==============================================================================
# Make sure software is not already running.
#
echo "------------------------------------------------------------------------"
echo
echo "Make sure software is not already running."
echo

if [ -f plb ]
 then
  HELP="PLB"
  answer=`ps -ef 2>/dev/null | grep " plb " | wc -l`
  if [ $answer -gt 1 ]
   then
    echo
    echo "WARNING!:"
    echo "'plb' is being used and, therefore, it cannot be copied"
    echo "over.  The software must be installed in a different directory"
  fi
elif [ -f sundm ]
 then
  HELP="SUNDM"
  answer=`ps -ef 2>/dev/null | grep " sundm " | wc -l`
  if [ $answer -gt 1 ]
   then
    echo
    echo "WARNING!:"
    echo "'sundm' is being used and, therefore, it cannot be copied"
    echo "over.  The software must be installed in a different directory"
  fi
elif [ -f plbserve ]
 then
  HELP="PLBSERVE"
  answer=`ps -ef 2>/dev/null | grep " plbserve " | wc -l`
  if [ $answer -gt 1 ]
   then
    echo
    echo "WARNING!:"
    echo "'plbserve' is being used and, therefore, it cannot be copied"
    echo "over.  The software must be installed in a different directory"
  fi
elif [ -f code/plbwebsrv ]
 then
  HELP="PLBWEB"
  answer=`ps -ef 2>/dev/null | grep " plbwebsrv " | wc -l`
  if [ $answer -gt 1 ]
   then
    echo
    echo "WARNING!:"
    echo "'plbwebsrv' is being used and, therefore, it cannot be copied"
    echo "over.  The software must be installed in a different directory"
  fi
fi

echo "-----------------------------------------------------------------------"
echo "Initializing file names and permissions."
PATH=`pwd`:$PATH	#Add current directory to path

#==============================================================================
# Make sure the scripts are executable.
#
chmod 775 *inst*	#Make sure scripts are executable

#==============================================================================
# Change access modes as appropriate.
#
suninstchmod		#Set all files as RW and ones with no extension as RWX

#=============================================================================
# Convert all file names to lower case
#
suninstlowcase

#=============================================================================
# Get Company Name and Authorization Number from user.  Keep trying until
# the user enters the correct name and number or they quit.
#

EXITLOOP=0
echo "-----------------------------------------------------------------------"
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 /home 0 >/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.
#
echo "-----------------------------------------------------------------------"
if [ "$DEFAULT" = "" ]
 then
     echo "The user must specify the directory for the Sunbelt software."
     echo
     echo "   [USER MUST ENTER THE DESTINATION DIRECTORY!]:"
     echo
     echo "   For Example:  [/Users/sunbelt/$PRODUCT.$VERSION]:"
 else
     echo "User can override destination directory or hit enter to continue:"
     echo
     echo "  The Current Default Output Directory is:"
     echo "  [$DEFAULT]"
     echo
     echo "Hit Enter to accept default output! OR "
     echo "Reenter full path to override! OR"
     echo "Enter 'q' to quit!"
fi

EXITLOOP=0
while [ "$EXITLOOP" != 1 ]
 do
   read INSTALL
   if [ "$INSTALL" = q -o "$INSTALL" = Q ]
    then
     echo
     echo "Installation aborted by user 'q' command!"
     exit 1
   fi

   INSTALL=${INSTALL:=$DEFAULT}

   if [ "$INSTALL" = "" ]
    then
     echo
     echo "Destination directory MUST BE entered by user!"
     echo "Or User can enter 'q' to quit installation!" 
     echo
     continue
   else
     EXITLOOP=1
   fi
done
echo "Installation output directory is:  '$INSTALL'"
echo

echo "-----------------------------------------------------------------------"
if [ "$INSTALL" = "$DEFAULT" ]
 then
  if [ ! -d $INSTALL  ]
   then
    echo "1... 'mkdir -p $INSTALL'"
    mkdir -p $INSTALL  >/dev/null 2>&1
    if [ $? != 0 ]
     then
      echo
      echo "Unable to create '$INSTALL'.  Aborting process."
      echo
      exit 1
    fi
    chmod 777 $INSTALL  >/dev/null 2>&1

    PWDSAVE=$PWD
    cd $INSTALL
    cd ..
    chmod 777 $PWD  >/dev/null 2>&1
    cd $PWDSAVE

  else
    echo "2... 'chmod 777 $INSTALL'"
    chmod 777 $INSTALL  >/dev/null 2>&1
    if [ ! -w $INSTALL ]
     then
      echo
      echo "Insufficient access privileges to $INSTALL .  Aborting process."
      echo
      exit 1
    fi
  fi

else    

  if [ ! -d $INSTALL ]
   then
    echo "3... 'mkdir -p $INSTALL'"
    mkdir -p $INSTALL >/dev/null 2>&1
    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 777 $INSTALL  >/dev/null 2>&1

    PWDSAVE=$PWD
    cd $INSTALL
    cd ..
    chmod 777 $PWD  >/dev/null 2>&1
    cd $PWDSAVE

  else
    echo "4... 'chmod 777 $INSTALL'"
    chmod 777 $INSTALL  >/dev/null 2>&1
    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
fi	#Finished creating directory structure

#==============================================================================
# Save old configuration file(s) if present.
#
if [ -f $INSTALL/sundm.cfg ]
 then
  mv $INSTALL/sundm.cfg $INSTALL/sundm.cfg.sav
fi
if [ -f $INSTALL/plb.ini ]
 then
  mv $INSTALL/plb.ini $INSTALL/plb.ini.sav
fi
if [ -f $INSTALL/plbserve.ini ]
 then
  mv $INSTALL/plbserve.ini $INSTALL/plbserve.ini.sav
fi
if [ -f $INSTALL/code/plbwebsrv.ini ]
 then
  mv $INSTALL/code/plbwebsrv.ini $INSTALL/code/plbwebsrv.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
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. This recurses down through any directories that might
# be present.
#
echo "-----------------------------------------------------------------------"
echo "Serializing the files."
suninstserialize """$CONAME""" $SERIAL """$INSTALL""" 1

#==============================================================================
# Now copy to the installation directory.
#
echo "-----------------------------------------------------------------------"
echo "Copying files to directory $INSTALL."
#mv * $INSTALL >/dev/null
cp -rp * $INSTALL
rm -r $WORKDIR/*

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,"
    echo "     then be sure the PATH variable is modified in the .profile for"
    echo "     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" = "SUNDM" ]
 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 'sundm.cfg' file to include the correct IP address,"
  echo "     PORT number, paths 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, paths and any other parameters as needed."
fi
if [ "$HELP" = "PLBWEB" ]
 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 'plbwebsrv.ini' file to include the correct IP address,"
  echo "     PORT number, paths and any other parameters as needed."
fi
echo "-----------------------------------------------------------------------"
