#!/usr/bin/perl

foreach (@ARGV) {
  if ($_ eq 'music-ui.xml') { 
    print "Skipping $_ - non MythUI theme file.\n.";
    next;
  }
  $f1 = $_;
  $f2 = $_ . '~';
  
  rename ($f1, $f2);
  
  open(IN, $f2);
  open(OUT, ">$f1");
  
  while(<IN>) {

    # Clock format tag changed to template
    s!format>!template>!g;
    if (/secondflash/) { next; }

    &DoGlobalSubs();

    # Change imagetype/gradients to shapes
    if (/<imagetype/) {
      my @imt = (), @shape = ();
      my $g = 0;
      push (@imt, $_);
      s/imagetype/shape/;
      push(@shape, $_);
      my ($ws) = (/^(\s+)/);
      push (@shape, "$ws<fill style=\"gradient\">\n");
      while(<IN>) {
#      print;
        if (/imagetype/) {
          push(@imt, $_);
          s/imagetype/shape/;
          push(@shape, "$ws</fill>\n");
          push(@shape, $_);
          last;
        };
        if (/gradient/) { $g = 1; }
        push (@imt, $_);
        push (@shape, $_);
        
      }
      if ($g == 1) { print OUT @shape; }
      else { print OUT @imt; }
      next;
    }
    
    # Look for 'font's that should be 'fontdef's
    $r =  s/<font name/<fontdef name/;
    print OUT $_;
    if ($r) {
      while(<IN>) {

        &DoGlobalSubs();
        $r = s!</font>!</fontdef>!;
        print OUT $_;
        if ($r) {
          last;
        }
      }
    }
  }    
  close IN;
  close OUT;
}
  
  
sub DoGlobalSubs {  
        # Bold changes to weight
        s!<bold>yes</bold>!<weight>bold</weight>!;
        s!<bold>no</bold>!<weight>normal</weight>!;
        s!></fill>! />!;
        s!dropcolor>!shadowcolor>!g;
        s!shadow>!shadowoffset>!g;
}
