<?



echo " <FORM METHOD=\"GET\" ACTION=\"$PHP_SELF\">\n";

echo "<TABLE WIDTH=\"800\" BORDER=\"1\" CELLSPACING=\"1\" CELLPADDING=\"5\" ALIGN=\"center\">\n";

echo "<TR><TH COLSPAN=\"2\">DIRECTOR</TH></TR>\n";


// SETUP UP DIRECTOR
$ncreators=sizeof($movie);
echo "<TR>\n";
echo "<TD COLSPAN=\"2\">";
echo "Director command :";
echo "<SELECT name=\"director\">\n";
for ($i=1; $i <= $ncreators; $i++) {
  $name=$movie[$i]['name'];
  if ($director==$i) { $S="selected"; } else { $S="";}
  echo " <OPTION $S value=\"$i\">$name\n";
}
echo "</SELECT>\n<BR>\n";

echo "output dir : $outdir\n<BR>";

// CHOOSE DELAY
if (isset($delay)==0) { $delay=1;}
echo "Delay per frame : <INPUT TYPE=txt name=\"delay\" VALUE=\"$delay\"><BR>\n";
echo "</TD>";
echo "</TR>\n";

// CHOOSE FRAMES FOR MOVIE
echo "<TR>";
echo "<TH WIDTH=\"50%\">Last N hours</TH><TH WIDTH=\"50%\">Last N Frames</TH>";
echo "</TR>\n";

echo "<TR>";
echo "<TD >\n";
echo "Hours : <INPUT TYPE=text name=\"hours\" value=\"$hours\"> &nbsp\n";
echo "<INPUT TYPE=\"submit\" NAME=\"create\" VALUE=\"hours\" >\n";

echo "\n</TD>";

echo "<TD>";
echo "Frames : <INPUT TYPE=text name=\"frames\" value=\"$frames\"> &nbsp\n";
echo "<INPUT TYPE=\"submit\" NAME=\"create\" VALUE=\"frames\" ><BR>\n";
if (!isset($timestamp)) { $timestamp=date("YmdHis"); };
echo "TimeStamp ref : <INPUT TYPE=txt name=\"timestamp\" value=\"$timestamp\"><BR>\n";
echo "</TD>";
echo "</TR>\n";



echo "</TABLE>";
echo "</FORM>";

// CREATE MOVIE
if (isset($create)) {

  echo "<TABLE WIDTH=\"800\">\n";
  echo "<TR><TD>\n";

  if ($create=="hours") {
    $n=$hours;
    $sec=3600*$hours;
    $sql="SELECT *,(UNIX_TIMESTAMP()-UNIX_TIMESTAMP(timestamp)) as time FROM images WHERE (UNIX_TIMESTAMP()-UNIX_TIMESTAMP(timestamp)<$sec) ORDER BY timestamp DESC";
    //echo "<BR>$sql<BR>";
    echo "Creating movie of the last $n $create, $nresults<P>\n";
  } elseif ($create=="frames") {
    $n=$frames;
    $sql="SELECT * FROM images WHERE timestamp<$timestamp ORDER BY timestamp DESC LIMIT 0,$n";
    //echo "<BR>$sql<BR>";
    echo "Creating movie of the last $n $create, $nresults<P>\n";
  }



  // MKDIR TMP DIR
  $tmpdir="$outdir/director";

  // WRITE IMAGES  
  if (!mkdir($tmpdir)) {
    echo "DIRECTORY COULD NOT BE CREATED\nPOSSIBLY IT ALLREADY EXIST<BR>";
  }

  $result = mysql_query($sql,$db);
  $nresults= mysql_affected_rows();
  
  

  if ($myrow = mysql_fetch_array($result)) {
    $i=0;
    do {
      $i++;
      $image=$myrow["image"];
      $timestamp=$myrow["timestamp"];
      $tmpfile=sprintf("$tmpdir/%04d.jpeg",$nresults-$i+1);
      echo "frame $i, $timestamp - $tmpfile<BR>\n";

      fwrite(fopen($tmpfile,"w"),($image));
    } while ($myrow = mysql_fetch_array($result));
  }


  // CREATE MOVIE
  if (isset($director)==1) {

    $moviefile=sprintf("$outdir/movie_%s_%s_%s",$create,$n,date("YMd"));

    $cmd=$movie[$director]['cmd'];
    $format=$movie[$director]['format'];
    $converttogif=$movie[$director]['converttogif'];

    $cmd = ereg_replace ("DELAY", $delay, $cmd);
    $cmd = ereg_replace ("MOVIEFILE", $moviefile, $cmd);
    $cmd = ereg_replace ("FORMAT", $format, $cmd);
    $cmd = ereg_replace ("TMPDIR", $tmpdir, $cmd);
    //echo "<BR>$cmd";

    if ($converttogif==1) {
      $cmd_to_gif="$mogrify -format gif $tmpdir/*.jpeg ";
      echo "<B>Converting to GIF</B></BR>\n";
      system($cmd_to_gif);      
    }

    echo "<B>Creating movie</B><BR>\n";
    system($cmd);      

    echo "<H3>Movie should be created in $moviefile.$format </H3>";

  }
  
  // REMOVE THE TEMPORARY DIR
  system("rm -fr $tmpdir");


  echo "</TD><TR>\n";

  echo "</TABLE>\n";

}


?>
