#!/bin/bash

# Check Python's global directories
have_python3=`type -p python3`
if [ "${have_python3}" ]
then
    SITES=($(python3 -c 'import site; print(site.getsitepackages())' | tr -d '[],'))
else
    SITES=($(python -c 'import site; print(site.getsitepackages())' | tr -d '[],'))
fi
for site in ${SITES[@]}
do
    site=`echo $site | sed -e "s/\'//g"`
    [ -d "${site}/roonapi" ] && {
        PYDIR=`echo ${site} | awk -F "/dist-packages/" ' { print $1 } '`
        USERSITE=$PYDIR/dist-packages
        break
    }
done
[ -d $USERSITE/roonapi ] || {
    echo "Roon API Python module not found. Exiting."
    exit 1
}

cd $PYDIR

[ -d ~/src/RoonCommandLine/patches ] || mkdir ~/src/RoonCommandLine/patches

OUT=$HOME/src/RoonCommandLine/patches/roonapi-listmedia.patch
rm -f $OUT
touch $OUT
for i in dist-packages/roonapi/*.orig
do
    j=`echo $i | sed -e "s/.orig//"`
    diff -Naur $i $j >> $OUT
done
