#!/usr/bin/perl # # Converts a index.txt file into image header comments # ######################### # $Id: comment2jhead 49 2003-02-08 22:02:13Z steve $ ######################### # $Log$ # Revision 1.2 2003/02/08 22:02:13 steve # now checks existing comments for need of updating # defaultly overwrites existing comments # # Revision 1.1 2003/01/25 19:00:39 steve # added # # ######################### use strict; use Getopt::Std; my ($version) = '$Revision: 49 $' =~ /Revision:\s*(.+)\$/; our ( $opt_h, $opt_V, $opt_v, $opt_n ); getopts("hVvn"); usage() if $opt_h; die "$0 version: $version\n" if $opt_V; usage() unless @ARGV; my $verbose = $opt_v; #### end init my $jhead = "jhead"; my $rdcom = "rdjpgcom"; my $wrcom = "wrjpgcom"; my $index_file = "index.txt"; ## parse jhead's version number #my ($jh_maj, $jh_min) = `$jhead -V` =~ /version:\s*(\d+)\.(\d+)\s/; # #warn "JHead version 1.9 or greater is required. \n" # if ( $jh_maj < 1 || $jh_min < 9 ); my %index; # what extensions to process my $process = qr/\.jpe?g$/i; die "$rdcom not found" unless `which $rdcom` =~ /\w/; die "$wrcom not found" unless `which $wrcom` =~ /\w/; die "no $index_file present. aborting.\n" unless -e $index_file; load_config( $index_file, \%index, {}); my $wd = $ENV{PWD}; my $no_overwrite = $opt_n; my $any_changes = 0; foreach my $file (@ARGV){ if( exists $index{$file} && $file =~ $process ){ if( -M "$wd/$index_file" < -M "$wd/$file" ){ print $file, "\n"; open HEAD, "-|", "$rdcom \"$file\"" or die "cannot run $rdcom: $!"; my $orig_comment = join "",
; close HEAD; chomp $orig_comment; if( $index{$file} eq $orig_comment){ print "not updating $file: no change in comment.\n" if $verbose; next; } print "adding comment to $file\n" if $verbose; my $tempfile =`tempfile`; chomp $tempfile; open TMP, ">$tempfile" or die "cannot create tempfile: $!"; print TMP $index{$file}; close TMP; my $replace = "-replace"; $replace = "" if $no_overwrite; # run the command to add the comments (system( "$wrcom $replace -cfile $tempfile $file > $file.tmp" ) == 0) or die "\n"; # make sure the file actually was made unless( -e "$file.tmp" && -s "$file.tmp" ){ warn "error writing tempfile: $file.tmp. not touching $file\n"; next; } rename( "$file.tmp", $file ) || (warn("cannot rename file: $!\n") && unlink "$file.tmp"); $any_changes = 1; unlink $tempfile; }else{ print "not updating $file: newer than $index_file.\n" if $verbose; } } } print "no changes made.\n" unless $any_changes; sub load_config{ my ($cfgfile, $index, $options) = @_; $cfgfile =~ s/^[\>\<\|]//g; if(-e $cfgfile && open INDEX, $cfgfile){ my $file = ""; foreach my $line(