#!/usr/local/bin/perl

# -----------------------------------------------------------------
#
# 'makealbum_name_tags.pl'
#
# A script to modify the content of "name" tags in HTML files.
# This script is primarily written for use with 'makealbum.sh'.
#
# -----------------------------------------------------------------
# This program is free software; you can redistribute it and/or
# modify it under the terms of the version 2 of the GNU General
# Public License as published by the Free Software Foundation.
#
# 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.
# -----------------------------------------------------------------
#
# Copyright (c) 2006 by Joerg Hau
#
# Author:   Joerg Hau
# e-mail:   joerg.hau <at> dplanet.ch
#
# Revision History:
#   2006-10-17, first working version (JHa)
# -----------------------------------------------------------------

use strict;
use warnings;

# -----------------------------------------------------------------
# Print usage mode
# -----------------------------------------------------------------
sub usage
{
print <<EOF
$0 - Changes the content of 'name' tags in HTML files.

Copyright (c) 2006 Joerg Hau <joerg.hau(at)dplanet.ch>.

This program is free software; you can redistribute it and-or
modify it under the terms of version 2 of the GNU General Public
License as published by the Free Software Foundation.

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.

Usage:  $0 annotation_file html_file

EOF
}

# -----------------------------------------------------------------
# check command line args
# -----------------------------------------------------------------
my $numArgs = $#ARGV + 1;
unless ($numArgs==2) {
    usage;
    die "Error: I need exactly 2 arguments.\n"
    }

my ($desc, $html)=@ARGV;
my ($line, $desc_line);

# -----------------------------------------------------------------
# read description file $desc into array @desc
# -----------------------------------------------------------------
open DESC, "$desc" or die "Error, can't open '$desc': $!";
my @desc = <DESC>;
close (DESC);

# -----------------------------------------------------------------
# read html file $html into array @html
# -----------------------------------------------------------------
open HTML, "$html" or die "Error, can't open '$html': $!";
my @html = <HTML>;
close (HTML);


# -----------------------------------------------------------------
# search/replace in a loop over all lines in @html _and_ @desc
# desc lines are like this:
#
#    20060922T123810 Col du Grand St-Bernard
#
# - the item up to the first whitespace is the text to search for
# - the rest of the line is the replacement text
# -----------------------------------------------------------------
foreach $desc_line (@desc)
    {
    $desc_line  =~/(\S+)\s+(.+)/;
    my $search  = $1;
    my $replace = $2;
    print "Search : ", $search, "\t", "replace: ", $replace, "\n";
    foreach $line (@html)
        {
        # important: the strings inside the HTML document are in '"' !
        $line =~ s/"$search"/"$replace"/gi;
        }
    }

# -----------------------------------------------------------------
# Open the (same) HTML file, and overwrite with the modified stuff
# -----------------------------------------------------------------
open HTML, ">$html" or die "Error, can't open '$html': $!";
foreach $line (@html)
   {
   print HTML "$line";
   }
close (HTML);

1;
