#!/bin/bash

###################################
###  POST INSTALL SCRIPT        ###
###################################
umask 0027

if [ "$SIS_DEBUG_MODE" = "true" ]; then
   debug_logfile=/var/tmp/sdcss-kmod_post$$.log
   exec 1<&-       # Close STDOUT file descriptor
   exec 2<&-       # Close STDERR FD
   exec 1<>$debug_logfile # Open STDOUT as $LOG_FILE file for read and write.
   exec 2>&1       # Redirect STDERR to STDOUT
   set -x          # Turn on shell debugging
fi

# Pickup response file settings if available.
RESPONSE_FILE=${RESPONSE_FILE:-/var/tmp/sdcss-agent.response}
if [ ! -f $RESPONSE_FILE ]; then
   RESPONSE_FILE=/etc/sisips/sdcss-agent.response
fi
if [ -f $RESPONSE_FILE ]; then
   . $RESPONSE_FILE
else
    printf  "\nkmod_postinstall using default response settings\n" 1>&2
fi

# Default the sdcss-agent.response values to sane defaults to remove the dependency 
# on its existence. The variables may also be set in the shell environment prior to rpm 
# install execution.
SIS_VERSION=10.0.8.2795
SIS_KMOD_VERSION=10.0.8.2795
BASEDIR=/usr/lib/symantec
PLAT_VARIANT=rhel8
AGENT_TYPE=5

INFUNLIB=/$BASEDIR/sdcssagent/lib/instfunlib-kmod
if [ ! -f $INFUNLIB ]; then
   printf "\nAgent not installed correctly: $INFUNLIB missing.\n"; exit 1
else . $INFUNLIB; fi

# needed to fixup %% substitutions in rc.functions
changeSettings

RCFUNCTION=/etc/symantec/sis/rc.functions
if [ ! -f $RCFUNCTION ]; then
   printf "\nAgent not installed correctly: $RCFUNCTION missing.\n"; exit 1
else . $RCFUNCTION; fi

logmsg "#--- Start kmod package post installation <pid=$$> -------"

if [ ! -d $SIS_ETC_DIR ]; then
	mkdir -p $SIS_ETC_DIR
fi

install_kmod
updateKmodVersion
mergeKernelTxts
if testForBwrap; then
  logmsg "rpm-ostree install skipping driver enablement step. Driver will be enabled later."
else
  enableDriverStart_kmod
fi

printf "Symantec Agent for Linux kmod package installed successfully.\n"

if [ -f $SIS_ETC_DIR/rebootRequired ] || [ -f $SIS_ETC_DIR/fimRebootRequired ]; then
  printf "A reboot is required. Please reboot your machine at the earliest convenience.\n"
fi


if testForBwrap; then
   # can't preserve logs
   logmsg "#--- Stop kmod package post bubblewrap installation <pid=$$> -------"
else
   [ "`ls -1 /var/tmp/sdcss-kmod*.log 2>/dev/null`" ] && [ -d $SIS_LOGDIR ] && \
     mv -f /var/tmp/sdcss-kmod*.log $SIS_LOGDIR
   logmsg "#--- Stop kmod package post installation <pid=$$> -------"
fi

ls -l $SIS_ETC_DIR/*ebootRequired >> $LOGFILE 2>/dev/null
exit 0

