#!/usr/bin/perl -w
#
# mp4_creator - simple program to create movies
#
# by Jim Crumley 9/18/2007

  my $maker = "mencoder"; #"ffmpeg";

  
  print "mp4 Creator \n\n";
  print "This program makes a mp4 movie from images using $maker\n\n";

  my $file_dir = user_response( 
    "\nPlease enter the directory that the images are stored in. \n"
    ."Use '.' for the current directory.\n",1);

  my $list_one_by_one = user_response(
    "\nWould you like to enter each file name individually or enter a \n"
    ."pattern for the file names?  Enter:\n"
    ."  1 for individually (not yet implemented) or\n" 
    ."  2 for pattern.\n"
    ."Choose that if your files are named like 2 if your \n"
    ."file names are something like img_01.png, img_02.png, ..., \n"
    ."img_10.png.\n", 2, 2);

  if ($list_one_by_one eq 1) {
    print "Not implemented yet - choose 2.\n";  
  } elsif ($list_one_by_one eq 2) {
    $file_pattern = user_response(
    "\n Please enter the pattern for the files that you want use for the \n"
    ."movie. Use '*'s to replace any parts of the name that vary from file\n"
    ." to file. For example if your files are named img_01.png, img_02.png,\n"
    ."..., img_10.png, use img_*.png for your pattern.\n", 3, $file_dir)
  }
 
  chomp($file_pattern);
  $int_file_pattern = "/tmp/$file_pattern";
  #PROBLEM BELOW TODO
  $int_file_pattern =~ s/\*.*$/*.jpg/;

  @inp_files = `ls $dir/$file_pattern`;
  chomp (@inp_files);
  @int_files = @inp_files;
  #print "$int_file_pattern @int_files\n";
 
 
  my $output_file = user_response(
    "\nPlease enter the prefix for the output movie file.\n",
    4, $file_dir);
  $output_file = "$output_file.avi";

  #$movie_com ="ffmpeg -r 10 -b 1800 ";
  for ($i=0; $i < $#inp_files; $i++)  {
    #print "$int_files[$i]\n";
    if ($inp_files[$i] =~ /.eps$/) {
      $tfile ="$inp_files[$i]001.ppm";
      #print"$tfile\n";
      system("pstopnm $inp_files[$i]");
      $inp_files[$i]=$tfile;
    }
    $int_files[$i] =~ s/^.*\///;
    $int_files[$i] =~ s/\..*$/.jpg/;
    $int_files[$i]="/tmp/$int_files[$i]";
    print "$int_files[$i]\n";
    system("convert $inp_files[$i] $int_files[$i]");
    #print "$file \n";
    #print "$movie_com\n";
    #$movie_com = "$movie_com -i $file";
    system("/bin/rm $tfile");
  }

   $movie_com ="mencoder \"mf://$int_file_pattern\"  -mf fps=1 -o $output_file -ovc lavc -lavcopts vcodec=msmpeg4v2:vbitrate=800";
  #$movie_com = "$movie_com $output_file.mp4";
  print "$movie_com \n";
  system ($movie_com);
  #system ("/bin/rm @int_files");
#end of main

sub user_response {
# Subroutine for asking for user input
# Response types:
# 1 - existing directory
# 2 - mutltiple choice
# 3 - file pattern
# 4 - file to create
#
  $question = $_[0];
  $response_type = $_[1];
  my $answer = '';
  $done = '0';

  while (! ($done) ) {
    print ($question) ;
    $answer =<STDIN>;
    chomp($answer);
    if ($response_type eq 1) {
      #Individual File/directory choices
      $done = (-r $answer);
      if (! $done) {print "File/directory $answer does not exist.\n"};
    } elsif ($response_type eq 2) {
     #Numeric Menu choices
      $num_choices = $_[2];
      if (($answer ge 1 ) and ($answer le $num_choices)){$done =1};
    } elsif ($response_type eq 3) {
      #File pattern choices
      #if ($answer ne '') {$done = 1};
      $dir =  $_[2];
      print "\nThe following files match that pattern:\n";
      system("ls $dir/$answer");
      print ("\nAre those the files that you wanted? (y or n)\n");
      $answer2 =<STDIN>;
      chomp($answer2);
      if (($answer2 eq 'y') or ($answer2 eq 'Y')  or ($answer2 eq 'yes')
        or ($answer2 eq 'Yes')) {$done = 1};
    } elsif ($response_type eq 4) {
     if ($answer ne '') {$done=1};
     #$dir =  $_[2];
     #$done = (-w "$dir/$answer");
     # if (! $done) {
     #   print "File/directory $dir/$answer cannot be written by you. \n Try again.\n"};
    }

    #print ("$answer\n$done i\n");
  }
  return $answer;
}
