#!/bin/bash
# NoCloudPDF — strip macOS source-tracking metadata from files.
#
# What it removes:
#   com.apple.metadata:kMDItemWhereFroms  (the URL the file came from)
#   com.apple.quarantine                  (downloaded-from-internet flag)
#
# Setup (one-time): browsers don't preserve executable bit on download, so:
#   chmod +x ~/Downloads/strip-meta.command
# After that: drag PDFs onto the icon, or double-click for interactive prompt.
#
# Note: for a single file, you don't need this script at all — just run
#   xattr -c yourfile.pdf
# directly in Terminal. This script's value is the drag-drop UX for repeated use.

set -e

strip_one() {
  local f="$1"
  if [ ! -f "$f" ]; then
    echo "  ✗ $(basename "$f") — not a file"
    return 1
  fi
  xattr -c "$f" 2>/dev/null
  echo "  ✓ $(basename "$f")"
}

if [ $# -gt 0 ]; then
  # CLI mode
  echo "Stripping metadata from $# file(s)..."
  ok=0; fail=0
  for f in "$@"; do
    if strip_one "$f"; then ok=$((ok+1)); else fail=$((fail+1)); fi
  done
  echo ""
  echo "Done: $ok cleaned, $fail skipped."
  exit 0
fi

# Interactive mode (double-click)
clear
cat <<'BANNER'
NoCloudPDF — Strip Source Metadata

Drag files from Finder into this window, then press Enter.
Press Enter on an empty line to quit.

BANNER

read -e -p "→ " input

if [ -z "$input" ]; then
  exit 0
fi

# Files dragged into Terminal arrive space-separated with backslash-escaped spaces.
# eval is safe here because we only ever run `xattr -c` against the parsed paths.
eval "arr=($input)"

ok=0; fail=0
for f in "${arr[@]}"; do
  if strip_one "$f"; then ok=$((ok+1)); else fail=$((fail+1)); fi
done

echo ""
echo "Done: $ok cleaned, $fail skipped."
echo ""
echo "Press Enter to close."
read -r
