#!/usr/bin/env bash
#
# onx-fail2ban-filter-list — /etc/fail2ban/filter.d/ içindeki tüm .conf dosyalarını listele.
# Output: {"ok":true,"filters":["sshd","apache-auth",...]}

set -euo pipefail

readonly FILTER_DIR="/etc/fail2ban/filter.d"

if [[ ! -d "$FILTER_DIR" ]]; then
    jq -nc --arg dir "$FILTER_DIR" '{ok:false,error:"filter directory not found",path:$dir}' >&2
    exit 2
fi

# List all .conf files, strip path + extension
filters_json="$(find "$FILTER_DIR" -maxdepth 1 -name '*.conf' -type f -printf '%f\n' 2>/dev/null \
    | sed 's/\.conf$//' \
    | sort -u \
    | jq -R -s 'split("\n") | map(select(length > 0))')"

[[ -z "$filters_json" ]] && filters_json='[]'

count=$(echo "$filters_json" | jq 'length')

jq -nc --argjson filters "$filters_json" --argjson count "$count" \
    '{ok:true,filters:$filters,count:$count}'
