#!/bin/bash
# This is a script which will be launched by the software-packages plugin
# after the RPM install transaction is complete and all other patching logic
# has been applied. This actually is the last script which will be executed
# before software-packages complete its update execution.

# Update init scripts with service dependenices.
/bin/update-svc-deps

# Reload the firewall in case there were services removed in patch phase
/usr/lib/applmgmt/networking/bin/firewall-reload

# Fix applmgmt endpoint
if [ -f /usr/lib/applmgmt/support/scripts/fix-endpoint.py ]; then
  /bin/python /usr/lib/applmgmt/support/scripts/fix-endpoint.py
fi

# PR 2005144 Post B2BUpgrade if systemd rpm gets updated
# all config changes are wiped off. Preserving these workaround after update
#TODO remove this workaround after moving to photon2
echo "Changing watchdog timer for journald service to 10min"
sed -i.bak s/#SyncIntervalSec=5m/SyncIntervalSec=10s/g \
/etc/systemd/journald.conf
sed -i.bak s/WatchdogSec=3min/WatchdogSec=10min/g \
/usr/lib/systemd/system/systemd-journald.service
sed -i.bak s/WatchdogSec=3min/WatchdogSec=10min/g \
/usr/lib/systemd/system/systemd-udevd.service
sed -i.bak s/WatchdogSec=3min/WatchdogSec=10min/g \
/usr/lib/systemd/system/systemd-logind.service
/usr/bin/systemctl restart systemd-journald
/usr/bin/systemctl restart systemd-udevd
/usr/bin/systemctl restart systemd-logind

#disable cloud-int service PR1982599
systemctl stop cloud-init-local.service && systemctl disable cloud-init-local.service
systemctl stop cloud-init.service && systemctl disable cloud-init.service
systemctl stop cloud-config.service && systemctl disable cloud-config.service
systemctl stop cloud-final.service && systemctl disable cloud-final.service

#PR 2035598 systemd rpm update 99-dhcp-en.network config file on system
#which has been set up with static IP.
dhcp_network_file="/etc/systemd/network/99-dhcp-en.network"

if [ -f $dhcp_network_file ] ; then
    /usr/bin/rm $dhcp_network_file
fi

# 1. Find if there is ntp rpm in the patch and if we install it
# 2. Find if ntpd is started. It is started/stopped by applmgmt.
#    If it is not started then we must not start it.
# 3. Restart ntpd
grep -q 'relativepath.*ntp.*rpm' \
/storage/core/software-packages/staged-configuration.json \
&& pgrep ntpd \
&& echo 'restarting ntpd' \
&& systemctl restart ntpd

true

# PR 2114031 Refresh DCUI to reflect correct version post-update 
sh /usr/lib/applmgmt/dcui/notify

# PR 2118423 Reset banner as vsphere-ui service couldnt download resource budnles
echo "Reset Banner"
/usr/bin/banner_ctrl reset

# PR 2108307 Fix update conf and patch-history
script_name=$0
script_full_path=$(dirname "$0")
dnsmasq_path="$script_full_path/configureDnsCaching.py"
python $dnsmasq_path
full_path="$script_full_path/fix_update_conf.py"
python $full_path

# PR 2173605, update sendmail.cf post patch
# Get the deployment type or assume embedded as default
deploymentType=$(cat /etc/vmware/deployment.node.type)
if [ $deploymentType == 'embedded' -o $deploymentType == 'management' ]
then
    /usr/lib/vmware-vpx/config_sendmail.sh
fi

# PR 2185694 Retain NTLM settings from Likewise as it is used by CAM for authentication
if [ -f /etc/likewise/gss/mech ]; then
  if [ `grep -c -e 'ntlm' /etc/likewise/gss/mech` -eq 0 ]; then
  echo "NTLM configurations are not present, updating it in /etc/likewise/gss/mech"
  sed -i '1 i\ntlm 1.3.6.1.4.1.311.2.2.10 libgssapi_ntlm.so' /etc/likewise/gss/mech
  fi
fi

#PR 2343936: Remove/uninstall openJRE,apache-tomcat
#and apache-ant in existing deployed VC, as we are no longer using it.
rpmRemoveList=("apache-tomcat" "apache-ant" "openjre")
for rpm in "${rpmRemoveList[@]}"
do
    if [ ! -z `rpm -qa | grep $rpm` ];then
        rpm -qa | grep "$rpm" | xargs rpm -e --nodeps > /dev/null 2>&1
        if [ "$?" -ne "0" ];then
            echo "Error in uninstalling $rpm"
            break
        else
            echo "Uninstalled $rpm"
        fi
    fi
done

# Restart DDNS
/usr/bin/python /usr/lib/applmgmt/ddns/py/ddns.py --disable
echo "$(date): Disabled DDNS"
/usr/bin/python /usr/lib/applmgmt/ddns/py/ddns.py --enable
echo "$(date): Enabled DDNS"

#Exit the patching and remove the pycrypto packaged
function exitPatchingAndCleanup() {
    if [ -d /usr/lib/applmgmt/pycrypto ]
    then
        rm -rf /usr/lib/applmgmt/pycrypto
    fi
    exit 1
}

# Function to Downgrade python3-pycrypto to stable version
function checkAndInstallStablePycrypto() {
    if rpm -qa | grep python3-pycrypto-2.7a1-3.ph1.x86_64 > /dev/null 2>&1
    then
        echo "Found python3-pycrypto-2.7a1-3.ph1.x86_64!"
        if ls /usr/lib/applmgmt/pycrypto/python3-pycrypto* > /dev/null 2>&1
        then
	    echo "Found the stable python3-pycrypto file!"
	    echo "Un-installing python3-pycrypto-2.7a1-3.ph1.x86_64"
	    rpm -e python3-pycrypto-2.7a1-3.ph1.x86_64 --nodeps > /dev/null 2>&1
	    if [ "$?" -ne "0" ]
	    then
	        echo "Error in Un-installing python3-pycrypto!"
	        exitPatchingAndCleanup
	    else
	        echo "Installing stable python3-pycrypto"
	        rpm -Uvh /usr/lib/applmgmt/pycrypto/python3-pycrypto* --force > /dev/null 2>&1
	        if [ "$?" -ne "0" ]
	        then
		    echo "Error in Installing python3-pycrypto!"
	            exitPatchingAndCleanup
	        else
		    echo "python3-pycrypto RPM installed successfully!"
                fi
	    fi
        fi
    fi
    rm -rf /usr/lib/applmgmt/pycrypto
}

# Check if unstable version of pycrypto is installed in VC
# if its found then downgrade it to the stable version.
checkAndInstallStablePycrypto
