#!/usr/bin/perl
#
# Make a list of JPG exposures with properties of each
#
# MWR 7/18/2020

use POSIX;

$debug = 0;

@files = glob("DSC*JPG");
foreach $file (@files) {

  if ($debug > 0) {
    printf " next is file %s \n", $file;
  }

  # grab the index number from the name
  $index = substr($file, 4, 4);
  if ($debug > 0) {
    printf " index is %4d \n", $index;
  }
 
  $cmd = "exif $file | grep 'Date and Time  '";
  $ret = exec_cmd($cmd);
  @words = split(/\s+/, $ret);
  if ($debug > 0) {
    printf "words[4] is ..%s.. \n", $words[4];
  }
  @times = split(/:/, $words[4]);
  $hour = $times[0];
  $minute = $times[1];

if (0 == 1) {
  # We need to subtract 35 minutes from the time in the EXIF
  $offset_minutes = 35;
  $total_minutes = 60*$hour + $minute;
  if ($debug > 0) {
    printf "  hour %02d minute %02d   total_minutes %3d \n", 
            $hour, $minute, $total_minutes;
  }
  $total_minutes -= $offset_minutes;
  $new_hour = floor($total_minutes / 60.0);
  $new_minute = $total_minutes - ($new_hour*60);
  if ($debug > 0) {
    printf "  old %02d %02d   new %02d %02d  \n", $hour, $minute, $new_hour, 
             $new_minute;
  }
}
else {
  $new_hour = $hour;
  $new_minute = $minute;
}


  # get the exposure time
  $cmd = "exif $file | grep 'Exposure Time '";
  $ret = exec_cmd($cmd);
  @words = split(/\s+/, $ret);
  if ($debug > 0) {
    printf "words[2] is ..%s.. \n", $words[2];
  }
  $exptime = $words[2];
  $exptime =~ s/\|//;
  if ($debug > 0) {
    printf " exptime is %.1f \n", $exptime;
  }

  # get the f/ setting (if present)
  $cmd = "exif $file | grep 'F-Number'";
  $ret = exec_cmd($cmd);
  @words = split(/\s+/, $ret);
  if ($debug > 0) {
    printf "words[1] is ..%s.. \n", $words[1];
  }
  $fratio = $words[1];
  $fratio =~ s/\|//;
  if ($debug > 0) {
    printf " fratio is %s \n", $fratio;
  }

  # get the focal length (if present)
  $cmd = "exif $file | grep 'Focal Length'";
  $ret = exec_cmd($cmd);
  @words = split(/\s+/, $ret);
  if ($debug > 0) {
    printf "words[2] is ..%s.. \n", $words[2];
  }
  $flen = $words[2];
  $flen =~ s/\|//;
  if ($debug > 0) {
    printf " flen is %s \n", $flen;
  }




  printf "%-15s index %4d    %02d:%02d  %5.1f  %s  %s \n", 
          $file, $index, $new_hour, $new_minute, $exptime, $flen, $fratio;
  
  
  



}


exit 0;





##############################################################################
# PROCEDURE: exec_cmd
#
# DESCRIPTION: Execute the given shell command line.  If the $debug flag
#              is set, we print to stdout the command line, and
#              also print out the result string.
#
# RETURNS:
#              the result of the command
#
#
sub exec_cmd {

  my($cmd, $ret);

  $cmd = $_[0];

  if ($debug > 0) {
    printf "cmd is ..$cmd.. \n";
  }
  $ret = `$cmd`;
  if ($debug > 0) {
    printf "ret is ..$ret.. \n";
  }

  return($ret);
}


