#! /bin/bash
#
# Removes the ufw rule(s) for the given mDNS/SLP port(s). mDNS and SLP are
# independent settings; only ports that HPLIP itself added at install time
# should be passed in (see base/services.py's run_close_mdns_port()/
# run_close_slp_port(), which track this in /var/lib/hp/hplip.state so a
# pre-existing user rule for the other protocol is never touched).
#
# Usage: close-ufw-firewall <5353|427>
# No-op if ufw is absent, inactive, or no ports are passed.

if ! command -v ufw >/dev/null 2>&1; then
    exit 0
fi

ufw status | grep -q "^Status: active"
if [ $? -ne 0 ]; then
    exit 0
fi

for port in "$@"; do
    case "$port" in
        5353)
            ufw --force delete allow proto udp from any to 224.0.0.251 port 5353 >/dev/null 2>&1
            ;;
        427)
            ufw --force delete allow proto udp from any to 224.0.1.60 port 427 >/dev/null 2>&1
            ;;
    esac
done

exit 0
