#!/usr/bin/perl -w

use strict;
use warnings;

use XML::Simple;
use CGI qw/:standard/;

my $query = new CGI;
my $videoid = $query->param("videoId");
my $playlist = $query->param("playlist");

my $metainfo = "/home/ftp/pub/video/frikanalen/meta.xml";
my $ref = XMLin($metainfo);

my $id = "id_$videoid";
if (! exists  $ref->{$id} ) {
    print STDERR "Unable to look up $id\n";
}
my $title = $ref->{$id}->{Title};
my $description = $ref->{$id}->{Description};
my $length = int($ref->{$id}->{Length});
my $imageuri = "thumbs/$videoid.jpg";
my $ogvurl = $ref->{$id}->{ogvUri};
my $wmvurl = $ref->{$id}->{VideoUri};
my $playlisturl = url(-relative=>1) . "?playlist=$videoid.m3u" ;

if ($playlist) {
    $playlist =~ /(^.+)\.m3u/;
    my $id = "id_$1";
    my $ogvurl = $ref->{$id}->{ogvUri};
    print "Content-Length: ", length($ogvurl)+1, "\n";
    print "Content-type: audio/x-mpegurl\n\n";
    print "$ogvurl\n";  
    exit 0;
} else {

print "Content-type: text/html\n\n";

print <<"EOF";
<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
  
  <title>Video test</title>
  <link rel="stylesheet" href="/nuug.css" type="text/css">
  <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
<h1>$title</h1>
<p>$description</p>
<p><video src="$ogvurl">
<source src="$wmvurl">
    <object
    data="$playlisturl"
    type="audio/x-mpegurl"
    border="1"
    width="640"
    height="320">
    <param name="src" value="$playlisturl">
    <param name="autoplay" value="true">
    <param name="autoStart" value="1">
    <APPLET code="com.fluendo.player.Cortado.class"
	  archive="http://www.nuug.no/tools/cortado-unsigned.jar"
          width="640" height="320">
  	  <PARAM name="url" value="$ogvurl"/>
  	  <PARAM name="local" value="false"/>
    	  <PARAM name="keepaspect" value="true"/>
    </APPLET>
    </object>
  </video>

<p>Video URLs:
  <br><a href="$ogvurl">Ogg Theora</a>
  <br><a href="$wmvurl">Windows Media</a>
</p>
</body>
</html>
EOF
}
