#!/bin/bash
# run.sh <arm plain|skill> <task> <rep> -> runs/<arm>-<task>-<rep>/ ; haiku, Bash allowed, web off, project settings only
set -u
HERE=$(cd "$(dirname "$0")" && pwd)
arm=$1; task=$2; rep=$3; d=$HERE/runs/$arm-$task-$rep
[ -s "$d/out.json" ] && exit 0
rm -rf "$d"; mkdir -p "$d/.claude"
cp "$HERE"/media/* "$d/"
cat > "$d/.claude/settings.json" <<J
{"permissions":{"deny":["WebFetch","WebSearch"]},"autoMemoryEnabled":false}
J
case $task in
 cut)  R="Cut src.mp4 from 0:03 to 0:08 and save it as out.mp4. Keep src.mp4 unchanged.";;
 vertical) R="Make a vertical 9:16 version of src.mp4 at 1080x1920 that fills the frame (crop, no black bars). Save it as out.mp4.";;
 captions) R="Burn the captions in subs.srt into src.mp4 and save the result as out.mp4.";;
 loudness) R="quiet.mp4 is too quiet. Normalize its audio to -14 LUFS integrated for YouTube and save it as out.mp4.";;
 join) R="Join a.mp4 and then b.mp4 with a 0.5 second crossfade between them, picture and sound. Save it as out.mp4.";;
 silence) R="talk.mp4 has silent pauses. Cut out the pauses and save the tightened video as out.mp4.";;
esac
if [ "$arm" = skill ]; then mkdir -p "$d/.claude/skills" && cp -R "$HERE/skillcopy/ffmpeg-skill" "$d/.claude/skills/"; P="/ffmpeg-skill $R"; else P="$R"; fi
cd "$d"; t0=$(date +%s)
PATH=/opt/homebrew/bin:/usr/local/bin:$PATH claude -p "$P" --model haiku --setting-sources project \
  --permission-mode bypassPermissions --output-format json < /dev/null > "$d/out.json" 2> "$d/err.log"
echo "{\"secs\": $(( $(date +%s) - t0 ))}" > "$d/time.json"
