#! /bin/bash
# (c) by ennit interactive GmbH, 2023
# Author ppatzelt@ennit.com

# Include defaults if available
if [ -f /etc/default/hosting-tools ]; then
    . /etc/default/hosting-tools
fi

TYPE="fail2ban"

if [[ $# -eq 0 ]] ; then
    echo "  Missing argument. Basename of new symlink is missing. Aborting."
    exit 1
fi

BASENAME=`basename $1`
SYMLINK_DIR="/etc/$TYPE/filter.d"

find -L ${SYMLINK_DIR} -type l -exec rm -f {} +

if [ ! -f "${SYMLINK_DIR}/${BASENAME}" ]; then
    echo "  Symlink target does not exist. Aborting."
    exit 1
fi

echo "  removing ${SYMLINK_DIR}/${BASENAME}"
rm -f ${SYMLINK_DIR}/${BASENAME} 2> /dev/null

if [ $? -eq 0 ]; then
    exit 0
else
    echo "  Could not remove symlink."
    exit 1
fi