#!/bin/bash

# Set the working 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 MSCZ files
  echo "Searching for MSCZ files..."
  mscz_file="$(fd . -t f -e mscz | fzf)"
  
  if [ -z "$mscz_file" ]; then
    echo "No file selected. Exiting the loop."
    break  # Exit the loop if no file is selected in FZF
  elif [ ! -f "$mscz_file" ]; then
    echo "Invalid file path: $mscz_file"
  else
    # Get the absolute path using realpath
    absolute_path=$(realpath "$mscz_file")
    
    echo "Launching MuseScore with file: $absolute_path"
    
    # Launch MuseScore in the background to open the selected file
    mscore "$absolute_path" >/dev/null 2>&1 &
  fi
done
