echo "------------------------------------------------------------------------"
echo "  Sunbelt install UNIX software procedure   Version 10.8   07 Oct 2025"
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 "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 is writable.
#
WORKDIR="/home/sunbelt.install"

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

#==============================================================================
# Make sure that 'glibc' is installed.
#

if [ ! -w "/dev/null" ]
 then
  command -v ldd 2>&1
 else
  command -v ldd > /dev/null 2>&1
fi

status=$?
if [ $status != 0 ]
 then
  echo "'glibc' is not detected and MUST be installed"
  echo "before executing the 'suninst' installation script!"
  echo
  echo "Do you want to abort?"
  echo "Enter {q} to abort!"
  echo
  read answer
  case "$answer" in
    [Qq] ) echo
      echo "Installation aborted."
      exit 1 ;;
  esac
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"
DEFAULT="/home/sunbelt/$PRODUCT.$VERSION"

#==============================================================================
# Make sure software is not already running.
#
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 "-----------------------------------------------------------------------"
echo "Specify the directory for the Sunbelt software. [$DEFAULT]:"
read INSTALL
if [ "$INSTALL" = q -o "$INSTALL" = Q ]
 then
  echo
  echo "Installation aborted."
  exit 1
fi

INSTALL=${INSTALL:=$DEFAULT}
if [ "$INSTALL" = "$DEFAULT" ]
 then
  if [ ! -d /home ]
   then
    mkdir /home >/dev/null 2>&1
    if [ $? != 0 ]
     then
      echo
      echo "Unable to create '/home'.  Aborting process."
      echo
      exit 1
    fi
    chmod 777 /home >/dev/null 2>&1
  else
    chmod 777 /home >/dev/null 2>&1
    if [ ! -w /home ]
     then
      echo
      echo "Insufficient access privileges to /home.  Aborting process."
      echo
      exit 1
    fi
  fi
  if [ ! -d /home/sunbelt ]
   then
    mkdir /home/sunbelt >/dev/null 2>&1
    if [ $? != 0 ]
     then
      echo
      echo "Unable to create '/home/sunbelt'.  Aborting process."
      echo
      exit 1
    fi
    chmod 777 /home/sunbelt >/dev/null 2>&1
  else
    chmod 777 /home/sunbelt >/dev/null 2>&1
    if [ ! -w /home/sunbelt ]
     then
      echo
      echo "Insufficient access privileges to /home/sunbelt.  Aborting process."
      echo
      exit 1
    fi
  fi
  if [ ! -d $INSTALL ]
   then
    mkdir $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
  else
    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
    mkdir $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
  else
    chmod 777 $INSTALL
    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, 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" = "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 "-----------------------------------------------------------------------"
