# Album Plugin:	captions/format/audio
# For info:	'album -plugin_info captions/format/audio'
# For usage:	'album -plugin_usage captions/format/audio'
use strict;
use File::Copy;

my $LICENSE = << 'LICENSE';
Copyright (c) 2005 Scott J. Bertin

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
LICENSE

my $DESCRIPTION = << 'DESCRIPTION';
Adds audio to image pages.

If you have an image named pict1234.jpg (for example), this plugin will 
find pict1234.wav and allow it to be played on the image page. It will 
look for audio files in wav, aiff, au, and mid format.
DESCRIPTION

my $SOUND_TYPES = "aif|AIF|aiff|AIFF|au|AU|mid|MID|wav|WAV";

my @ALIGNMENTS = ('top', 'bottom', 'center', 'baseline', 'left', 'right',
                  'texttop', 'middle', 'absmiddle', 'absbottom');

sub start_plugin {
  my ($opt) = @_;

  # Setup the options
  album::add_option(1, 'show_license', album::OPTION_BOOL, one_time=>1,
                    usage=>'Show the license for this plugin.');
  album::add_option(1, 'embed', album::OPTION_BOOL, default=>1,
                    usage=>'Embed the sound in the page');
  album::add_option(2, 'width', album::OPTION_NUM, default=>200,
                    usage=>'Width of sound control (-1 to hide)');
  album::add_option(2, 'height', album::OPTION_NUM, default=>45,
                    usage=>'Height of sound control (-1 to hide)');
  album::add_option(2, 'align', album::OPTION_STR, default=>'bottom',
                    usage=>'Location sound control ('.join(",", @ALIGNMENTS).')');
  album::add_option(2, 'autostart', album::OPTION_BOOL, default=>1,
                    usage=>'Automatically start playing');
  album::add_option(2, 'text', album::OPTION_STR, default=>'Play sound',
                    usage=>'Text for noembed link');
  # Setup the hooks
  album::hook($opt, 'end_handle_file', \&add_sound);

  return {
    author => 'Scott J. Bertin',
    href => 'mailto://scottbertin@yahoo.com',
    version => '1.0',
    description => $DESCRIPTION,
  };
}

sub add_sound {
  my ($opt, $data, $hookname, $dir, $pic, $cap) = @_;
  
  # Check the options
  show_license($opt) if album::option($opt, 'show_license');
  my $slash = album::option($opt, 'slash');
  my $width = album::option($opt, 'width');
  my $height = album::option($opt, 'height');
  my $align = album::option($opt, 'align');
  my $autostart = album::option($opt, 'autostart');
  my $text = album::option($opt, 'text');
  my $embed = album::option($opt, 'embed');
  
	my $me = album::curr_plugin($opt);
  album::usage("Unknown ${me}:align option: $align\n".
               "\tShould be one of: ". join(",", @ALIGNMENTS))
    unless !$width || !$height || grep(uc($align), @ALIGNMENTS);
  
  my $obj = $data->{obj}{$pic};
  my $basename = $data->{obj}{$pic}{full}{path};
  $basename =~ s/[^\.]+$//;
  foreach my $ext (split(/\|/,$SOUND_TYPES)) {
    if (-r $basename.$ext) {
      my $tn = $dir.$slash.album::option($opt, 'dir');
      # Windows Media Player won't use paths containing ".."
      #my $snd = album::url_quote($opt,
      #                           album::diff_path($opt, $tn, $basename.$ext));
      $data->{obj}{$pic}{full}{file} =~ /^(.+\.)[^\.]+$/;
      my $snd = $1.$ext;
      copy($basename.$ext, $tn.$slash.$snd)
        unless -e $tn.$slash.$snd || link($basename.$ext, $tn.$slash.$snd);
      $snd = album::url_quote($opt,$snd);
      if ($embed) {
        $obj->{cap_image} .= '<br><embed src='.$snd;
        $obj->{cap_image} .= ' width='.album::url_quote($opt,$width) if $width > 0 && $height >= 0;
        $obj->{cap_image} .= ' height='.album::url_quote($opt,$height) if $width >= 0 && $height > 0;
        $obj->{cap_image} .= ' hidden="true"' unless $width >= 0 && $height >= 0;
        $obj->{cap_image} .= ' align='.album::url_quote($opt,$align) if $align && $width && $height;
        $obj->{cap_image} .= ' autostart="true"' if $autostart;
        $obj->{cap_image} .= '><br>';
        $obj->{cap_image} .= '<noembed>' if $text;
      } else {
        $obj->{cap_image} .= '<br>';
      }
      $obj->{cap_image} .= '<a href='.$snd.'>'.$text.'</a><br>' if $text;
      $obj->{cap_image} .= '</noembed>' if $text && $embed;
      # Only add one sound
      last;
    }
  }
  
  return 0;	# Don't skip the file!
}

sub show_license {
  my ($opt) = @_;
  
	my $me = album::curr_plugin($opt);
  print "The following license applies ONLY to the ",
        "$me plugin, and should not be construed ",
        "to apply to album, or any other plugin.\n\n", $LICENSE;
  exit;
}

# Plugins always end with:
1;
