#!/bin/sh
#
#		Copyright (C) 2007 Olivier Meurice <olivier_meurice@users.sourceforge.net>
#		ACripper V1.2
#		
#		This library is free software; you can redistribute it and/or
#   modify it under the terms of the GNU Library General Public
#   License as published by the Free Software Foundation; either
#   version 2 of the License, or (at your option) any later version.
#
#   This library 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
#   Library General Public License for more details.
#
#   You should have received a copy of the GNU Library General Public
#   License along with this library; if not, write to the
#   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
#   Boston, MA  02111-1307, USA.
#
#   This Bash Script read music tracks titles from freedb.org or a text 
#   file if no CDDB info found. For each track name it rips the track from 
#   the CD and encode it on the fly (ogg by default).
#   Requirements: 
#     - cdparanoia <http://www.xiph.org/paranoia/>
#     - oggenc     <http://www.vorbis.com/> 
#                  <http://www.xiph.org/ogg/>
#     - cddbc      <From this package: http://sourceforge.net/projects/acripper/>
#
#   Optional:
#     -flac        <http://flac.sourceforge.net/>
#     -lame        <http://www.mp3dev.org>
#
#		Text file for CD traks should be of following structure:
#   	artist
#   	title
#			genre
#   	track 1
#   	...
#   	track n
#
#   Freedb.org values for genre are listed in the FAQ at <http://www.freedb.org/en/faq.3.html#26>
#   For mp3, ID3 tags values must be used instead (see at <http://www.id3.org/id3v2.3.0> for 
#   genre List from ID3v1)
#
#		Usage: acripper [-e flac|ogg|mp3] [-f[file] -n]
#   Examples:
#   > acripper
#   > acripper -f
#   > acripper -e flac -fcdinfo.txt

LENGTH=2;
RC=0;
TRACK_NUM=1;
TITLES_FILE="titles.txt";     # Default CD info file
USE_FILE="n";                 # By default try to retrieve CD info from CDDB server
CODEC="ogg"                   # Encode to OGG by default
NUMBERING="n";                # Numbering track titles

ARTIST="unknown";
ALBUM="unknown";
GENRE="misc";
OGG_QUALITY=6;

# Encoding function
# Params: CODEC, TRACK_NUM, ARTIST, ALBUM, GENRE, TITLE, OUTFILE
function encode()
{
	OUTFILE=`echo $6 | sed s/\ /_/g`.$1;
	echo " Ripping and encoding track $2 [$6] to [$OUTFILE]";
		
	if [ $1 = "ogg" ]; then	  
		cdparanoia -q -w $2 - | oggenc -q $OGG_QUALITY -a "$3" -l "$4" -G "$5" -t "$6" -o "$OUTFILE" -;
	elif [ $1 = "flac" ]; then
		cdparanoia -q -w $2 - | flac --best -T "ARTIST=$3" -T "ALBUM=$4" -T "GENRE=$5" -T "TITLE=$6" -o "$OUTFILE" -;
	elif [ $1 = "mp3" ]; then
	  GENRE=$5;
	  if [ $GENRE = "misc" ]; then GENRE="Other"; fi  #Only ID3 tag supported
		cdparanoia -q -w $2 - | lame -q2 --ta "$3" 	--tl "$4" --tg "$GENRE" --tt "$6" - "$OUTFILE";
	fi
}

function print_header_info()
{
	echo;
	echo " CD title:    $1";
	echo " Artist name: $2";
	echo " Genre:       $3";
	echo;
}
 
# ---------------------------------------- #
# Parse command-line args in unquoted mode #
# ---------------------------------------- #
PARAMS=`getopt -o f::e:n -u -n 'ACRipper' -- "$@"`
[ $? != 0 ] && echo "Usage: acripper [-e flac|ogg|mp3] [-f[file] -n]" >&2 && exit 1;
set -- $PARAMS;
while true ; do
	case "$1" in
		-f)
			case "$2" in
				--) echo "Force the usage of file titles.txt"; USE_FILE="y"; shift 2 ;;
				*)  echo "Force the usage of file $2" ; USE_FILE="y"; TITLES_FILE=$2; shift 2 ;;
			esac ;;
		-e)
			case "$2" in
			  ogg)  CODEC="ogg"; shift 2 ;;
			  flac) CODEC="flac"; shift 2 ;;				
				mp3)  CODEC="mp3"; shift 2 ;;
			esac ;;		
		-n)
			case "$2" in
				--) echo "Preceed track title by track number"; NUMBERING="y"; shift 1;;
			esac ;;
		--) shift ; break ;;
		*) break ;;
	esac
done

#Get CD track count
TRACK_COUNT=`cddbc -c`;

echo;
echo " ACRipper v1.2";
echo " Olivier Meurice 2007";
echo " See ACRipper home page at <http://sourceforge.net/projects/acripper>";
echo;
echo " CD contains $TRACK_COUNT track(s)";
echo;

# Determine whether CD info file should be used instead of CDDB server
if [ "${USE_FILE}" = "n" ]; then  
	echo " --> Try to connect to freedb.org...";  
	ALBUM=`cddbc -t`; 
	[ $? -ne 0 ] && echo " --> No info available on CDDB server" && USE_FILE="y";
fi

# ----------------------------------- #
# Rip & encode using CDDB server info #
# ----------------------------------- #    
if [ "${USE_FILE}" = "n" ]; then
  ARTIST=`cddbc -a`;
	if [ $? -ne 0 ]; then
			ARTIST="Unknown";
	fi
	GENRE=`cddbc -g`;
	if [ $? -ne 0 ]; then
			GENRE="misc";
	fi
	
	print_header_info "$ALBUM" "$ARTIST" "$GENRE";
	
	until [ $TRACK_NUM -gt $TRACK_COUNT ]; do
		TITLE=`cddbc -t$TRACK_NUM`;				
		if [ $? -ne 0 ]; then
			TITLE=`printf "track%.${LENGTH}d" $TRACK_NUM`;
		else
			if [ "${NUMBERING}" = "y" ]; then
				TITLE=`printf "%.${LENGTH}d_$TITLE" $TRACK_NUM`;
			fi
		fi
		encode $CODEC $TRACK_NUM "$ARTIST" "$ALBUM" "$GENRE" "$TITLE" "$OUTFILE";
		TRACK_NUM=$(($TRACK_NUM + 1));
	done
# ---------------------------- #
# Rip & encode using file info #
# ---------------------------- #	
else                       
  echo;
  echo " --> Try to use file $TITLES_FILE...";
	echo;
	
	if [ -f "${TITLES_FILE}" ]; then
		exec < $TITLES_FILE;

		read ARTIST;
		read ALBUM;
		read GENRE;
		
		print_header_info "$ALBUM" "$ARTIST" "$GENRE";

		until [ $TRACK_NUM -gt $TRACK_COUNT ]; do
			read TITLE;
			if [ $? -ne 0 ]; then
				TITLE=`printf "track%.${LENGTH}d" $TRACK_NUM`;				
			fi
			encode $CODEC $TRACK_NUM "$ARTIST" "$ALBUM" "$GENRE" "$TITLE" "$OUTFILE";
			TRACK_NUM=$(($TRACK_NUM + 1));
		done		
		
	else                   # No CD info file available
		echo;
		echo " --> File $TITLES_FILE not found.";
		echo;
		
		until [ $TRACK_NUM -gt $TRACK_COUNT ]; do
			TITLE=`printf "track%.${LENGTH}d" $TRACK_NUM`;
			OUTFILE=`echo $TITLE`.$CODEC;	
			encode $CODEC $TRACK_NUM "$ARTIST" "$ALBUM" "$GENRE" "$TITLE" "$OUTFILE";
			RC=$?;
			TRACK_NUM=$(($TRACK_NUM + 1));
		done
	fi
fi
