#!/usr/bin/perl
#H# This renames all files listed on STDIN to a normal title like case.
#H# IOW, it capitolizes the first letter of every word except the simple ones
#H# like 'a' and 'the'.
#H# The script is also loaded with exception because it was mainly written to
#H# deal with mp3 file names and many bands use special cases.
#H# IOW this script is constantly updated to deal with special cases like 'KoRn'
#H# 
#H# The script is designed to be run like:
#H# ls | fixcase.pl
#H# It is designed to handle files names formatted like:
#H# Artist - Album - TrackNUM - Title.ext

# print out help info if requested
if ($ARGV[0] ne "") {
  open (SELF, $0);
  while (<SELF>) {
    if ($_ =~ /^#H# /) {
      $_ =~ s/^#H# //;
      print $_;
    }
  }
  exit;
}

while (<>)  {
  $LINE=$_;
  chomp $LINE;

# chop off and lowercase the extention
  @FP=split(/\./,$LINE);
  if ($#FP == 0)  {
    $FN=$LINE;
    $EXT="";
  } else {
    $EXT=$FP[$#FP];
    $EXT="\L$EXT";
    $FN=$LINE;
    $FN =~ s/$EXT$//;
  }

# convert %20 to space
  $FN =~ s/%20/ /g;

# chop out dumb stuff...
  $FN =~ s/-amrc.$/./i;

# uppercase the first letter of all words
  $FN =~ s/(\w+)/\u\L$1/g;

# begin fixing things that I DON'T want capitalized
# At the same time make exceptions for some words if they are the first word in a title
  $FN =~ s/ - Unknown - / - unknown - /g;
  $FN =~ s/^Unknown - /unknown - /g;
  $FN =~ s/Soundtrack/soundtrack/g;
  $FN =~ s/ The / the /g;
  $FN =~ s/- the /- The /g;
  $FN =~ s/\& the /\& The /g;
  $FN =~ s/ And / and /g;
  $FN =~ s/- and /- And /g;
  $FN =~ s/ From / from /g;
  $FN =~ s/- from /- From /g;
  $FN =~ s/ An / an /g;
  $FN =~ s/ Am / am /g;
  $FN =~ s/- an /- An /g;
  $FN =~ s/- am /- Am /g;
  $FN =~ s/ Or / or /g;
  $FN =~ s/- or /- Or /g;
  $FN =~ s/ Of / of /g;
  $FN =~ s/- of /- Of /g;
  $FN =~ s/ On / on /g;
  $FN =~ s/- on /- On /g;
  $FN =~ s/ To / to /g;
  $FN =~ s/- to /- To /g;
  $FN =~ s/\.Sq\./.sq./g;
  $FN =~ s/Dvd\./DVD./g;
  $FN =~ s/\.Dvd\./.dvd./g;
  $FN =~ s/\.Dvdr\./.DVDr./g;
  $FN =~ s/\.Dvdr\+Commentary\./.DVDr+commentary./g;
  $FN =~ s/\.Q\./.q./g;
  $FN =~ s/\.Tvr\./.TVr./g;
  $FN =~ s/\.Esvcd\./.esvcd./g;
  $FN =~ s/\.Gvsvcd\./.gvsvcd./g;
  $FN =~ s/\.Wsvcd\./.wsvcd./g;
  $FN =~ s/\.Vvcd\./.vvcd./g;
  $FN =~ s/\.Gvcd\./.gvcd./g;
  $FN =~ s/\.Gsvcd\./.gsvcd./g;
  $FN =~ s/\.Svcd\./.svcd./g;
  $FN =~ s/\.Vcd\./.vcd./g;
  $FN =~ s/\.Aac\./.aac./g;
  $FN =~ s/\.Ac3\./.ac3./g;
  $FN =~ s/ Be / be /g;
  $FN =~ s/ Ep / EP /g;
  $FN =~ s/Oc Remixes/OC Remixes/g;
  $FN =~ s/- be /- Be /g;
  $FN =~ s/ For / for /g;
  $FN =~ s/ Bto / BTO /g;
  $FN =~ s/- for /- For /g;
  $FN =~ s/ In / in /g;
  $FN =~ s/- in /- In /g;
  $FN =~ s/ Is / is /g;
  $FN =~ s/- is /- Is /g;
  $FN =~ s/ It / it /g;
  $FN =~ s/- it /- It /g;
  $FN =~ s/ At / at /g;
  $FN =~ s/- at /- At /g;
  $FN =~ s/ A / a /g;
  $FN =~ s/ Mc\./ MC./g;
  $FN =~ s/ Gt / GT /g;
  $FN =~ s/- a /- A /g;
  $FN =~ s/ Ar / ar /g;
  $FN =~ s/ Tv / TV /g;
  $FN =~ s/ Tv$/ TV/g;
  $FN =~ s/- ar /- Ar /g;
  $FN =~ s/ That / that /g;
  $FN =~ s/- that /- That /g;
  $FN =~ s/ With / with /g;
  $FN =~ s/- with /- With /g;
  $FN =~ s/U\.s\./U.S./g;
  $FN =~ s/Usa/USA/g;
  $FN =~ s/Debarge /DeBarge /g;
  $FN =~ s/Inxs /INXS /g;
  $FN =~ s/ Tos / TOS /g;
  $FN =~ s/Revamp /ReVamp /g;
  $FN =~ s/Aka /aka /g;
  $FN =~ s/Uk/UK/g;
  $FN =~ s/Korn /KoRn /g;
  $FN =~ s/Tvc15 /TVC15 /g;
  $FN =~ s/Waaf/WAAF/g;
  $FN =~ s/Deathmatch/DeathMatch/g;
  $FN =~ s/Wwiii/WWIII/g;
  $FN =~ s/Ou812/OU812/g;
  $FN =~ s/\(Ep\)/(EP)/g;
  $FN =~ s/Kmfdm/KMFDM/g;
  $FN =~ s/Lovehatetragedy/LoveHateTragedy/g;
  $FN =~ s/Symlink/symlink/g;
  $FN =~ s/'T/'t/g;
  $FN =~ s/'D/'d/g;
  $FN =~ s/'M/'m/g;
  $FN =~ s/'N/'n/g;
  $FN =~ s/'R/'r/g;
  $FN =~ s/'V/'v/g;
  $FN =~ s/'S/'s/g;
  $FN =~ s/'L/'l/g;
  $FN =~ s/'E/'e/g;
  $FN =~ s/ 'em/ 'Em/g;
  $FN =~ s/Relixiv/ReliXIV/g;
  $FN =~ s/Astrowaste/AstroWaste/g;
  $FN =~ s/\[Disc /[disc /g;
  $FN =~ s/\(Live/(live/g;
  $FN =~ s/\(Instrumental/(instrumental/g;
  $FN =~ s/\(Remix\)/(remix\)/g;
  $FN =~ s/\(Demo\)/(demo\)/g;
  $FN =~ s/\(Low Quality\)/(low quality\)/g;
  $FN =~ s/\(Incomplete\)/(incomplete\)/g;
  $FN =~ s/\(Uncensored\)/(uncensored\)/g;
  $FN =~ s/ - Anime\./ - anime./g;
  $FN =~ s/Uhf/UHF/g;
  $FN =~ s/^Misc$/misc/g;
  $FN =~ s/Run Dmc/Run DMC/g;
  $FN =~ s/Zz Top/ZZ Top/g;
  $FN =~ s/ Ost / OST /g;
  $FN =~ s/Ac-Dc/AC-DC/g;
  $FN =~ s/Iii/III/g;
  $FN =~ s/Ii/II/g;
  $FN =~ s/ Iv / IV /g;
  $FN =~ s/ Ix/ IX/g;
  $FN =~ s/Viii/VIII/g;
  $FN =~ s/Vii/VII/g;
  $FN =~ s/Xiii/XIII/g;
  $FN =~ s/Xii/XII/g;
  $FN =~ s/ Vi / VI /g;
  $FN =~ s/ Xi / XI /g;
  $FN =~ s/ Xiv / XIV /g;
  $FN =~ s/ Vi\./ VI./g;
  $FN =~ s/ Xi\./ XI./g;
  $FN =~ s/ Xiv\./ XIV./g;
  $FN =~ s/IXnay/Ixnay/g;
  $FN =~ s/\(Part /\(part /g;
  $FN =~ s/^Dragonforce /DragonForce /g;

# remove extra spaces
  $FN =~ s/  / /g;
  $FN =~ s/  / /g;
  $FN =~ s/  / /g;
  $FN =~ s/  / /g;
  $FN =~ s/  / /g;

# put file name and extention back together
  if ($EXT eq "") { $NFN=$FN; }
  if ($EXT ne "") { $NFN="$FN$EXT"; }

# Do the actual move but only if the filename has actually changed.
  if ($LINE ne $NFN)  {
    print ("$LINE ==> $NFN\n");
    # This double move is a hold over from the days when my mp3z were stored on a
    # vfat partition.  In vfat you can't just change the name of a filename because
    # vfat is not case sensitive and thinks both filenames are the same file.
    # I probably don't need this anymore but it doesn't hurt.
    system (qq[mv "$LINE" x]);
    system (qq[mv x "$NFN"]);
  }
}