#!/bin/sh

export PATH=/usr/bin:/bin:/sbin:$PATH
export HOME=/root

RPM_PATH=
UPDATE_FLAG=0

while getopts "uip:" OPT
do
  case ${OPT} in
    p)
      echo "Supplied RPM path: ${OPTARG}"
      RPM_PATH=${OPTARG}
      ;;
    u)
      echo "Called for update"
      UPDATE_FLAG=1
      ;;
    i)
      echo "Called for first install"
      UPDATE_FLAG=2
      ;;
    \?)
      echo "Invalid option: -${OPTARG}" >&2
      exit 1
      ;;
    :)
      echo "Option -${OPTARG} requires a value" >&2
      exit 1
      ;;
  esac
done

#in this case, we do something only in case of an upgrade
if [ "$UPDATE_FLAG" != "1" ]; then
  exit 0
fi

#display the cluster status right before the upgrade in order to log if something was failing at this point
#try to setup the profile completely - maybe the command works
echo "`/bin/date` ASYAPSPRE: running default profile"
. /etc/profile

echo "`/bin/date` ASYAPSPRE: Reducing CPU and I/O priority for this script"
#reprio this script with reduced prio
/opt/webservice/scripts/re_prio.sh $$

echo "`/bin/date` ASYAPSPRE: Dumping cluster status"
/usr/sbin/crm_mon -1fA

#remember if this is a type 1 HF and restore memory tuning state if it was previously active
is_mem_tun_enabled=false
#idea is: if one of RMX or Assistant are running but Portal is not running then consider
# this to me a MemoryTuning enabled state
#preinstall is called before openais stop
ping -c 1 -w 1 192.0.2.3 && is_mem_tun_enabled=true
ping -c 1 -w 1 192.0.2.5 && is_mem_tun_enabled=true
/etc/init.d/webservice status && is_mem_tun_enabled=false
if $is_mem_tun_enabled; then
	echo "`/bin/date` ASYAPSPRE: Detected enabled MemoryTuning state before Type 1 PLT HF. Will reenable MemoryTuning after the PLT HF within one hour."
	touch /tmp/.ASyaps.re_enable_memory_tunning
else
	echo "`/bin/date` ASYAPSPRE: MemoryTuning is not enabled."
fi

echo "`/bin/date` ASYAPSPRE: removing old backups and RLC copy in order to free some space on /var/pgsql partition"
nice -n 10 ionice -c 3 rm -rf /var/pgsql/olddisk/Assistant/data/BACKUP/ /var/pgsql/olddisk/Assistant/data/SWS/

echo "`/bin/date` ASYAPSPRE: Finished preinstall actions"

exit 0
