#!/bin/bash
# Load a new DROID patch via USB -> MIDI SYSEX -> X7.
# This works on Linux and on Mac.
#
# Call this script with the file name of a DROID patch and it will be uploaded
# to the DROID master and started immediately. example: ./droidpatch mypatch.ini
#
# In order for this to work, you need:
# - An X7 connected to your master
# - An USB cable from your Linux/Mac to the X7
# - On Mac 'sendmidi' installed to /usr/local/bin
# - On Linux either 'amidi' or 'sendmidi' installed.
#
# Notes for Mac users:
#
# On Mac make sure that the tool "sendmidi" is available as an executable
# for the shell. This is easiest done by copying it to /usr/local/bin.
# Make sure that it is executable. If not, do a chmod 755 /usr/local/bin/sendmidi.
# You can download sendmidi here: https://github.com/gbevin/SendMIDI/releases.
#
# Notes for Linux users:
#
# On Linux you probably have installed ALSA, since it is the default
# lowlevel driver for MIDI and sound on Linux. The tool 'amidi' is contained
# there so most probably everything will simply work. If you have a Linux
# distribution with ALSA, download and install 'sendmidi' just as Mac users
# do (see above).

PATH=$PATH:/usr/loca/bin

if [ -n "$1" -a ! -e "$1" ] ; then
    echo "Droid patch file '$1' missing." >&2
    echo "Usage: $0 mypatch.ini" >&2
    exit 1
fi

if [ -z "$1" ] ; then
    echo "Reading DROID patch from standard input..."
    echo "Press Ctrl-C to abort."
fi

TMPFILENAME=$(mktemp /tmp/droidpatch-XXXXXX)

# Write start sequence (including sysex header)
echo -ne '\0360\0000\0146\0146P' > $TMPFILENAME

# Write content but remove comments. They might contain non-ascii characters
# Also remove spaces. They just eat up bytes. We need to be a bit
# more carful about spaces and # within strings. They must not be removed.
# At the end we use tr to remove all remaining non-ascii characters. They
# are not allowed. MIDI SYSEX needs the MSB to be cleared. To no UTF-8
# characters in texts like header = "Spaß". Sorry...
sed -e 's/^\([^"]*\)#.*/\1/' \
    -e 's/^[[:space:]]*//' \
    -e 's/^\([^=[:space:]]*\)[[:space:]]=[[:space:]]/\1=/' \
    -e '/^[^"]*$/s/[[:space:]]//g' \
    -e '/^[^"]*$/s/#.*//' "$1" \
    | tr -c -d '\n-~' >> $TMPFILENAME
# cat $TMPFILENAME ; exit

# Write SYSEX end byte
echo -ne '\0367' >> $TMPFILENAME

echo -n "Size of SYSEX containing compressed patch: " ; wc -c < $TMPFILENAME

if [ -e /usr/local/bin/sendmidi -a ! -x /usr/local/bin/sendmidi ] ; then
    echo "/usr/local/bin/sendmidi is not executable. " >&2
    echo "Please do with root permissions: " >&2
    echo "chmod 755 /usr/local/bin/sendmidi" >&2
    TOOL=false
fi

if type amidi >/dev/null 2>&1 ; then
    MIDIPORT=$(amidi -l | grep DROID | awk '{print $2;}')
    if [ -z "$MIDIPORT" ] ; then
        echo "Cannot find MIDI connection to Droid X7." >&2
        TOOL=false
    else
        TOOL=amidi
    fi
elif type sendmidi > /dev/null 2>&1 ; then
    TOOL=sendmidi
else
    echo "Commandline tool amidi and sendmidi both missing." >&2
    TOOL=false
fi


if [ "$TOOL" = amidi ] ; then
    amidi -p "$MIDIPORT" -s $TMPFILENAME
else
    $TOOL dev droid syf $TMPFILENAME
fi

if [ "$?" != 0 ]
then
    echo "Error sending MIDI SYSEX message" >&2
    rm -f $TMPFILENAME
    exit 1
else
    echo "Successfully sent $(wc -c < $TMPFILENAME) bytes to DROID X7 using $TOOL."
    rm -f $TMPFILENAME
    exit 0
fi

