#!/bin/bash

# ======================================================================
#   Copyright (C) 2007 and onwards Robert Muth <robert at muth dot org>
#
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
# ======================================================================

Usage()
{
cat  <<EOF
Useful tools for the manipulation of rhythmbox database files
(requires unescape.py)

Examples:
rhythmbox-tool.sh dump_all_playlists
* export all playlists in m3u format

rhythmbox-tool.sh dump_ratings
* export all ratings

EOF
}


set -o nounset
set -o errexit



RBPATH=${HOME}/.gnome2/rhythmbox
DB=$RBPATH/rhythmdb.xml
PL=$RBPATH/playlists.xml

# ======================================================================
# ======================================================================
rhythmbox-db-cleanup()
{
    echo "cleaning up  $1"
    cp --backup=numbered $1 $1.backup
    xmlstarlet ed\
        -d  "/rhythmdb/entry[contains(./location,'/music1')]"\
        -d  "/rhythmdb/entry[contains(./location,'/Favorites/')]"\
        $1.backup >  $1

}


dump_ratings()
{
    xmlstarlet sel -T -t\
        -m "/rhythmdb/entry/rating"\
        -s D:N:- '.'\
        -v "concat(.,' ', ../location)" -n $1
}

dump_playlist()
{
    xmlstarlet sel -T -t\
       -m "/rhythmdb-playlists/playlist[@name='$2']/location"\
       -v "." -n $1
}

list_playlists()
{
    xmlstarlet sel -T -t\
       -m "/rhythmdb-playlists/playlist[@type='static']"\
       -v "@name" -n $1
}


# ======================================================================
# ======================================================================
MODE=${1:-none}
if [ $MODE = 'none' ] ; then
    echo "you must specify a mode on the commandline:"
    Usage
    exit -1
fi

shift 1


if [ $MODE = 'db-cleanup' ] ; then
    rhythmbox-db-cleanup $DB
    exit 0
fi

if [ $MODE = 'll' ] ; then
    ls -l $RBPATH/*rhythmdb*
    exit 0
fi

if [ $MODE = 'dump' ] ; then
    cat $DB
    exit 0
fi

if [ $MODE = 'dump_ratings' ] ; then
    db=${1:-$DB}
    dump_ratings $db | sed -e 's+file:///+/+' | unescape.py
    exit 0
fi

if [ $MODE = 'dump_playlist' ] ; then
    pl=$1
    dump_playlist $PL "$pl" | sed -e 's+file:///+/+' | unescape.py
    exit 0
fi

if [ $MODE = 'list_playlists' ] ; then
    pl=${1:-$PL}
    list_playlists $pl
    exit 0
fi

if [ $MODE = 'dump_all_playlists' ] ; then
    pl=${1:-$PL}
    playlists=$(list_playlists $pl)
    for p in $playlists ; do
	echo "dumping $p"
	dump_playlist $pl "$p" | sed -e 's+file:///+/+' | unescape.py >$p
    done
    exit 0
fi

if [ $MODE = 'num-entries' ] ; then
    db=${1:-$DB}
    xmlstarlet sel -t -v  "count(/rhythmdb/entry)" $db
    exit 0
fi

if [ $MODE = 'num-ratings' ] ; then
    db=${1:-$DB}
    xmlstarlet sel -t -v  "count(/rhythmdb/entry/rating)" $db
    exit 0
fi


if [ $MODE = 'backup' ] ; then
    cp --backup=numbered $DB $DB.backup
    cp --backup=numbered $PL $PL.backup
    exit 0
fi

echo "unknown mode $MODE"
echo Usage
exit -1
