#!/bin/sh
# Wrapper invoked by ~/Library/LaunchAgents/com.lilith.disk-reclaim.plist
# on user login. Appends a timestamped disk-reclaim snapshot to the log.
#
# Not meant for direct human use — invoke `disk-reclaim` instead.

set -eu

log="$HOME/Library/Logs/disk-reclaim.log"
mkdir -p "$(dirname "$log")"

# Absolute path so we don't depend on $PATH in launchd's minimal env.
script_dir=$(cd "$(dirname "$0")" && pwd -P)
reclaim="$script_dir/disk-reclaim"

{
    echo
    echo "=== $(date '+%Y-%m-%d %H:%M:%S %z') (boot) ==="
    "$reclaim" "$HOME" --min 1G
} >> "$log" 2>&1

# Trim to last ~200KB so it can't grow without bound across years of boots.
if [ -f "$log" ] && [ "$(wc -c <"$log")" -gt 204800 ]; then
    tail -c 204800 "$log" > "$log.trim" && mv "$log.trim" "$log"
fi
