#!/usr/local/bin/perl -w
###########################################
# pounce-yml-to-xml
# Mike Schilli, 2008 (m@perlmeister.com)
###########################################
use strict;
use Sysadm::Install qw(:all);

use YAML qw(LoadFile);
use Template;

my($home) = glob "~";
my $path = "$home/.pidgin-pounce";
my $yaml = LoadFile( "$path/pounce.yml" );

my $xml_file = "$home/.purple/pounces.xml";
my $SOUND_CMD = "~/bin/pounce-sound";

my @pounces = ();

for my $account (
        keys %{ $yaml->{sound_map} }) {

  my($proto, $recv) = split /:/, $account;

  for my $buddy (keys %{ 
      $yaml->{sound_map}->{$account} }) {

    push @pounces,
      mk_pounce($buddy, $recv, $proto);
  }
}

binmode STDOUT, ":utf8";

my $xml = q{
<?xml version='1.0' encoding='UTF-8' ?>
<pounces version='1.0'>
[% FOR pounce IN pounces %]
  [% pounce %]
[% END %]
</pounces>
};

my $tmpl = Template->new();

mv $xml_file, "$xml_file.old" if
   -f $xml_file;

$tmpl->process( \$xml, 
  { pounces => \@pounces }, 
  $xml_file ) or die $tmpl->error;

###########################################
sub mk_pounce {
###########################################
    my($buddy, $recv, $prot) = @_;

    return qq{
<pounce ui='gtk-gaim'>
  <account 
 protocol='prpl-$prot'>$recv</account>
  <pouncee>$buddy</pouncee>
  <options/>
  <events>
    <event type='sign-on'/>
    <event type='message-received'/>
  </events>
  <actions>
    <action type='execute-command'>
      <param name='command'
     >$SOUND_CMD $prot:$recv $buddy</param>
    </action>
  </actions>
  <save/>
</pounce>
    }
}
