#!/bin/bash

# "lastfmarchive" bash script - builds up an archive of a last.fm XML feed
# (c) 2007 Dan Stowell
# This is free software under the terms of the GNU General Public License
# See http://www.gnu.org/copyleft/gpl.html for details of your rights to redistribute

set -o nounset
#set -o errexit

##################### HERE are the paths to the executables - you may need to change these ################
xmlstarlet='/opt/local/bin/xmlstarlet'; 
curl='/opt/local/bin/curl';


if [ $# -ne 2 ]
then
  echo "\"lastfmarchive\" - builds up an archive of a last.fm XML feed"
  echo "    (c) 2007 Dan Stowell"
  echo "             Free to use and distribute under the terms of the GNU Public License (GPLv2 or later)"
  echo ""
  echo "Usage: lastfmarchive [feedurl] [storagefolder]"
  echo "    feedurl should be a URL to a last.fm-style XML feed (not XSPF, not RSS)"
  echo "    storagefolder should be a folder not used for other things - "
  echo "         files will be read/written there, and a file archive.xml will be built up"
  echo ""
  echo "It relies on two CLI commands: XML starlet (command named 'xml' or 'xmlstarlet') and curl."
  echo ""
  exit 10;   # $WRONG_ARGS
fi

if [ ! -e "$xmlstarlet" ]; then
    echo "ERROR in lastfmarchive.bash: Can't find \"xmlstarlet\" executable. You may need to change the path, hard-coded in the script."
    exit 7;
fi
if [ ! -e "$curl" ]; then
    echo "ERROR in lastfmarchive.bash: Can't find \"curl\" executable. You may need to change the path, hard-coded in the script."
    exit 7;
fi

echo " --- lastfmarchive started at `date`"

# Get the params into humane vars
feedurl=$1
writedir=$2
tmplatestpath="${writedir}/tmplatest.xml"
latestpath="${writedir}/latest.xml"
trimlatestpath="${writedir}/trimlatest.xml"
archivepath="${writedir}/archive.xml"
newarchivepath="${writedir}/newarchive.xml"

# Download the file, check if it's changed, check if it's valid
$curl -s "$feedurl" > "$tmplatestpath"

#if exists file AND if the diff comes out equal, quit happily, no work to do
if [ -e "$latestpath" ]; then
    diffres=`diff -b "$tmplatestpath" "$latestpath"`
    if [ "$diffres" ]; then
        rm "$latestpath"
    else
        echo "No change since prev download; no work to do, exiting."
        rm "$tmplatestpath"
        exit 0;
    fi
else
    echo "Not got it"
fi
# Put the new version in place
mv "$tmplatestpath" "$latestpath"


# if archive file doesn't exist, we just copy into place and quit happily
if [ ! -e "$archivepath" ]; then
    cp "$latestpath" "$archivepath"
    echo "No archive path found; copying current to archive"
    exit 0;
fi

# Find the latest timestamp in the archive data
latestarchdate=`$xmlstarlet sel -T -t -v "/*/track/date/@uts" "$archivepath"`

echo "Most recent entry in the archive file, timestamp is $latestarchdate"


# Trim the latest file down to only those elements beating the timestamp
$xmlstarlet ed -d "/*/track[date/@uts<=$latestarchdate]" "$latestpath" > "$trimlatestpath"

newcount=`$xmlstarlet sel -t -v "count(/*/track)" "$trimlatestpath"`
echo "$newcount new record(s) to add to the archive"
$xmlstarlet sel -T -t -m /*/track -v "concat(name,' (',artist,')')" -n "$trimlatestpath"

if [ $newcount != 0 ]; then
    # Hackily prepend the new data
    cat "$trimlatestpath" | sed '$ d'   >  "$newarchivepath"
    cat "$archivepath"    | sed '1,2 d' >> "$newarchivepath"
    
    # Copy correct new archive into place
    rm -f "$archivepath"
    mv "$newarchivepath" "$archivepath"
    
fi

echo " --- lastfmarchive finished at `date`"
