#!/usr/bin/perl -w
#
# Announce a move from Crafty using some soundfiles
#

# The program used for Sound Output
my $playprg   = "/usr/bin/play";

# Where the sound files are located
my $soundpath = "/usr/share/crafty/sound";
# Which language to use (each supported language is in a separate
# subdir of $soundpath)
my $language  = "en";

# The move sent by crafty
my $move      = $ARGV[0];

# Set soundpath to the correct language
$soundpath = $soundpath . "/" . $language;

%piece = (
  'B' => 'Bishop',
  'K' => 'King',
  'N' => 'Knight',
  'Q' => 'Queen',
  'R' => 'Rook'
);


%moves = (
  'O-O'        => '0-0',
  'O-O-O'      => '0-0-0',
  'Stalemate'  => 'Stalemate',
  'Drawaccept' => 'Drawaccept',
  'Drawoffer'  => 'Drawoffer',
  'Resign'     => 'Resign'
);

# First check some specials

if($moves{$move}) {
  system("$playprg $soundpath/$moves{$move}.wav");
}
# Handle all normal moves. All that needs to be done is announce
# each character sent by crafty alone. Set some pause beteween each
# char for clearer understanding.
#
# NOTE 1: Crafty uses short notation, so short notation is
# announced. For long notation announcements the crafty engine has
# to be hacked and pgn output as well as logging would get broken.
#
# NOTE 2: There has to exist a sound file named for each row,
# column and piece. That is there has to be sound files like a.wav,
# 1.wav, R.wav and so on. One can easily rename the files
# distributed for free eg. at the "Arena" Website
# (http://www.playwitharena.com). Soundfiles from Fritz are not
# suitable, at the moment, but the script can easily be rewritten
# to handle them as well.
else {
  for (my $i=0; $i<length($move); $i++) {
    my $char = substr $move, $i, 1;

    if($i == 0 && $char =~ /[a-h]/) {
      system("$playprg $soundpath/Pawn.wav");
    }

    if ($char =~ /[^=]/) {
      if ($char =~ /x/) {
        system("$playprg $soundpath/takes.wav");
        system("usleep 400000");
      }
      elsif ($char =~ /#/) {
        system("$playprg $soundpath/Checkmate.wav");
      }
      elsif ($char =~ /\+/) {
        system("$playprg $soundpath/Check.wav");
      }
      elsif ($char =~ /[1-8]/) {
        system("$playprg $soundpath/${char}from.wav");
      }
      elsif ($char =~ /[a-h]/) {
        system("$playprg $soundpath/\U${char}\Efrom.wav");
      }
      elsif ($char =~ /[QKNBR]/) {
        system("$playprg $soundpath/$piece{$char}.wav");
      }
      else {
        system("$playprg $soundpath/$char".".wav");
      }
    }
  }

  system("$playprg $soundpath/click.wav");

}
