#!/bin/bash
# freeslapmac-silent — prove the silent build really is silent.
#
# For every MP3 in the destination bundle this decodes the file with macOS's own
# AudioToolbox (afconvert) — the same decoder `spank` uses to play it — and asserts the
# PCM payload contains no non-zero byte at all, and that its duration still matches the
# original clip. Also checks the signature, the bundle id and the executable.
#
# Usage: bash verify.sh [SOURCE_APP] [DEST_APP]

set -uo pipefail

SRC="${1:-/Applications/FreeSlapMac.app}"
DEST="${2:-/Applications/FreeSlapMac Silent.app}"
SOUNDS_REL="Contents/Resources/Sounds"
fail=0
say() { printf '%s\n' "$*"; }
bad() { printf '  ✗ %s\n' "$*"; fail=$((fail + 1)); }

[ -d "$SRC" ] || { echo "no source app: $SRC" >&2; exit 2; }
[ -d "$DEST" ] || { echo "no destination app: $DEST" >&2; exit 2; }

TMP="$(mktemp -d)"; trap 'rm -rf "$TMP"' EXIT

dur() { afinfo "$1" 2>/dev/null | awk -F': ' '/estimated duration/ {print $2; exit}' | awk '{print $1}'; }

say "== files =="
srcfiles=(); while IFS= read -r l; do srcfiles+=("$l"); done < <(find "$SRC/$SOUNDS_REL" -name '*.mp3' | sort)
dstfiles=(); while IFS= read -r l; do dstfiles+=("$l"); done < <(find "$DEST/$SOUNDS_REL" -name '*.mp3' | sort)
say "  original: ${#srcfiles[@]} mp3   silent: ${#dstfiles[@]} mp3"
[ "${#srcfiles[@]}" -eq "${#dstfiles[@]}" ] && [ "${#dstfiles[@]}" -gt 0 ] || bad "file count differs"

say "== decode check (AudioToolbox) =="
n=0 silent=0 loud=0 durdrift=0
for f in "${dstfiles[@]}"; do
  n=$((n + 1))
  wav="$TMP/x.wav"
  if ! afconvert -f WAVE -d LEI16 "$f" "$wav" 2>/dev/null; then bad "cannot decode $f"; continue; fi
  off="$(LC_ALL=C grep -abo 'data' "$wav" | head -1 | cut -d: -f1)"
  [ -n "$off" ] || { bad "no data chunk in $f"; continue; }
  nz="$(tail -c +$((off + 9)) "$wav" | LC_ALL=C tr -d '\000' | wc -c | tr -d ' ')"
  if [ "$nz" = "0" ]; then silent=$((silent + 1)); else loud=$((loud + 1)); bad "audible content in $f ($nz non-zero bytes)"; fi

  rel="${f#"$DEST"/}"
  if [ -f "$SRC/$rel" ]; then
    d1="$(dur "$f")"; d2="$(dur "$SRC/$rel")"
    awk -v a="$d1" -v b="$d2" 'BEGIN { exit (a - b < 0.10 && b - a < 0.10) ? 0 : 1 }' || { durdrift=$((durdrift + 1)); bad "duration drift $rel: $d2 s -> $d1 s"; }
  fi
  rm -f "$wav"
done
say "  decoded: $n   digitally silent: $silent   audible: $loud   duration mismatches: $durdrift"

say "== signature / identity =="
codesign --verify --deep --strict "$DEST" >/dev/null 2>&1 && say "  signature: valid (ad-hoc)" || bad "signature invalid"
src_id="$(/usr/libexec/PlistBuddy -c 'Print :CFBundleIdentifier' "$SRC/Contents/Info.plist")"
dst_id="$(/usr/libexec/PlistBuddy -c 'Print :CFBundleIdentifier' "$DEST/Contents/Info.plist")"
[ "$src_id" = "$dst_id" ] && say "  bundle id: $dst_id (settings carry over)" || bad "bundle id changed: $src_id -> $dst_id"
dst_name="$(/usr/libexec/PlistBuddy -c 'Print :CFBundleName' "$DEST/Contents/Info.plist")"
say "  display name: $dst_name"

cp "$SRC/Contents/MacOS/$(/usr/libexec/PlistBuddy -c 'Print :CFBundleExecutable' "$SRC/Contents/Info.plist")" "$TMP/a.bin" 2>/dev/null
cp "$DEST/Contents/MacOS/$(/usr/libexec/PlistBuddy -c 'Print :CFBundleExecutable' "$DEST/Contents/Info.plist")" "$TMP/b.bin" 2>/dev/null
codesign --remove-signature "$TMP/a.bin" 2>/dev/null; codesign --remove-signature "$TMP/b.bin" 2>/dev/null
if cmp -s "$TMP/a.bin" "$TMP/b.bin"; then
  say "  executable: identical code ($(shasum -a 256 "$TMP/b.bin" | cut -c1-16)…)"
else
  bad "executable differs from the original"
fi

say "== size =="
say "  original $(du -sh "$SRC" | cut -f1)   silent $(du -sh "$DEST" | cut -f1)"

if [ "$fail" -eq 0 ]; then
  say "PASS — every clip decodes to digital silence, signature valid, same code and settings."
  exit 0
fi
say "FAIL — $fail problem(s) above."
exit 1
