#!/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(){ # file description if( $line =~/^\s*\:(.+)/o ){ $file = $1 } #comment elsif( $line =~/^\s*\#/o ){ } # action elsif( $line =~/^\s*\@(\w+)\s*(.+)\s*(\#.+)?$/o ){ my $action = $1; my $param = $2; if( $action eq "load" ){ load_config( actual_file($param), $index, $options ); } } # variable elsif( $line =~/^\s*\$(\w+)\s*=\s*(.+)\s*(\#.+)?$/o ){ $options->{$1} = $2 } else { $index->{$file}.= $line } } close INDEX; } } #### helper subs sub usage(){ die < Licensed under the GNU GPL. See documentation for complete details. USAGE } __END__ =head1 NAME comment2jhead - adds comments to jpgs from the index.txt file. See dirinfo for more information =head1 SYNOPSIS B S<[ B<-n> B<-v> B<-h> B<-V> ]> I =head1 DESCRIPTION C adds comments to jpgs from the index.txt file. See dirinfo for more information =head1 OPTIONS =over 8 =item B<-h> This help =back =head1 ENVIRONMENT No environment variables are used. =head1 AUTHOR Steve Pomeroy http://staticfree.info/ =head1 LICENSE Copyright © 2003 Steve Pomeroy This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA =head1 SEE ALSO perl(1) =head1 BUGS none known. =cut