<?php

### these need to be set to the correct locations!!
$PERLLIB = "/home/kiawe/eugene/psconfig/dev.linux/lib";
$BINDIR = "/home/kiawe/eugene/psconfig/dev.linux/bin";
$SITE = "/home/kiawe/eugene/.ipprc";
$MISSING = "missing.png";

// load an image file from the image directory
// validate request
if ($_SERVER[REQUEST_METHOD] != 'GET') { 
  exit ();
}

putenv("PERL5LIB=$PERLLIB:");
$PATH = getenv("PATH");
putenv("PATH=$BINDIR:$PATH");

# echo "args: $args<br>";

# $basename may contain filename@filerule
$basename = strtok($_GET[name],"@");
$filerule = strtok("@");

# echo "basename: $basename<br>";
# echo "filerule: $filerule<br>";

# need to supply the camera as well...

# if ($filerule) {
#   if ($filerule == "PPIMAGE.JPEG1") {
#     $basename = "$basename.b1.jpg";
#   }
#   if ($filerule == "PPIMAGE.JPEG2") {
#     $basename = "$basename.b2.jpg";
#   }
# }
# exec ("ipp_datapath.pl --site=$SITE $basename", $output, $status);

# do I need to do this to the output result as well?
$basename = escapeshellarg($basename);
$basename = str_replace ('..','',$basename);

exec ("ipp_filename.pl --site=$SITE --basename $basename --camera $camera --class_id $class_id", $output, $status);

$filename = $output[0];
if ($status) {
  $filename = $MISSING;
}

if (0) {
  echo "basename: $basename<br>";
  echo "output:   $output[0]<br>";
  echo "status:   $status<br>";
  echo "filename: $filename<br>";
  exit ();
}

$file = fopen ($filename, "r");
if ($file) {
  # do I need to use image/jpg? can I modify this to image/png, etc, based on the type?
  header ('Content-Type: image/jpg');
  fpassthru ($file);
}
exit ();
?>
