#! /bin/bash
#
# ArchiveOrgGetter by Tyler spam@fenestrated.net
# This program is in the public domain. Do with it what you want, but realize
# that it is WITHOUT WARRANTY OF ANY KIND. It worked for me, but for all I know
# it might blow up your computer, make you cheat on your spouse, and lie to the
# tax authorities. I do not guarantee it in any way.
#
# This script uses the data in a _files.xml file from Archive.org to
# automatically:
# -Download the FLAC audio files for the concert
# -Convert the FLAC files to mp3 format
# -Set appropriate ID3 tags
# -Name the files in "tracknumber  songname.mp3" format
# -Name the folder to "concertname"
# -Clean up after itself (no XML, FLAC, or AIFF files hanging around after)
#
# Requirements: Some kind of *NIX box with the following software:
# BASH
# LAME
# wget
# flac
#		Note that most Linux distros have these out-of-the-box.
#
# How to use this script:
# Download the _files.xml file for the concert in which you're interested.
# Save the .xml file into a directory that has room for all the audio data.
# cd into the directory with the XML file in it.
# Run the script: ArchiveOrgGetter concert_files.xml
# DO NOT RUN THIS SCRIPT FROM ANOTHER DIRECTORY. BAD THINGS WILL HAPPEN.

#
# Begin Program
#

# Configuration Stuff
ARCHIVE_ORG_PATH="http://www.archive.org/download/"
FLAC_EXTENSION=".flac" #If you edit this, be sure to edit the "for..." line
AIFF_EXTENSION=".aiff"
MP3_EXTENSION=".mp3"
MP3_QUALITY="--preset standard"
WGET="wget"
LAME="lame"
FLAC="flac"

# Make the names make sense
xmlfile="$1"

# Get the path to the files at archive.org
PREFIX=`echo $xmlfile | sed 's/\(.*\)\(_files.xml\)/\1/'`

# Make us a work directory
mkdir "$PREFIX"

# Loop through the files
for FILE in `cat "$xmlfile" | sed -n 's/\(^.*[Nn][Aa][Mm][Ee]="\)\(.*\)\(\.flac".*$\)/\2/p'`
	do
	
	# Get the ID3 information from the XML file
	TRACK=`cat "$xmlfile" | sed -n "/$FILE/,/\/[Ff][Ii][Ll][Ee]>/ s/\(^.*[Tt][Rr][Aa][Cc][Kk]>\)\(.*\)\(<\/[Tt][Rr][Aa][Cc][Kk]>.*$\)/\2/p"`
	CREATOR=`cat "$xmlfile" | sed -n "/$FILE/,/\/[Ff][Ii][Ll][Ee]>/ s/\(^.*[Aa][Tt][Oo][Rr]>\)\(.*\)\(<\/[Cc][Rr][Ee][Aa].*$\)/\2/p"`
	ALBUM=`cat "$xmlfile" | sed -n "/$FILE/,/\/[Ff][Ii][Ll][Ee]>/ s/\(^.*[Aa][Ll][Bb][Uu][Mm]>\)\(.*\)\(<\/[Aa][Ll][Bb][Uu][Mm]>.*$\)/\2/p"`
	TITLE=`cat "$xmlfile" | sed -n "/$FILE/,/\/[Ff][Ii][Ll][Ee]>/ s/\(^.*[Tt][Ii][Tt][Ll][Ee]>\)\(.*\)\(<\/[Tt][Ii][Tt][Ll][Ee]>.*$\)/\2/p"`
	
	# Download the FLAC file
	$WGET -O "$PREFIX/$FILE$FLAC_EXTENSION" "$ARCHIVE_ORG_PATH/$PREFIX/$FILE$FLAC_EXTENSION"
	
	# Decompress the FLAC file and remove it
	$FLAC -d -o "$PREFIX/$FILE$AIFF_EXTENSION" "$PREFIX/$FILE$FLAC_EXTENSION"
	rm "$PREFIX/$FILE$FLAC_EXTENSION"

	# Encode the AIFF to MP3 and remove it
	$LAME $MP3_QUALITY --resample 44.1 --tt "$TITLE" --ta "$CREATOR" --tl "$ALBUM" --tn "$TRACK" --add-id3v2 "$PREFIX/$FILE$AIFF_EXTENSION" "$PREFIX/$FILE$MP3_EXTENSION"
	rm "$PREFIX/$FILE$AIFF_EXTENSION"

	# Make the filename nice
	PADTRACK=`echo $TRACK | sed '/^[0-9]$/ s/\(^.*$\)/0\1/'`
	mv "$PREFIX/$FILE$MP3_EXTENSION" "$PREFIX/$PADTRACK.$TITLE$MP3_EXTENSION"
	done

# Make the directory name nice
mv "$PREFIX" "$ALBUM"

# Clean up the XML file
rm "$1"

# We're Done!
# EOF
