# Album Plugin: media/videoplayer
# For info:     'album -plugin_info media/videoplayer'
# For usage:    'album -plugin_usage media/videoplayer'
# For license:  'album -media/videoplayer:show_license'
#
# Uses jwplayer viewer from http://jwplayer.com/
use strict;
use File::Copy;

# Plugin files to copy to the album
my $PLAYER = 'mediaplayer.swf';
my @SOURCE = ($PLAYER);
# 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 video files in your album using the JW Player from:

  http://www.jwplayer.com/
	https://code.google.com/archive/p/youplayer/downloads

The JW Players were 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 JWPlayer/Mediaplayer to include videos with an embedded player 
in albums.

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

  % album -media/videoplayer: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/videoplayer: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_videoplayer_type);
  album::hook($opt,'is_image', \&is_videoplayer_type);
  album::hook($opt,'end_handle_file', \&embed_videoplayer);

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


sub is_videoplayer_type {
	my ($opt,$hook,$pic) = @_;
	$pic =~ /\.(flv|mp4|mpg|webm|mov|aac|m4a|f4a|mp3|ogg|oga)$/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 embed_videoplayer {
	my ($opt,$data,$hook,$dir,$pic,$obj,$path) = @_;
	return 0 unless is_videoplayer_type($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;
	<embed src="$PLAYER?file=$video" width="$x" height="$y" allowfullscreen="true" />
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;
