#!/bin/sh
# ChangeLog
# 0.3 - id3 tags support
# 0.2 - directories support
# 0.1 - initial version
#DEVICE MUST not end with /
DEVICE=/mnt/usb
DEFAULT_LAMEOPTS="-m j -q 2 --vbr-new -V 9 -B 112"
DEFAULT_LAMEOPTS="-m j -q 7 --vbr-new -V 9 -B 96"
DEFAULT_LAMEOPTS="-m j -q 7 --vbr-new -V 9 -B 80"
DEFAULT_LAMEOPTS="-m j -q 7 -b 96"

if [ x$LAMEOPTS = x ] ; then
    LAMEOPTS="$DEFAULT_LAMEOPTS"
fi

echo "$(basename $0) version 0.3 by Michal Cihar <cihar at email dot cz>"

if [ "$#" -ne 1 ] ; then
    echo "Script to recompress mp3s and put them to $DEVICE"
    echo "usage: $(basename $0) dir"
    echo "   dir ... directory name in $DEVICE"
    exit 100
fi

if grep -q "$DEVICE" /proc/mounts ; then
    echo "$DEVICE seems to be mounted"
else 
    echo "Attempting to mount $DEVICE"
    if mount "$DEVICE" ; then
        echo "...succeeded"
    else
        echo "...failed"
        exit 1
    fi
fi

if [ -d "$DEVICE/$1" ] ; then
    echo "$DEVICE/$1 is directory, so it will be used"
else
    echo "$DEVICE/$1 is not directory, trying to create"
    if mkdir "$DEVICE/$1" ; then
        echo "...succeeded"
    else
        echo "...failed"
        exit 3
    fi
fi    

TMPFILE=`mktemp /tmp/to_diva.XXXXXXXXXX` || ( echo "Failed temp file creating!"; exit 2 )
rm -f "$TMPFILE"
TMPLCKFILE=`mktemp /tmp/to_diva.LCK.XXXXXXXXXX` || ( echo "Failed temp file creating!"; exit 2 )
rm -f "$TMPLCKFILE"

echo "Checking for needed subrectories"
find . -type d | sed 's#^\./##' | while read dirname ; do
    echo " * Creating subdirectory $dirname"
    mkdir -p "$DEVICE/$1/$dirname"
done
echo "...all subdirectories should be ready"


echo 1 > "$TMPLCKFILE"1
echo 1 > "$TMPLCKFILE"2
CYCLE=1
find . -name '*.mp3' | sed 's#^\./##' | while read fname ; do
    if [ $CYCLE -eq 1 ] ; then
        CYCLE=2
    else
        CYCLE=1
    fi
    
    # acquire id3 info
    ARTIST=`mp3info "$fname" -p "%a"`
    TITLE=`mp3info "$fname" -p "%t"`

    # wait for previous copying
    while [ x`cat "$TMPLCKFILE"$CYCLE` != x1 ]; do sleep 2;echo -n '.'; done

    # recompress mp3
    if [ "$TITLE" ] ; then
        if [ "$ARTIST" ] ; then
            lame $LAMEOPTS --ta "$ARTIST" --tt "$TITLE" "$fname" "$TMPFILE"$CYCLE
        else
            lame $LAMEOPTS --tt "$TITLE" "$fname" "$TMPFILE"$CYCLE
        fi
    else 
        # empty title -> don't create tag
        lame $LAMEOPTS "$fname" "$TMPFILE"$CYCLE
    fi
    
    # wait for previously moved file
    while [ x`cat "$TMPLCKFILE"$CYCLE` != x1 ]; do sleep 2;echo -n '.'; done
    # move in background
    echo 0 > "$TMPLCKFILE"$CYCLE; 
    ( mv "$TMPFILE"$CYCLE "$DEVICE/$1/$fname"; echo 1 > "$TMPLCKFILE"$CYCLE ) &
done

#cleanup
for CYCLE in 1 2 ; do
    while [ x`cat "$TMPLCKFILE"$CYCLE` != x1 ]; do sleep 2;echo -n '.'; done
    rm -f "$TMPLCKFILE"$CYCLE
    rm -f "$TMPFILE"$CYCLE
done

echo "Copying done"

gen_pl.sh "$1"

#echo "Attempting to create playlist"
#UPPER=`echo "$1"|tr '[:lower:]' '[:upper:]'`
#if ( mdir -/ "d:\\$1"|grep '^.\{8\} MP3'|sed 's#^\(.\{8\}\).*#$UPPER\\\1.MP3#' > "$DEVICE/""$UPPER.PL" ) ; then
#    echo "Seems to be done"
#else
#    echo "It seems to fail, check mtools configuration"
#fi


