#!/bin/bash
#
# Date    2020-11-25
# Updated 2025-05-29
#
# YOSHPATH="/usr/bin"
#
# Starts Yoshimi with our oft-used GM patch setup for Yoshimi.
# Adjust the variables below for your setup.

YOSHPATH="/usr/bin"
YOSHIMI="yoshimi"
OPTIONS=""
REPOPATH="Home/ca/mls/git"
CFGPATH=""
DEFCFGPATH="$HOME/$REPOPATH/yoshimi-cookbook/sequencer64/b4uacuse"
DOSTATE="no"
USECODEC="no"
STATEFILE="yoshimi-b4uacuse-gm.state"
STATEARG=""

# USELATEST="no"

if [ "$1" == "--help" ] || [ "$1" == "help" ] ; then

   cat << E_O_F
Usage v. 2025-05-29

   ystart [ options ] Start the Yoshimi software synthesizer.

Options:

   --alsa           Use ALSA for both MIDI and audio.
   --jack           Use JACK for both MIDI and audio.
   --ja             Use JACK for MIDI and ALSA for audio.
   --codec          Use ALSA for both, but use the CODEC device.
   --cfgpath dir    Set the Yoshimi configuration directory.
   --b4             Set the configuration directory to:
        $DEFCFGPATH
                    and use the state file $STATEFILE.

Default command:

    $YOSHPATH/$YOSHIMI $OPTIONS
    
E_O_F

exit 0

else

   while [ "$1" != "" ] ; do

      case "$1" in

         --alsa)
            OPTIONS="--alsa-midi --alsa-audio"
            ;;

         --jack)
            OPTIONS="--jack-midi --jack-audio --autostart-jack"
            ;;

         --ja)
            OPTIONS="--jack-midi --alsa-audio --autostart-jack"
            ;;

         --codec)
            OPTIONS="--alsa-midi --alsa-audio=CODEC"
            ;;

         --cfgpath)
            shift
            CFGPATH="$1"
            ;;

         --b4)
            shift
            CFGPATH="$DEFCFGPATH"
            DOSTATE="yes"
            ;;

         *)
            echo "? Unsupported option; --help for more information"
            exit 1
            ;;

      esac
      shift

   done

fi

# if [ "USELATEST" == "yes" ] ; then
#     YOSHPATH="/usr/local/bin"
#     YOSHIMI="yoshimi-1.7.2rc1"
#     shift
# fi

if [ "$DOSTATE" == "yes" ] ; then
    STATEARG="--state=$CFGPATH/$STATEFILE"
    echo "Running $YOSHPATH/$YOSHIMI $OPTIONS $STATEARG"
        exec $YOSHPATH/$YOSHIMI $OPTIONS --state=$STATEARG # &
else
    exec $YOSHPATH/$YOSHIMI $OPTIONS # &
fi

