#! /bin/bash

# mp3sync
# Syncs my music collection onto my MP3 player.

set -e

FLAGS='-a --delete -v --progress --copy-links --modify-window=1 --include=*/ --include=*.[Mm][Pp][23Gg] --include=*.[Ww][Mm][Aa] --exclude=*'
test -n "$DEBUG" && FLAGS="--dry-run -v $FLAGS"

DEVICE=/dev/sda1
TARGET=/media/xclef

#DATAROOTS="$HOME/incoming $HOME/Data/{,1,2,3}"
#DATACATS="Music Audiobooks"

DATAROOTS="$HOME/Data/"
DATACATS="Music"

SOURCES=
for x in $DATAROOTS
do
        for y in $DATACATS
        do
                if test -d $x/$y
                then
                        SOURCES="$SOURCES $x/$y"
                        test -n "$DEBUG" && echo $x/$y
                fi
        done
done

mounted=no
grep -q "$DEVICE $TARGET " /proc/mounts && mounted=yes

test $mounted = no && mount $TARGET

find $SOURCES -type f -perm +111 -exec chmod a-x {} ';'
nice rsync $FLAGS $SOURCES $TARGET || true

if test $mounted = no
then
        umount $TARGET
else
        echo $TARGET NOT unmounted!
fi
