#!/bin/bash

# Change to the desired directory
directory="/sshfsmounts/20tb-1-raid1c3/personal/Documents/studies/pianostudy/sheets"

if [ -d "$directory" ]; then
    cd "$directory" || exit
else
    cd "/home/blu/Documents/studies/pianostudy/sheets" || exit
fi

while true; do
  # Use FZF to select MIDI files
  echo "Searching for MIDI files..."
  midi_file="$(fd -e mid -e MID | sort | fzf --no-sort)"
  
  if [ -z "$midi_file" ]; then
    echo "No file selected. Exiting the loop."
    break  # Exit the loop if no file is selected in FZF
  elif [ ! -f "$midi_file" ]; then
    echo "Invalid file path: $midi_file"
  else
    # Get the absolute path using realpath
    absolute_path=$(realpath "$midi_file")
    
    echo "Launching Pianoteq with file: $absolute_path"
    
    # Launch Pianoteq in the background and play the selected MIDI file
    pianoteq8 --fullscreen --midi "$absolute_path" --play-and-quit >/dev/null 2>&1 &
  fi
done
