#!/usr/bin/perl
# Written to suppliment crip with a tag editor.
# Supports both .ogg and .flac files.

# Put preferred editor here
$editor = "vim";

$file = $ARGV[0];

unless (-e "$file") {
	die "File \"$file\" does not exist.\n";
}

if (-e "$file.tag1.tmp") {
	die "There is already a file \"$file.tag1.tmp\" in /tmp !!\n";
}
if (-e "$file.tag2.tmp") {
	die "There is already a file \"$file.tag2.tmp\" in /tmp !!\n";
}

# Escape certain characters from $file
# code taken directly from crip actually (crip does the same
#  sort of thing 3 different times in its code)
# (don't ask me why it worked above but doesn't work below without escapes)
$file =~ s/\(/\\(/g;  $file =~ s/\)/\\)/g;
$file =~ s/'/\\'/g;  $file =~ s/`/\\`/g;
$file =~ s/\"/\\\"/g;  $file =~ s/ /\\ /g;
$file =~ s/\;/\\\;/g;  $file =~ s/,/\\,/g;
$file =~ s/&/\\&/g;  $file =~ s/\$/\\\$/g;

if ($file =~ m/\.ogg$/) {
	# First check for vorbiscomment existence:
	$out = `which vorbiscomment`;
	if ($out eq "") {
		die "Cannot find `vorbiscomment` for tagging the Ogg Vorbis files!\n";
	}

	system "vorbiscomment -l $file > /tmp/$file.tag1.tmp";
	if (-z "/tmp/$file.tag1.tmp") {
		print "Note: incoming tag is empty.\n";
	}

	system "cp /tmp/$file.tag1.tmp /tmp/$file.tag2.tmp";
	system "$editor /tmp/$file.tag2.tmp";

	$out = system "diff /tmp/$file.tag1.tmp /tmp/$file.tag2.tmp";
	if ($out == 0) {
		print "No changes detected.  Leaving file and tag untouched.\n";
		print "Deleting temporary file: /tmp/$file.tag1.tmp\n";
		system "rm /tmp/$file.tag1.tmp";
		print "Deleting temporary file: /tmp/$file.tag2.tmp\n";
		system "rm /tmp/$file.tag2.tmp";
	} else {
		if (-z "/tmp/$file.tag2.tmp") {
			print "Warning: outgoing tag is now empty.\n";
			print "Are you sure you want to overwrite the old tag? (y/n): ";
			$inp = <STDIN>;  chop($inp);
			unless (($inp eq "y") || ($inp eq "Y")) {
				print "Will not overwrite old tag.  Exitting...\n";
				exit(0);
			}
		}
		print "Writing new tag info...\n";
		system "vorbiscomment -w -c /tmp/$file.tag2.tmp $file";
		print "Done.\n";
		print "Deleting temporary file: /tmp/$file.tag1.tmp\n";
		system "rm /tmp/$file.tag1.tmp";
		print "Deleting temporary file: /tmp/$file.tag2.tmp\n";
		system "rm /tmp/$file.tag2.tmp";

		print "\nTag info for $file now reads:\n";
		system "vorbiscomment -l $file";
		print "\n";
	}
} elsif ($file =~ m/\.flac$/) {
	# First check for metaflac existence:
	$out = `which metaflac`;
	if ($out eq "") {
		die "Cannot find `metaflac` for tagging the flac files!\n";
	} else {
		# metaflac exists, get its version number
		$mfver = `metaflac --version`;  chop $mfver;
		$mfver =~ m/(\d+\..+)/;  $mfver = $1;
	}

	if ($mfver lt "1.1.1") {
		system "metaflac --export-vc-to=/tmp/$file.tag1.tmp $file";
	} else {
		system "metaflac --export-tags-to=/tmp/$file.tag1.tmp $file";
	}
	if (-z "/tmp/$file.tag1.tmp") {
		print "Note: incoming tag is empty.\n";
	}

	system "cp /tmp/$file.tag1.tmp /tmp/$file.tag2.tmp";
	system "$editor /tmp/$file.tag2.tmp";

	$out = system "diff /tmp/$file.tag1.tmp /tmp/$file.tag2.tmp";
	if ($out == 0) {
		print "No changes detected.  Leaving file and tag untouched.\n";
		print "Deleting temporary file: /tmp/$file.tag1.tmp\n";
		system "rm /tmp/$file.tag1.tmp";
		print "Deleting temporary file: /tmp/$file.tag2.tmp\n";
		system "rm /tmp/$file.tag2.tmp";
	} else {
		if (-z "/tmp/$file.tag2.tmp") {
			print "Warning: outgoing tag is now empty.\n";
			print "Are you sure you want to overwrite the old tag? (y/n): ";
			$inp = <STDIN>;  chop($inp);
			unless (($inp eq "y") || ($inp eq "Y")) {
				print "Will not overwrite old tag.  Exitting...\n";
				exit(0);
			}
		}
		print "Writing new tag info...\n";
		if ($mfver lt "1.1.1") {
			system "metaflac --remove-vc-all --import-vc-from=/tmp/$file.tag2.tmp $file";
		} else {
			system "metaflac --remove-all-tags --import-tags-from=/tmp/$file.tag2.tmp $file";
		}
		print "Done.\n";
		print "Deleting temporary file /tmp/$file.tag1.tmp\n";
		system "rm /tmp/$file.tag1.tmp";
		print "Deleting temporary file: /tmp/$file.tag2.tmp\n";
		system "rm /tmp/$file.tag2.tmp";

		print "\nTag info for $file now reads:\n";
		if ($mfver lt "1.1.1") {
			system "metaflac --export-vc-to=- $file";
		} else {
			system "metaflac --export-tags-to=- $file";
		}
		print "\n";
	}
} else {
	die "File \"$file\" does not have the .ogg or .flac extension.\n";
}

# Done.