#!/bin/bash
# Rspamd Learning Script for Dovecot Maildir
# Supports global defaults and per-user Sieve-based configuration.

VMAIL_DIR="/home/vmail"
SINCE=""

# 1. Load Global Defaults
[ -f /etc/default/sun-mail-config ] && . /etc/default/sun-mail-config
HAM_FOLDERS=${HAM_FOLDERS:-".Sent"}
SPAM_FOLDERS=${SPAM_FOLDERS:-".Junk"}

while [[ $# -gt 0 ]]; do
  case $1 in
    --since)
      SINCE="$2"
      shift 2
      ;;
    *)
      echo "Unknown option: $1"
      exit 1
      ;;
  esac
done

learn_folder() {
  local type=$1
  local folder_path=$2
  local find_opts=""

  if [ -n "$SINCE" ]; then
    mmin=$(echo $SINCE | sed 's/h/*60/' | bc)
    find_opts="-mmin -$mmin"
  fi

  # Count files first for the log summary
  file_count=$(find "$folder_path" -type f $find_opts | wc -l)
  
  if [ "$file_count" -gt 0 ]; then
    echo "  🔍 Learning $type from '$folder_path' ($file_count files)..."
    for sub in cur new; do
      if [ -d "$folder_path/$sub" ]; then
        # We pipe to tail to avoid flooding the journal with one line per mail,
        # but we keep the last result to see if it worked.
        find "$folder_path/$sub" -type f $find_opts -exec rspamc learn_$type {} + | tail -n 1
      fi
    done
  else
    echo "  skipping $type in '$folder_path' (no new mail)"
  fi
}

echo "🚀 Starting Rspamd learning cycle..."

# Iterate over each mailbox (domain/user)
find "$VMAIL_DIR" -maxdepth 2 -mindepth 2 -type d | while read user_dir; do
  echo "📂 Mailbox: $user_dir"

  USER_HAM="$HAM_FOLDERS"
  USER_SPAM="$SPAM_FOLDERS"

  # 2. Check for Sieve-based configuration
  SIEVE_CONF="$user_dir/sieve/learn.sieve"
  if [ -f "$SIEVE_CONF" ]; then
    # Extract ham folders
    EXTRACTED_HAM=$(grep -i "# learn-ham:" "$SIEVE_CONF" | cut -d: -f2)
    [ -n "$EXTRACTED_HAM" ] && USER_HAM="$EXTRACTED_HAM"

    # Extract spam folders
    EXTRACTED_SPAM=$(grep -i "# learn-spam:" "$SIEVE_CONF" | cut -d: -f2)
    [ -n "$EXTRACTED_SPAM" ] && USER_SPAM="$EXTRACTED_SPAM"
  fi

  # Process HAM
  for ham in $USER_HAM; do
    [ -d "$user_dir/$ham" ] && learn_folder "ham" "$user_dir/$ham"
  done

  # Process SPAM
  for spam in $USER_SPAM; do
    [ -d "$user_dir/$spam" ] && learn_folder "spam" "$user_dir/$spam"
  done
done

echo "✅ Learning cycle complete."
