#!/bin/bash

# Paths to Python scripts
SUBTITLE_SCRIPT="$HOME/.bin/auto_embed_subtitles.py"

# Check both Python scripts exist
if [ ! -f "$SUBTITLE_SCRIPT" ]; then
    echo "Error: Subtitle script '$SUBTITLE_SCRIPT' not found."
    exit 1
fi

# === Mode: Manual Audio Embed ===
if [ "$#" -eq 3 ]; then
    echo "Embedding subtitle: $1 + $2 -> $3"
    python "$SUBTITLE_SCRIPT" "$1" "$2" "$3"
    exit 0

# === Mode: Auto Detection ===
elif [ "$#" -eq 0 ]; then
    echo "No arguments provided. Attempting to embed audio and subtitles in the current directory."

    files_processed=0

    # Process audio embeddings
    for video_file in *.mp4 *.mkv *.avi *.mov; do
        [ -e "$video_file" ] || continue
        base_name="${video_file%.*}"
        audio_file="${base_name}.mp3"

        if [ -f "$audio_file" ]; then
            output_file="${base_name}_with_audio.mp4"
            echo "Embedding subtitle: $video_file + $audio_file -> $output_file"
            python "$SUBTITLE_SCRIPT" "$video_file" "$audio_file" "$output_file"
            files_processed=1
        fi
    done

    # Process subtitle embeddings
    echo "Scanning for subtitles to embed..."
    python "$SUBTITLE_SCRIPT" "."

    exit 0

else
    echo "Usage:"
    echo "  $0 <video_file> <subtitle_file> <output_file>  # Manual subtitel embed"
    echo "  $0                                          # Auto mode (pair audio + embed subtitles)"
    exit 1
fi
