########
#
# $Copyright: Copyright (c) 2022 Veritas Technologies LLC. All rights reserved $
# Common postremove script.
# @(#) $Revision$

#
########

OS=`uname`
if [ "$OS" = "SunOS" ]
then
   LOG_DIR=$PKG_INSTALL_ROOT/var/log/VRTSpbx
else
   LOG_DIR=/var/log/VRTSpbx
fi
LOGFILE=$LOG_DIR/vrtspbx_install.log

# Helper function
rmEmptyDir()
{
    # If dir exists
    if [ -d $1 ] ; then
        if [ "$2" = "" ] ; then
            # and only contains "." and ".."
            if [ `/bin/ls -a $1 | wc -l` -eq 2 ] ; then
                # Then remove
                /bin/rmdir $1
            else
                echo "Not removing directory $1 - it is not empty." >> $LOGFILE
            fi
        else
            if [ "$2" = "checkChildDir" ] ; then 
                # If child directories to be checked, then remove child directories if empty and then remove parent directory
                find $1 -depth -type d | xargs /bin/rmdir {} \; 2>/dev/null
                # If dir exists 
                if [ -d $1 ] ; then
                    echo "Not Removing directory $1 - it is not empty." >> $LOGFILE
                fi
            fi 	
        fi
    fi
}


echo "******************" >> $LOGFILE
echo "* postremove logs *" >> $LOGFILE
echo "******************" >> $LOGFILE

date >> $LOGFILE

# clean the leftovers.
REMOVE_ROOT=""
if [ "$OS" = "SunOS" ]; then
	REMOVE_ROOT=$PKG_INSTALL_ROOT
fi
PBX_BASE_DIR=$REMOVE_ROOT/opt/VRTSpbx
rmEmptyDir $PBX_BASE_DIR/bin/cluster
rmEmptyDir $PBX_BASE_DIR/bin
rmEmptyDir $PBX_BASE_DIR/lib
rmEmptyDir $PBX_BASE_DIR/man/man1
rmEmptyDir $PBX_BASE_DIR/man
rmEmptyDir $PBX_BASE_DIR/resources/VxPBX
rmEmptyDir $PBX_BASE_DIR/resources/vxul
rmEmptyDir $PBX_BASE_DIR/resources
rmEmptyDir $PBX_BASE_DIR
rmEmptyDir $LOG_DIR
rmEmptyDir $REMOVE_ROOT/etc/vx/VxICS
rmEmptyDir /var/VRTSpbx checkChildDir

rm -f /etc/rc.d/init.d/vxpbx_exchanged

date >> $LOGFILE 
# End of file #
