# Album Plugin: media/flv
# For info:     'album -plugin_info media/flv'
# For usage:    'album -plugin_usage media/flv'
# For license:  'album -media/flv:show_license'
#
# Uses flv viewer from http://www.jeroenwijering.com/?item=JW_FLV_Media_Player
use strict;
use File::Copy;

# Plugin files to copy to the album
my @SOURCE = qw(player.swf swfobject.js yt.swf);
# Eventually we may want to make sure that plugin files are somewhere
# web visible, but for now this will do.  On UNIX we'll use symlink anyways.

my $LICENSE = << 'LICENSE';
Plugin is copyright (c) 2008 David Madison

Plugin plays FLV files in your album using the JW Player from:

  http://www.jeroenwijering.com/?item=JW_FLV_Media_Player

The JW Players are licensed under a Creative Commons license. It
allows you to use, modify and redistribute the script for
noncommercial purposes. For all other use, buy a commercial license.

You must buy a commercial license if:

   1. Your site has any ads (AdSense, display banners, etc.)
   2. You want to remove the players' attribution (the right-click link)
   3. You are a corporation (governmental or nonprofit use is free)

Full license information for JW Player can be found at:

http://creativecommons.org/licenses/by-nc-sa/3.0/

LICENSE

my $DESCRIPTION = << 'DESCRIPTION';
Uses JW Player and ffmpeg to include FLV videos in albums.

ffmpeg is used for making the snapshot file (tn/file.snap.flv.jpg).
Unfortunately it doesn't work on all flv files, you may have to
make the snapshot manually (it should be the same size as the video).

JW Player is released under a non-commercial license, please see:

  % album -media/flv:show_license

The JW Player is copied into each album directory using symlinks
if your system supports symlinks.  If; however, your web server
will not follow symlinks outside of your web tree, you can do:

  % album -media/flv:no_symlink

DESCRIPTION

# Does this OS have symlinks?
my $HAS_SYMLINK = eval { symlink("",""); 1; };

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

  # Setup the options
  album::add_option(1, 'show_license', \&show_license, one_time=>1,
                    usage=>'Show the license for this plugin.');
  album::add_option(1, 'symlink', album::OPTION_BOOL, default=>$HAS_SYMLINK,
                    usage=>'Use symlinks for linking the plugin source into the album');

  # Setup the hooks
  album::hook($opt,'is_movie', \&is_flv);
  album::hook($opt,'is_image', \&is_flv);
  album::hook($opt,'end_handle_file', \&embedFLV);

  return {
    author => 'David Madison and Jeroen Wijering',
    href => 'http://MarginalHacks.com/',
    version => '1.0',
    description => $DESCRIPTION,
  };
}


sub is_flv {
	my ($opt,$hook,$pic) = @_;
	$pic =~ /\.flv$/i ? 1 : undef;
}

sub copyPlayer {
	my ($opt,$dir) = @_;

	my $path = $PATH;
	$path =~ s/\.alp$//;

	foreach my $source ( @SOURCE ) {
		my $from = "$path/$source";
		my $to = "$dir/$source";
		next if !album::option_changed($opt,'symlink') && -f $to && (-M $to <= -M $from);
		unlink($to);	# In case the option changed
		next if album::option($opt,'symlink') && symlink($from,$to);
		next if copy($from,$to);
	}
}

sub embedFLV {
	my ($opt,$data,$hook,$dir,$pic,$obj,$path) = @_;
	return 0 unless is_flv($opt,$hook,$pic);

	copyPlayer($opt,"$dir/$opt->{dir}");

	# We need the snapshot size
	album::get_size($opt,'snapshot',$obj);

	# Player parameters
	my $x = $obj->{snapshot}{x};
	my $y = $obj->{snapshot}{y};
	my $snapshot = $obj->{snapshot}{file};
	my $video = $obj->{URL}{image_page}{image};
	# Kludge: remove quotes around video
	$video =~ s/^['"](.+)['"]$/$1/;

	$obj->{full}{img_tag} = <<EMBED;
  <div id="container"><a href="http://www.macromedia.com/go/getflashplayer">Get the Flash Player</a> to see this player.</div>
  <script type="text/javascript" src="swfobject.js"></script>
  <script type="text/javascript">
    var s1 = new SWFObject("player.swf","ply",$x,$y,"9","#FFFFFF");
    s1.addParam("allowfullscreen","true"); s1.addParam("allowscriptaccess","always");
    s1.addParam("flashvars","file=$video&image=$snapshot");
    s1.write("container");
  </script>
EMBED
	$obj->{medium}{img_tag} = $obj->{full}{img_tag};

	0;	# Don't skip
}


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;
