#!/bin/bash

#   Audina Audiobook to Nokia Converter
#   Based on idea of Fezouro's script from Nokia forum
#   Copyright (C) Bohdan R. Rau 2010 <ethanak@polip.com>
#
#   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, 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.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write see:
#               <http://www.gnu.org/licenses/>.

function helpme()
{
(
echo "Sposob użycia: $0 [opcje] <outputdir> [<booktitle>]"
echo "Jeśli nie podałeś booktitle, przyjmowany jest <outputdir>"
echo "Opcje -s, -d, -m, -g, -p, -E i -P"
echo "będą przekazane do programu audina"
echo
echo "Program musi być uruchomiony w katalogu zawierającym pliki mp3"
echo "oraz ewentualnie jeden obrazek jpg (okładka)."
echo "Utworzony zostanie katalog <outputdir> dla Nokia Audiobook") | \
if [ "`locale charmap`" = "UTF-8" ] ; then
    cat
else
    iconv -f UTF-8 -t `locale charmap`//TRANSLIT
fi
exit 1
}

austr=""
while getopts "hs:d:m:p:g:E:P:" opt ; do
    if [ "$opt" == "h" ] ; then
        helpme
    elif [ "$opt" == "s" ] || [ "$opt" == "d" ] || \
        [ "$opt" == "g" ] || \
        [ "$opt" == "E" ] || \
        [ "$opt" == "P" ] || \
        [ "$opt" == "m" ] || [ "$opt" == "p" ] ; then
        austr=$austr" -$opt $OPTARG"
    else
        echo "Unknown option "$opt
        helpme
    fi
done

shift $((OPTIND-1))

if [ $# -lt 1 ]; then
	helpme
fi

dir="$1"
if [ "$2" = "" ] ; then
title="$dir"
else
title="$2"
fi

jpegs=`find . -maxdepth 1 -iname "*jpg"| wc -l`

echo "Converting: $title"

tmp="$TMPDIR"
if [ "$tmp" == "" ] ; then
    tmp=$HOME"/tmp"
    if [ ! -d "$tmp" ] ; then
        tmp="/tmp"
    fi
fi

index="$tmp/index.inx"

rm -f $index
rm -rf "$dir"
mkdir "$dir"

echo "#BOOK" > $index
echo "$title;" >> $index
if [ $jpegs -eq 1 ] ; then
	echo "#PIC" >> $index
	echo "$dir.jpg;" >> $index
    cp *.jpg "$dir/$dir.jpg"
fi
echo "#TRACKS" >> $index

count=`find . -maxdepth 1 -iname "*mp3"| wc -l`

start=`date +%s`

current=0
ls *mp3 | sort | while read i; do
    current=$(($current+1))
	out=$dir'_'${current}.awb
	echo -n "[$current/$count] $out"
	length=`audina $austr "$i" "$dir/$out" 2>/dev/null | awk '/Duration/ {print int(($2+500)/1000)}'`
	echo " "$length" sec"
	echo "$out:$length;" >> "$index"
done
echo "#CHAPTERS" >> "$index"
chapter=0;
ls *mp3 | sort | while read i; do
	chapter=$(($chapter+1))
	out=$dir'_'${chapter}.awb
	echo "$out:0s:$chapter:Chapter $chapter;" >> $index
done
echo "#VERSION" >> "$index"
echo "0.7;" >> "$index"

echo "#CONTENT_INFO" >> "$index"
echo "NokiaAudiobookManagerVersion=Audina Converter;" >> $index
echo "CodecMode=7;" >> "$index"
echo "CodecBitRate=23050;" >> "$index"
echo "DTX=0;" >> "$index"
echo "SBRA=0;" >> "$index"
echo "SBRA_Rate=900;" >> "$index"
echo "InputFormat=2;" >> "$index"

# index must be in little endian UTF-16 unicode format
iconv -t UTF-16 "$index" > "$1/$1.inx"
rm "$index"
