#!/bin/bash

# settags 1.1 is a script for mass tagging of MP3, OGG, and FLAC files
# it uses: mutagen package (see: https://bitbucket.org/lazka/mutagen/)
#          or id3v2 and id3lib (libid3) packages for MP3s,
#          vorbiscomment from vorbis-tools package for OGGs,
#          and metaflac from flac package for FLACs
# Copyright (C) 2008-2015 Cezary M. Kruk <c.kruk@bigfoot.com>

# the script sets tags in "01 Track Title.mp3", "02 Track Title.mp3", ... files
# from "Artist Name - Album Title [Genre, Year]" directory
# or "Artist Name - Work Title (Orchestra; Director; etc.)" directory

if [ "$1" == "-h" -o "$1" == "--help" ]
then
cat <<EOF
settags: sets tags in MP3, OGG, and FLAC files
         on the basis of the directory and the files names

directory name format:
    "Artist Name - Album Title [Genre, Year]"
files names format:
    "01 Track Title.*", "02 Track Title.*", etc.
    where * is lowercase extension mp3, ogg, or flac

command:
    settags [ genre ]

genres:
    Rock, Jazz, Classical, Opera, Vocal, Other, etc.

default genre:
    Rock

genres list:
    lame --genre-list
EOF
exit
fi

file_test="no"
for file in *.mp3 *.ogg *.flac
do
    if [ -f "$file" ]
    then
	file_test="yes"
    fi
done

if [ "$file_test" == "no" ]
then
    echo "settags: there is no *.mp3, *.ogg, nor *.flac files here..."
    exit
fi

directory_test="no"
directory=`pwd | sed 's/.*\///'`
if [ "`echo $directory | grep -E '.+ - .+ [.+, [0-9]+]'`" != "" ]
then
    directory_test="yes"
fi

if [ "`echo $directory | grep -E '.+ - .+ (.+)'`" != "" ]
then
    directory_test="yes"
fi

if [ "$*" != "" ]
then
    genre="$*"
elif [ "$directory_test" == "yes" ]
then
    genre="`pwd | sed -r "s/.*\[//;s/, //;s/[0-9]+\]//"`"
else
    genre="Rock"
fi

if [ "$directory_test" == "yes" ]
then
    artist=`pwd | sed 's/.*\///;s/ - .*//'`
    album=`pwd | sed -r "s/.*? - //;s/ \[.*//"`
    year=`pwd | sed -r "s/.*\[//;s/.*, //;s/\]//"`
    if [ "`echo $year | sed -r 's/[0-9]+//'`" != "" ]
    then
	album=`pwd | sed -r "s/.*? - //;s/ \(.*//"`
	genre="Classical"
	year=""
    fi
fi

comment="ID3 v. 2.4"
echo
echo $comment

if [ "$directory_test" == "yes" ]
then
    echo $artist
    echo $album
    echo $genre
    echo $year
else
    echo $genre
fi

for file in *.mp3
do
    if [ -f "$file" ] && [[ -e `which mid3v2 2>/dev/null` || -e `which id3v2 2>/dev/null` ]]
    then
	track=`echo $file | sed -r 's/^(..) .*/\1/'`
	song=`echo $file | sed 's/...//;s/.mp3//'`
	echo "$track $song"
	if [ -e `which id3v2 2>/dev/null` ]
	then
	    id3v2 -D "$file" 1>/dev/null
	elif [ -e `which mid3v2 2>/dev/null` ]
	then
	    mid3v2 -D "$file" 1>/dev/null
	fi
	if [ "$directory_test" == "yes" ]
	then
	    if [ -e `which id3v2 2>/dev/null` ]
	    then
		if [ "$year" != "" ]
		then
		    id3v2 -2 -a "$artist" -A "$album" -t "$song" -y $year -g "$genre" -T $track -c "$comment" "$file" 1>/dev/null
		else
		    id3v2 -2 -a "$artist" -A "$album" -t "$song" -g "$genre" -T $track -c "$comment" "$file" 1>/dev/null
		fi
	    elif [ -e `which mid3v2 2>/dev/null` ]
	    then
		mid3v2 -a "$artist" -A "$album" -t "$song" -y $year -g "$genre" -T $track -c "$comment" "$file" 1>/dev/null
	    fi
	else
	    if [ -e `which id3v2 2>/dev/null` ]
	    then
		id3v2 -2 -t "$song" -g "$genre" -T $track -c "$comment" "$file" 1>/dev/null
	    elif [ -e `which mid3v2 2>/dev/null` ]
	    then
		mid3v2 -t "$song" -g "$genre" -T $track -c "$comment" "$file" 1>/dev/null
	    fi
	fi
	chmod 644 *.mp3
    fi
done

for file in *.ogg
do
    if [ -f "$file" ] && [ -e `which vorbiscomment 2>/dev/null` ]
    then
	track=`echo $file | sed -r 's/^(..) .*/\1/'`
	song=`echo $file | sed 's/...//;s/.ogg//'`
	echo "$track $song"
	if [ "$directory_test" == "yes" ]
	then
	    vorbiscomment -w -t "artist=$artist" -t "album=$album" -t "title=$song" -t "date=$year" -t "genre=$genre" -t "tracknumber=$track" -t "comment=$comment" "$file"
	else
	    vorbiscomment -w -t "title=$song" -t "genre=$genre" -t "tracknumber=$track" -t "comment=$comment" "$file"
	fi
	chmod 644 *.ogg
    fi
done

for file in *.flac
do
    if [ -f "$file" ] && [ `which metaflac 2>/dev/null` ]
    then
	track=`echo $file | sed -r 's/^(..) .*/\1/'`
	song=`echo $file | sed 's/...//;s/.flac//'`
	echo "$track $song"
	metaflac --dont-use-padding --remove-all-tags "$file"
	if [ "$directory_test" == "yes" ]
	then
	    metaflac --dont-use-padding --set-tag="comment=$comment" --set-tag="artist=$artist" --set-tag="album=$album" --set-tag="genre=$genre" --set-tag="date=$year" --set-tag="tracknumber=$track" --set-tag="title=$song" "$file"
	else
	    metaflac --dont-use-padding --set-tag="comment=$comment" --set-tag="genre=$genre" --set-tag="tracknumber=$track" --set-tag="title=$song" "$file"
	fi
	chmod 644 *.flac
    fi
done

echo

