#!/usr/bin/python3 -I
"""osascript shim: a Finder delete goes to the Trash silently; everything else runs unchanged.

11-12 (owner: "Finder still making the noise"). Finder deletes play the trash sound once per item.

Agents (Codex jobs especially) kept writing
`osascript -e 'tell application "Finder" to delete POSIX file "..."'` despite a written rule, so
this sits first on PATH (~/.local/bin; tools/launch.sh refuses a job where it would not be first) and turns a Finder delete into /usr/bin/trash: same Trash,
same Put Back, no sound. Anything else, or a delete whose paths it cannot read, goes to the real
/usr/bin/osascript unchanged.
"""
import os, re, subprocess, sys

REAL = os.environ.get('OSASCRIPT_REAL', '/usr/bin/osascript')
TRASH = os.environ.get('OSASCRIPT_TRASH', '/usr/bin/trash')
args = sys.argv[1:]

scripts, rest, i = [], [], 0
while i < len(args):
    a = args[i]
    if a == '-e' and i + 1 < len(args):
        scripts.append(args[i + 1]); i += 2; continue
    if a.startswith('-') and not scripts:
        os.execv(REAL, [REAL] + args)  # flags we don't model: pass through
    rest.append(a); i += 1

text = '\n'.join(scripts)
if not (re.search(r'application\s+"Finder"', text) and re.search(r'\bdelete\b', text)):
    os.execv(REAL, [REAL] + args)

paths = re.findall(r'POSIX file\s+"((?:[^"\\]|\\.)*)"', text)
paths = [p.replace('\\"', '"').replace('\\\\', '\\') for p in paths]
if 'argv' in text:
    paths += rest
paths = [p for p in paths if os.path.lexists(p)]
if not paths:
    os.execv(REAL, [REAL] + args)

# Check before both Trash and Finder fallbacks. This is also the cleanup path
# used by agent Finder deletes; malformed/missing registered sources refuse.
sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)))
try:
    from protected_paths import assert_disposable
    assert_disposable(paths)
except Exception as error:
    print('Trash refused: ' + str(error), file=sys.stderr)
    sys.exit(1)

# iCloud Drive files must go through Finder, so they land in iCloud's Recently Deleted; /usr/bin/trash cannot
# move them (11-14: the KB backup's prune of old iCloud bundles failed every day after this shim went in).
# A trash that fails for any other reason also falls back to the real osascript: a sound beats a skipped delete.
ICLOUD = os.path.expanduser('~/Library/Mobile Documents') + os.sep
if any(os.path.abspath(p).startswith(ICLOUD) for p in paths):
    os.execv(REAL, [REAL] + args)
r = subprocess.run([TRASH] + paths)
if r.returncode:
    os.execv(REAL, [REAL] + args)
sys.exit(0)
