#!/bin/bash
set -e

SRC_DIR="/run/media/powellc/SANSA CLIPZ"
DEST_DIR="/home/powellc/var/rockbox/"
FILENAME=".scrobbler.log"

WEBDAV_URL="https://box.unbl.ink/cloud/files/var/rockbox"
WEBDAV_USER="colin@unbl.ink"

# Wait a moment to ensure mount is complete
sleep 2

if [ -f "$SRC_DIR/$FILENAME" ]; then
    TIMESTAMP=$(date +"%Y%m%d-%H%M%S")
    NEW_NAME="scrobbles-$TIMESTAMP.tsv"

    # Backup locally
    mv "$SRC_DIR/$FILENAME" "$DEST_DIR/$NEW_NAME"

    # Send to WebDAV
    curl -s --netrc -T "$DEST_DIR/$NEW_NAME" "${WEBDAV_URL}/${NEW_NAME}"

    # Notify user
    curl -s -d "Backed up $FILENAME locally and sent to Webdav" https://ntfy.unbl.ink/life >/dev/null
else
	echo "File not found"
fi
