#!/bin/bash

while getopts d:i:o: flag
do
    case "${flag}" in
        d) directory="${OPTARG}";;
        i) input_file="${OPTARG}";;
        o) output_file="${OPTARG}";;
        *)  # Catch-all for unexpected arguments
            echo "Invalid option: -$OPTARG" >&2
            echo "Usage: ./compress.sh -d <audio_folder> -i <input_file> -o <output_file>"
            exit 1
            ;;
    esac
done

# Check if all required arguments are provided
if [ -z "$directory" ] || [ -z "$input_file" ] || [ -z "$output_file" ]; then
    echo "All arguments are required."
    echo "Usage: ./compress.sh -d <audio_folder> -i <input_file> -o <output_file>"
    exit 1
fi

cd "$directory" || exit 1

# Compress the file
/usr/bin/ffmpeg -i "$input_file" -ar 16000 -ac 1 -map 0:a "$output_file"

# Ensure the file is fully written to disk
sync "$output_file"

WAIT_TIME=5
MAX_ATTEMPTS=12  # 1 minute total wait time

# Function to check for file existence and stability
check_file() {
  if [ -f "$output_file" ]; then
    initial_size=$(stat -c %s "$output_file")
    sleep 2
    current_size=$(stat -c %s "$output_file")
    if [ "$initial_size" -eq "$current_size" ]; then
      echo "File $output_file is available and stable."
      return 0
    else
      echo "File $output_file is still being written. Waiting..."
      return 1
    fi
  else
    echo "File $output_file does not exist. Waiting..."
    return 1
  fi
}

# Wait for the file to become available and stable
attempt=0
while ! check_file; do
  sleep $WAIT_TIME
  attempt=$((attempt + 1))
  if [ $attempt -ge $MAX_ATTEMPTS ]; then
    echo "Max attempts reached. File may not be fully written."
    exit 1
  fi
done

# Final check using lsof
if lsof "$output_file" > /dev/null 2>&1; then
  echo "Warning: File $output_file is still open by another process."
  exit 1
else
  echo "File $output_file is fully written and closed."
  exit 0
fi