#!/bin/sh
# Copyright (C) 2008 - 2012 vortexbox.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 2
# of the License, or (at your option) any later version.
#
# 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.
#

# rename Windows files
cd /storage/music/flac
find . -name Cover.jpg -type f -execdir mv Cover.jpg cover.jpg \;
find . -name Folder.jpg -type f -execdir mv Folder.jpg cover.jpg \;

# get rid of false tracks
cd /storage/music/mp3
find . -size -30k -name *.mp3 -print0 | xargs -0 rm -rf
cd /storage/music/flac
find . -size -60k -name *.flac -print0 | xargs -0 rm -rf

echo ""
echo "Getting cover art from amazon.com ..."
echo ""

cd /storage/music/flac
find . -name folder.jpg -type f -execdir cp -an folder.jpg cover.jpg \;
find . -type d -exec /usr/bin/php /opt/vortexbox/covergrabber.php {} \;

# Some application use cover.jpg and some use folder.jpg
# This way we have both
# Thanks Ron, for point out that -a is better!
cd /storage/music/flac
find . -name cover.jpg -type f -execdir cp -a cover.jpg folder.jpg \;

echo ""
echo "Setting permissions..."

cd /storage
chown -R root:root *
chmod -R 777 *
chown -R squeezeboxserver:squeezeboxserver /storage/music/playlist/
chown -R transmission:transmission /storage/downloads

echo ""
echo "Rescanning SqueezeBox music library..."
echo ""

/usr/bin/php /opt/vortexbox/rescan.php

echo ""
echo "Done."
echo ""
