#!/usr/bin/perl -w
# pcd2html module pcd2html_create_jpg
# Copyright Andreas Tille <tille@debian.org>
# License: GPL
#
# This modul creates the JPG images and Icons for these images
use pcd2html_uti;
use strict;

my $debug = 0;

my ( $key, $dir, %dirs, $license, $source,
     %item, %keys,
     $num, $name, $image, $quality,
     $O, $Q, $E, $WikiPediaLink ) ;
unless ( $ARGV[0] ) { die "Usage: pcd2html_create_jpg <image_info_file>" ; };

%keys = OpenRules();

%item = GetTranslationItems("eng") ;

$_ = $ARGV[0] ;
/^(\w*)_(\w*)\.\w{3,4}$/ ;
my $pickey = $1 ;
my $picnum = $2 ;

($key, $source) = GetKeyAndSource($pickey, $picnum, $ARGV[0]);

($num, $name, $image, $O, $Q, $E, $WikiPediaLink ) = GetParametersFromRules($pickey, $picnum) ;

my $icon = "$num.jpg" ;

if ( CheckChanges($ARGV[0], $ARGV[0], $O, $E, $Q) == 0 ) {
  system "touch", "$num.jpg" ;
  print "Keine Änderungen für $num.jpg.\n" ;
  exit 0;
}

if ( ! ( defined ( $source ) || $picnum =~ /extra/ ) ) {
  print STDERR "Please insert Photo CD with key $pickey to create image ".$pickey."_"."$picnum.jpg.\n" ;
  exit -1 ;
}

if ( $E ne "" ) { $source = "$source\[$E\]"; } 
$quality = GetQuality($Q) ;

print "   Creating $image ...\n" ;

if ( open(IMG, ">$ARGV[0]" ) ) {
    my $internal_info_key = GetInternalInfoKey();
    print IMG "<!-- $internal_info_key: O=$O, E=$E, Q=$Q -->\n" ;
    close IMG ;
}

my ( $cmdstring );
if ( $picnum =~ /extra/ ) {
  my ( $tmp_icon ) ;
  system "${key}_extra" ;
  $tmp_icon = ExistingPicWithName ( "${key}_extra" ) ;
  unless ( $tmp_icon ) {
    # Since ImageMagick 6.0.3 convert uses a new syntax 
    # system GetIconConvert() . " $image $icon 2>> $ARGV[0]" ;
    $cmdstring = GetIconConvert($image) . " $icon 2>> $ARGV[0]" ;
    if ($debug) { print STDERR "DEBUG (jpg/62): $cmdstring\n" ; }
    system $cmdstring ;
  }
} else {
  # Since ImageMagick 6.0.3 convert uses a new syntax 
  # system GetConvert() . " $quality $O $source $image 2>> $ARGV[0]" ;
  # system GetIconConvert() . " $image $icon 2>> $ARGV[0]" ;
  $cmdstring =  GetConvert($source) . " $quality $O $image 2>> $ARGV[0]" ;
  if ($debug) { print STDERR "DEBUG (jpg/70): $cmdstring\n" ; }
  system $cmdstring ;
  $cmdstring = GetIconConvert($image) . " $icon 2>> $ARGV[0]" ;
  if ($debug) { print STDERR "DEBUG (jpg/73): $cmdstring\n" ; }
  system $cmdstring ;
}

