#!/bin/bash
#
# Fix all ARTIST tags with "The bla-de-bla" to "bla-de-bla, The"
#
# Thanks to http://blog.tordeu.com/?p=184 for snippets
#

if [ $1 ]; then
  do_artist=$1
else
  do_artist=""
fi

base_dir=/home/music
high_dir="flac"

tmpy=/tmp/flac_un_the

# Some stat counters
let n_artists=0
let n_albums=0
let n_tracks=0
let n_artists_r=0
let n_albums_r=0
let n_tracks_r=0
let n_artists_c=0
let n_albums_c=0
let n_tracks_c=0
last_artist=""
last_album=""

# Create a tmp directory for dumps
if [ ! -d "${tmpy}" ]; then
  mkdir ${tmpy}
fi

# Scan through all of the high quality directories
for this_high_dir in ${high_dir}
do
  # Scan for artists
  echo "Scanning ${this_high_dir} directory ..."
  ls -1 "${base_dir}/${this_high_dir}" > ${tmpy}/${this_high_dir}

  # Scan for albums
  while read artist
  do
    if [ "${do_artist}" = "" ] || [ "${do_artist}" = "${artist}" ]; then
      let n_artists=$n_artists+1
      # Scan for tracks
      echo "  Found ${artist} - scanning for albums ..."
      artist_clean=`echo ${artist//[ \[\]()&\'\"\,]/_} | tr -s _ _ `
      if [ "${artist}" != "${artist_clean}" ]; then
        echo "  Renaming ${artist} to ${artist_clean}"
        mv "${base_dir}/${this_high_dir}/${artist}" "${base_dir}/${this_high_dir}/${artist_clean}"
        artist=${artist_clean}
        let n_artists_r=$n_artists_r+1
      fi
      ls -1 "${base_dir}/${this_high_dir}/${artist}" > ${tmpy}/${this_high_dir}-${artist}

      while read album
      do
        let n_albums=$n_albums+1
        echo "    Found ${album} - scanning for tracks ..."
        album_clean=`echo ${album//[ \[\]()&\'\"\,]/_} | tr -s _ _ `
        if [ "${album}" != "${album_clean}" ]; then
          echo "    Renaming ${album} to ${album_clean}"
          mv "${base_dir}/${this_high_dir}/${artist}/${album}" "${base_dir}/${this_high_dir}/${artist}/${album_clean}"
          album=${album_clean}
          let n_albums_r=$n_albums_r+1
        fi

        ls -1 "${base_dir}/${this_high_dir}/${artist}/${album}" > ${tmpy}/${this_high_dir}-${artist}-${album}

        # Now the fun bit ... let's start re-encoding the track we've just found
        while read track
        do
          track_the=`echo ${track} | awk -F "-" '{print $2}' | awk -F "_" '{print $1}'`
          if [ "${track_the}" = "the" ]; then
	    track_id=`echo ${track} | awk -F "-" '{print $1}'`
	    track_artist=`echo ${track} | awk -F "-" '{print $2}'`
	    track_the_rest=`echo ${track} | awk -F "-" '{$1=$2=""}1' | tr -d ' ' | tr ' ' '_'`
	    track_artist_rest=`echo ${track_artist} | awk -F "_" '{$1=""}1' | sed 's/^ *//g' | tr ' ' '_'`
	    track_clean="${track_id}-${track_artist_rest}_the-${track_the_rest}"
            echo "      Renaming ${track}"
            echo "            to ${track_clean}"
            mv "${base_dir}/${this_high_dir}/${artist}/${album}/${track}" "${base_dir}/${this_high_dir}/${artist}/${album}/${track_clean}"
            track=${track_clean}
            let n_tracks_r=$n_tracks_r+1
          fi

	  this_source="${base_dir}/${this_high_dir}/${artist}/${album}/${track}"
	  this_dest=`echo ${base_dir}/${out_dir}/${artist}/${album}/${track} | sed 's/.flac/.mp3/g'`

          # Check that it's a FLAC file
	  if [ `file "${this_source}" | grep -c "FLAC audio bitstream data"` = "1" ]; then
            let n_tracks=$n_tracks+1
            # Get tag data from source. 
	    # Artist
	    s_artist=`metaflac --show-tag=ARTIST "${this_source}" | awk -F "=" '{print $2}'`

	    is_the_the=`echo ${s_artist} | awk '{print $1}'`
	    if [ "${is_the_the}" = "The" ] || [ "${is_the_the}" = "the" ] || [ "${is_the_the}" = "THE" ]; then
              #the_rest=`echo ${s_artist} | awk '{for(i=2;i<=NF;i++)printf "%s", $i}' | tr '\n' ' '`
              the_rest=`echo ${s_artist} | awk '{$1=""}1' | sed 's/^ *//g'`
	      new_artist="${the_rest}, The"
              echo "      Re-tagging \"${s_artist}\" to \"${new_artist}\""
              echo "        in ${track}"
	      metaflac --remove-tag=ARTIST ${this_source}
	      metaflac --set-tag="ARTIST=${new_artist}" ${this_source}

              let n_tracks_c=$n_tracks_c+1
              if [ "${artist}" != "${last_artist}" ]; then
                let n_artists_c=$n_artists_c+1
              fi
              if [ "${album}" != "${last_album}" ]; then
                let n_albums_c=$n_albums_c+1
              fi
              last_artist=${artist}
	      last_album=${album}

	      ##########################
	      # Wasn't that simple?!?! #
	      ##########################
	    fi
	  fi
        done <${tmpy}/${this_high_dir}-${artist}-${album}
      done <${tmpy}/${this_high_dir}-${artist}
    fi
  done <${tmpy}/${this_high_dir}
done
  
echo ""
echo "Found ${n_tracks} tracks in ${n_albums} albums from ${n_artists} artists"
echo "Renamed ${n_tracks_r} tracks, ${n_albums_r} albums and ${n_artists_r} artists"
echo "Re-tagged ${n_tracks_c} tracks in ${n_albums_c} albums from ${n_artists_c} artists"
echo ""

# Now clean up the tmp area
""rm -f -R ${tmpy}