#!/bin/sh

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

. /etc/profile

VERSION=`/bin/getydata -v`

#write version infos to /boot/soco2_maintenance.log
echo "@`/bin/date` on `/bin/hostname` SW version is $VERSION" >> /boot/soco2_maintenance.log

#remove dummy script needed by webservice during upgrade
rm -rf /bin/store_package_unique.sh

#OSFOURK-27947 PRB000078690 Contents of the Firstinstall.XML are displayed via the IP addresses of "customer-interface"
echo "`/bin/date` ASYAPSPOST: doing OSFOURK-27947 ..."
SYSINFO_FIRST_INST_XML_FILE_NAME="/var/opt/sysinfo/firstinst-central.xml"
FIRST_INST_XML_FILE_NAME="/var/opt/ha/conf/firstinst-central.xml"
FIRST_INST_XML_FILE_NAME_TMP="/var/opt/ha/conf/firstinst-central-tmp.xml"
function update_sysinfo_xml() {
    #ensure no link in any case
    readlink -f $SYSINFO_FIRST_INST_XML_FILE_NAME | grep $SYSINFO_FIRST_INST_XML_FILE_NAME || {
        rm -f $SYSINFO_FIRST_INST_XML_FILE_NAME
        grep -v system-root.password $FIRST_INST_XML_FILE_NAME > $SYSINFO_FIRST_INST_XML_FILE_NAME
        chmod 644 $SYSINFO_FIRST_INST_XML_FILE_NAME
    }
    [ "x$1" = "0" ] && {
        [ -e $SYSINFO_FIRST_INST_XML_FILE_NAME ] && return 0
    }
    #if we reach here it means no link and request is to update
    grep -v system-root.password $FIRST_INST_XML_FILE_NAME > $SYSINFO_FIRST_INST_XML_FILE_NAME
    chmod 644 $SYSINFO_FIRST_INST_XML_FILE_NAME
}
#force-update central XML during HF activation
update_sysinfo_xml 1

#OSFOURK-8124 - update FI scripts - Nezihi and phase1,2
echo "`/bin/date` ASYAPSPOST: doing OSFOURK-8124 - update FI scripts ..."
sed -i -e /install-appliance-phase/d /instmedia/MD5SUMS
cp -pv /opt/yaps/script/update_new/install-appliance-phase* /instmedia/.
cp -pv /opt/soco-common/firstinst-netw.sh /instmedia/config/.
#also update nezihi in /var/opt/firstinstall
rm /var/opt/firstinstall/firstinst-netw.sh
cp -pv /opt/soco-common/firstinst-netw.sh /var/opt/firstinstall/firstinst-netw.sh

#insert other fixes here

#OSFOURK-27475 Prepare VM for migration from ESXi to HyperV
/usr/sbin/system-is-virtual && {
echo "`/bin/date` ASYAPSPOST: doing OSFOURK-27475 update initrd to support VM migration to HyperV ..."
sed -i -e "s/pvscsi/hv_storvsc/" /etc/sysconfig/kernel
/sbin/mkinitrd
echo "`/bin/date` ASYAPSPOST: OSFOURK-27475 done."
}

#OSFOURK-24012 V10R1.41 - NUC from V10R1.40 - Assistant Warning - partition / reaches 92%
#OSFOURK-27303 V11R0.22. HF1 - Assistant Warning - partition / reaches 87%
echo "`/bin/date` ASYAPSPOST: doing OSFOURK-24012 - deleting OLD world ..."
/instmedia/opt/yaps/script/update_new/update-new-preparations.sh clear_other_world
echo "`/bin/date` ASYAPSPOST: doing OSFOURK-24012 - OLD world deletion finished."

#[OSFOURK-26572] Installation of OSEM on standard EcoServer SSD
[ "x`/opt/webservice/scripts/gethaipsetupentry.sh system.has4Kube`" = "x1" ] && {
    ( system-is-ecoserver || system-is-branch ) && {
        #IMPORTANT !!! - this code is valid only for PLT HF1
#        if [ "x`getydata -v`" = "xV11_R0.22.1" -o "x`getydata -v`" = "xV11_R0.22.2" ] ; then
            #if we land here then we are on Eco/Branch and activation is from HF0 to HF1 while Kube is enabled => keep OLD HD style via flag file
            touch /opt/4kube-big-disk
            echo "`/bin/date` ASYAPSPOST: created /opt/4kube-big-disk"
#            echo "`/bin/date` ASYAPSPOST: created /opt/4kube-big-disk because we are running on Eco/Branch and update was HF0 -> HF1"
#        else
#            echo "`/bin/date` ASYAPSPOST: ERROR ERROR WARNING WARNING FATAL FATAL - please implement this branch for HF2 or above !!!!"
#        fi
    }
}

#[OSFOURK-28496] - remove wwwrun sudoers entry for client.pl
sed -i -e "/bin\/client.pl/d" /etc/sudoers

#OSFOURK-27502 - /opt/webservice/scripts/sshx not working on V11 R0.22.1 Duplex/GSD
# we restart fast update daemon, haconfd and inventory
echo "`/bin/date` ASYAPSPOST: doing OSFOURK-27502 ..."
echo "Sleeping 2 mins to ensure all post-webservice restart actions finished ..."
sleep 120
echo "`date` Listing fast update daemon process ..."
ps -wwweo pid,cmd | grep -- '--fast-swupdated-server'
echo "Will now kill it and wait for the new instance to appear ..."
kill $(ps -wwweo pid,cmd | grep -- '--fast-swupdated-server' | grep -v grep | awk '{print $1}')
sleep 20
echo "`date` Listing again fast update daemon process ..."
ps -wwweo pid,cmd | grep -- '--fast-swupdated-server'
echo "Listing java cache before deletion ..."
ls -al /root/.cache/javasharedresources/
rm -fv /root/.cache/javasharedresources/*
echo "Collecting HA resources ..."
/usr/sbin/crm resource list 1>/dev/shm/hf1-crm-res-list.out 2>&1
doit=1
/opt/webservice/scripts/is_duplex
if [ $? -eq 1 ] ; then
  ps -ef | grep lxc-start | grep -q Assistant || doit=0
fi
if grep -q haconfd /dev/shm/hf1-crm-res-list.out ; then 
  echo "`date` haconfd resource found"
  if [ $doit -eq 0 ] ; then
    echo "Duplex non-primary node -> skipping haconfd restart"
  else
    /usr/sbin/crm --wait resource restart clo4k_haconfd
    echo "`date` haconfd restart finished with $?"
  fi
else
  echo "`date` haconfd resource not found"
fi
sleep 10
echo "Listing java cache after haconfd restart ..."
ls -al /root/.cache/javasharedresources/
if grep -q inventory /dev/shm/hf1-crm-res-list.out ; then 
  echo "`date` inventory resource found"
  if [ $doit -eq 0 ] ; then
    echo "Duplex non-primary node -> skipping inventory restart"
  else
    /usr/sbin/crm --wait resource restart rsc4k_inventory
    echo "`date` inventory restart finished with $?"
  fi
else
  echo "`date` inventory resource not found"
fi
echo "`/bin/date` ASYAPSPOST: OSFOURK-27502 done."

#OSFOURK-27491
[ -e /opt/yaps_data.bak ] && {

  #this script seems to be called also when STMIX boots for the first time in configure_cib_local by update daemon
  # we need to "repair" the version from RLC base to HF only during HF activation
  # this will happen only if our parent process is update daemon called with hfactivate
  ishfactivatecall=false
  myppid=$$
  while [ -n "$myppid" -a $myppid -ne 1 ]; do
    myppid=$(ps --no-headers -o ppid -p $myppid | tr -d [:space:])
    [[ "`cat /proc/$myppid/cmdline`" =~ hfactivate ]] && ishfactivatecall=true && break
  done
  if $ishfactivatecall ; then
      echo "`/bin/date` ASYAPSPOST: OSFOURK-27491 restoring YAPS version to the HF level from PLT HF"
      cp -v /opt/yaps_data.bak /opt/yaps/data
      cp -v /opt/yaps_data_stmix.bak /opt/yaps/data_stmix
      rm -f /opt/yaps_data.bak /opt/yaps_data_stmix.bak
      #force checkydata cache update
      checkydata -v
  else
      echo "`/bin/date` ASYAPSPOST: OSFOURK-27491 NOT restoring YAPS version because we are NOT called by hfactivate"
  fi
}

#OSFOURK-25987
/usr/sbin/system-is-stmix && {
  echo "`/bin/date` ASYAPSPOST: Will install ensure-stmix-boots.sh on STMIX ..."
  bash /opt/yaps/script/update_new/ensure-stmix-boots.sh install
}
#update ASyaps and plt-update-new.sh script inside OS Update set on the Host if no new RLC was transfered
updateOSupdateSet=1
#skip doing anything in case there is no OS Update set
if ls /instmedia/sg_os_rlc/plt-update-new.sh ; then
  #also skip updating the OS Update set in case there is a newer RLC transfered and the OS Update does not match the PLT HF
  curpltfrver=`getydata -v | cut -d. -f1-2`
  rpm -qip /instmedia/sg_os_rlc/hp4k_instkit*noarch.rpm | grep Version | grep $curpltfrver || updateOSupdateSet=0
  echo "`/bin/date` ASYAPSPOST: will NOT update the OS Update Kit with this PLT HF because a newer/different RLC has been transfered in the meanwhile"
else
  updateOSupdateSet=0
  echo "`/bin/date` ASYAPSPOST: no OS Update Kit found to be updated with this PLT HF"
fi
[ $updateOSupdateSet -eq 1 ] && {
#this needs to be forked as the PLT HF tar will appear in the current_hotfix folder only about 30 seconds later after this script is executed
  echo "`/bin/date` ASYAPSPOST: will update the OS Update Kit with the ASyaps and plt-update-new.sh from the PLT HF"
(
    exec 1>>/var/log/webservice/update-daemon-forked-osf-25987.log
    exec 2>&1
    #loop this for max 1h
    i=0
    while [ $i -lt 360 ] ; do
        i=$(($i + 1))
        sleep 10
        curhf=/var/opt/soco2/current_hotfix/PLT-`getydata -v`.tar
        date
        ls $curhf && {
            sleep 5
            echo "Found the PLT HF => will update the OS Update set with it ..."
            cp -v $(ls -1 /var/opt/soco2/current_hotfix_rpms/ASyaps*rpm) $(ls -1 /instmedia/sg_os_rlc/ASyaps*rpm)
            cp -v /opt/yaps/script/update_new/plt-update-new.sh /instmedia/sg_os_rlc/plt-update-new.sh
            #update checksums
            echo "updating /instmedia/sg_os_rlc/MD5SUMS ..."
            sed -i -e /ASyaps/d -e /plt-update-new/d /instmedia/sg_os_rlc/MD5SUMS
            md5sum $(ls -1 /instmedia/sg_os_rlc/ASyaps*rpm) >> /instmedia/sg_os_rlc/MD5SUMS
            md5sum /instmedia/sg_os_rlc/plt-update-new.sh >> /instmedia/sg_os_rlc/MD5SUMS
            echo "done."
            exit 0
        }
    done
    echo "ERROR : did not find PLT HF in /var/opt/soco2/current_hotfix folder. Listing it ..."
    ls -al /var/opt/soco2/current_hotfix
    exit 1
) &
}

#OSFOURK-27772 - PRB000077505 sshr-safe-SUID Privilege Escalation possible for specific sub-command (Plt V11 R0)
echo "`/bin/date` ASYAPSPOST: fixing OSFOURK-27772 ..."
rm -fv /bin/sshr-safe /opt/rcon/bin/sshr-safe /opt/CAP_Inside/share/rootfs_*/bin/sshr-safe /opt/CAP_Inside/share/rootfs_*/opt/rcon/bin/sshr-safe

#start squid by default
echo "`/bin/date` ASYAPSPOST: start squid by default if not already configured ..."
#OSFOURK-28999 Zoom Zappa - CL docker is continuously restarting after system RELOAD on duplex
if netcat -n -zv -w 2 127.0.0.1 3128 ; then
	ps -ef | grep squid
	echo "Squid is running => do not do anything"
else
if [ -e /opt/yapp/infoProxy.txt ] ; then
	echo "/opt/yapp/infoProxy.txt exists -> will assume it is OK. Leaving squid as it is."
else
	echo "/opt/yapp/infoProxy.txt does not exist -> will generate and start squid ..."
	dnsserver=$(2>/dev/null grep -m 1 ^nameserver /etc/resolv.conf | awk '{print $2}')
	echo "read following DNS : [$dnsserver]"
	echo "mode=0" >/opt/yapp/infoProxy.txt
	echo "dnsserver=$dnsserver" >>/opt/yapp/infoProxy.txt
	echo "address=" >>/opt/yapp/infoProxy.txt
	echo "port=" >>/opt/yapp/infoProxy.txt
	echo "user=" >>/opt/yapp/infoProxy.txt
	echo "password=" >>/opt/yapp/infoProxy.txt
	/opt/yaps/script/squid-config.sh test
	echo "squid-config.sh ret code : $?"
fi
fi

#OSFOURK-28851 - increased HD temperature warning threshold to 70'C
sed -i -e "s/threshold = 50/threshold = 70/" /etc/healthdaemon.ini

echo "`/bin/date` ASYAPSPOST: Postinstall actions finished"

exit 0
