#!/bin/sh
###############################################################################
# The postremove script cleans up any leftover links and files after removal
###############################################################################
# Copyright (c) 2022 Veritas Technologies LLC. All rights reserved
###############################################################################

INSTALL_PATH="/usr/openv/pdde"
OSTPLUGINS_PATH="/usr/openv/lib/ost-plugins"
TIMESTAMP=`date +%F_%H:%M`
LOGDIR="/var/log/puredisk"
if [ ! -d "${LOGDIR}" ] ; then
    mkdir -p ${LOGDIR}
fi
LOGFILE=${LOGDIR}/${TIMESTAMP}-pdde-postremove.log

echo | tee -a ${LOGFILE}
echo "Starting VRTSpddea postremove script." | tee -a ${LOGFILE}
echo| tee -a ${LOGFILE}
if [ -f /tmp/PDDE_SKIP_PACKAGE_SCRIPTS ] ; then
    echo "Skipping..."| tee -a ${LOGFILE}
else

# Remove group and world writable permissions
umask 0022

# Determine the platforms
os=`uname -s`
case "${os}" in
    Linux*) 
        PLATFORM="RedHat"
        if [ -f /etc/SuSE-release -o -f /sbin/SuSEconfig ]; then 
            PLATFORM="SuSE"
        elif [ -f /etc/os-release ] ; then
            # Both Red Hat 7 and SUSE 12 have an os-release file.
            grep -i SUSE /etc/os-release > /dev/null 2>&1
            if [ $? = 0 ] ; then
                PLATFORM="SuSE"
            fi
        elif [ -f /etc/debian_version ]; then 
            PLATFORM="Debian"
        fi      
    ;;      
    SunOS)  PLATFORM="Solaris"  ;;
    AIX)    PLATFORM="AIX"      ;;      
    HP-UX)  PLATFORM="HPUX"     ;;
esac

# Even though the rpm only runs the removing for the last package, not for upgrading,
# but still, skipping the removing all links for Linux OS just for safety. 

if [ $PLATFORM = "RedHat" ]; then
    if [ -f /etc/exports.d/vpfs-shares.exports ]; then
        rm -f /etc/exports.d/vpfs-shares.exports
    fi

    if [ -f /etc/sudoers.d/msdp ]; then
        echo "Removing msdp sudoers file from /etc/sudoers.d" | tee -a ${LOGFILE}
        rm -f /etc/sudoers.d/msdp
    fi

    if [ -f /etc/samba/vpfs-shares.conf ]; then
        sed -i '/include.*vpfs-shares.conf/d' /etc/samba/smb.conf
        systemctl reload smb
        rm -f /etc/samba/vpfs-shares.conf
    fi

    #files generated by ia byo
    grep model /etc/nbapp-release > /dev/null 2>&1
    if [ $? -ne 0 ]; then
        rm -f /etc/msdp-release /etc/nginx/locations/spws.conf /etc/nginx/keys/spws.cert /etc/nginx/keys/spws.key /etc/nginx/conf.d/byo.conf
    fi
    if [ -d ${INSTALL_PATH}/vpfs/tmp ]; then
        rm -f ${INSTALL_PATH}/vpfs/tmp/vpfs_config_history
        rm -f ${INSTALL_PATH}/vpfs/tmp/controller.conf
        rm -r ${INSTALL_PATH}/vpfs/tmp
   
    fi
fi

if [ ${os} != "Linux" ]; then

if [ -h /opt/pdag ]; then
    echo "Removing link /opt/pdag" | tee -a ${LOGFILE}
    rm -f /opt/pdag 
fi          
if [ -h /opt/pdshared ]; then
    echo "Removing link /opt/pdshared" | tee -a ${LOGFILE}
    rm -f /opt/pdshared
fi  
if [ -d /opt/pdde ]; then
    echo "Removing /opt/pdde directory." | tee -a ${LOGFILE}
    rm -rf /opt/pdde
fi
if [ -h ${OSTPLUGINS_PATH}/libstspipd.so ]; then
    echo "Removing link ${OSTPLUGINS_PATH}/libstspipd.so" | tee -a ${LOGFILE}
    rm -f ${OSTPLUGINS_PATH}/libstspipd.so
fi 
if [ -h ${OSTPLUGINS_PATH}/libstspipdMT.so ]; then
    echo "Removing link ${OSTPLUGINS_PATH}/libstspipdMT.so" | tee -a ${LOGFILE}
    rm -f ${OSTPLUGINS_PATH}/libstspipdMT.so
fi
if [ -h ${OSTPLUGINS_PATH}/pd.conf ]; then
    echo "Removing file ${OSTPLUGINS_PATH}/pd.conf" | tee -a ${LOGFILE}
    rm -f ${OSTPLUGINS_PATH}/pd.conf
fi 

if [ -d ${INSTALL_PATH} ]; then
    echo "Removing PDDE installation directory." | tee -a ${LOGFILE}
    rm -rf ${INSTALL_PATH}
fi

fi

fi 
echo | tee -a ${LOGFILE}
echo "VRTSpddea postremove script done!" | tee -a ${LOGFILE}
echo | tee -a ${LOGFILE}

