#!/bin/bash

#
# This program is free software under the terms of GNU General Public License
# See the GNU General Public License for more datails.
#
# Send any bug reports, comment or advice, and so on.
# To: mac24@kyoto.email.ne.jp
#
# This is version 0.2
# Date: 2000/10/24
# Update: 2000/11/16
#

#nice="nice -n -15"
nice=""

if [ "$1" = "" ]; then
    cddanum=`cdparanoia -Q 2>&1 | tail -n 3 | head -n 1 | sed 's/\./ /g' | awk '{print $1}'`
else
    cddanum=$1
fi

num=1

while [ $num -le $cddanum ]; do
    $nice cdparanoia -q $num $num.wav
    $nice gogo $num.wav -silent -m s -nopsy $num.mp3
    rm $num.wav
    num=`expr $num '+' 1`
done

tf="./title"

if [ -f $tf ]; then

    tn=`wc $tf | awk '{print $1}'`
    num=1

    if [ $cddanum -eq `expr $tn '-' 1` ]; then
	h=1
    else
	if [ $cddanum -ne $tn ]; then
	    echo "title error"
	    exit
	fi
	head=
	h=0
    fi

    for i in `cat $tf`; do

	if [ $h = "1" ]; then
	    head=$i-
	    h=0
	fi

	if [ $num -gt $cddanum ]; then
	    exit
	fi

	if [ $num -lt "10" ]; then
	    mv $num.mp3 $head\0$num-$i.mp3
	else
	    mv $num.mp3 $head$num-$i.mp3
	fi

	num=`expr $num '+' 1`
    done

fi
