#!/bin/bash
exec >> /home/pi/pythonDiskPlayer/mount.log 2>&1
export LC_ALL=en_GB.utf-8
export LANG=en_GB.utf-8

echo "$(date) Start."
echo "$(date) Media change detected on device $1"
device=${1##*/}

lsblk | grep $device

if [ $? -eq 0 ]; then
    echo "$(date) Device exists on machine."
    echo "$(date) Forcing unmount of floppy"
    /usr/bin/systemd-mount --umount /mnt/floppy
    echo "$(date) Floppy unmounted"
    echo "$(date) Mounting floppy "
    /usr/bin/systemd-mount $1 /mnt/floppy
    echo "$(date) Floppy mounted"
    var=$(cat /mnt/floppy/diskplayer.contents)
    echo python3 /home/pi/pythonDiskPlayer/player.py \"$var\"
    runuser -l pi -c "python3 /home/pi/pythonDiskPlayer/player.py \"$var\""
else
    echo "$(date) Device does not exist on machine."
    runuser -l pi -c "python3 /home/pi/pythonDiskPlayer/player.py stop"
    echo "$(date) Forcing unmount of floppy"
    /usr/bin/systemd-mount --umount /mnt/floppy
fi
echo "$(date) End."