#!/bin/bash
################################################################################
#    HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
#
#    Licensed under the Apache License, Version 2.0 (the "License");
#    you may not use this file except in compliance with the License.
#    You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS,
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    See the License for the specific language governing permissions and
#    limitations under the License.
################################################################################

INSTALL_DIR=/opt/HPCCSystems
CONFIG_DIR=/etc/HPCCSystems
ENV_XML_FILE=environment.xml
ENV_CONF_FILE=environment.conf
PID_DIR=/var/run/HPCCSystems
LOCK_DIR=/var/lock/HPCCSystems
LOG_DIR=/var/log/HPCCSystems
INIT_PATH=/etc/init.d


source ${INSTALL_DIR}/etc/init.d/hpcc_common
source ${INSTALL_DIR}/etc/init.d/init-functions

removeSymlink ()
{
    toDelete=$1
    
    rm -f ${toDelete} 2> /dev/null

    printf "Removing Symlink %-44s ..." "${toDelete}"
    if [ -L ${toDelete} ]; then
        log_failure_msg
    elif [ -e ${toDelete} ] && [ ! -L ${toDelete} ] ; then
        log_failure_msg
    else
        log_success_msg
    fi
}

if [ -e /etc/systemd/system/hpccsystems-platform.target ]; then
  removeSymlink "/etc/systemd/system/hpccsystems-platform.target"
fi

echo ""

## uninstalling files
removeSymlink "/etc/init.d/dafilesrv"
removeSymlink "/usr/bin/dfuplus"
removeSymlink "/usr/bin/ecl"
removeSymlink "/usr/bin/eclcc"
removeSymlink "/usr/bin/eclplus"
removeSymlink "/usr/bin/soapplus"
removeSymlink "/usr/bin/testsocket"
removeSymlink "/usr/bin/wuget"

if [ -d ${INSTALL_DIR}/etc/bash_completion.d ] && [ -d /etc/bash_completion.d ]; then
    for subInstall in $(ls ${INSTALL_DIR}/etc/bash_completion.d | grep -v dpkg-tmp); do
        removeSymlink /etc/bash_completion.d/$subInstall
    done
fi

# locate sub uninstall files.
if [ -d ${INSTALL_DIR}/etc/init.d/uninstall ]; then
    for subUnInstall in $(ls ${INSTALL_DIR}/etc/init.d/uninstall); do
        source ${INSTALL_DIR}/etc/init.d/uninstall/${subUnInstall}
    done
fi

if which systemctl 1>/dev/null 2>&1; then
    systemctl daemon-reload
	systemctl reset-failed
fi
