#!/store/bin/perl
#
# Search and replace params in HTML tags.  Change all lines to end with \r\n
#
# Author: Petter Reinholdtsen <pere@td.org.uit.no>
# Date:   1997-05-27
# Changed:1997-05-27

#$debug = 1;

require "find.pl";
use Getopt::Std;

sub usage {
    print "$0 [-c <configfile>|'<search-replace command>'] <paths>\n";
    print " -c	Change default config-file\n\n";
    print "Command format: <TAG>.<PARAM> {<from>-><to>|-}\n";
    print " If <from> is '*', the param will be inserted where necessary.\n";
    print " If command is '-', the param is removed.\n\n";
}

if (! getopts('c:') || ! @ARGV) {
    usage();
    exit 1;
}

if ($opt_c) {
    open(CFILE, "<$opt_c") || die "Unable to open $opt_c for reading";
    while (<CFILE>) {
	next if (/^\#/);
	push(@commands,msdosChompLine($_));
    }
    close(CFILE);
} else {
    @commands = (shift(@ARGV));
}

umask 002;

&find(@ARGV);

sub wanted {
    return if ( ! /\.s?html?$/i || ! -r "$dir/$_" );
    $filename = "$dir/$_";
    print "Prosessing $filename\n";
    $oldtime = (stat("$filename"))[10]; # save change-time to compare before saveing
    if (! open(FILE, "<$filename") ) {
	warn "Unable to open $filename for reading. Nothing changed.";
	return;
    }
    foreach $line (<FILE>) {
	push(@html, $line);
	$line = msdosChompLine($line);
	foreach $command (@commands) {
	    local($tag, $param, $cmd) = $command =~ m/^(\w+)\.(\w+) (.+)$/;
	    if ( $line =~ /\<$tag[^\>]*\>/i ) {
		print "L: \"$line\"\nC: $tag $param $cmd\n" if ($debug);
		if ($cmd eq "-") { # Remove the param
		    $line =~ s/(\<$tag[^\>]*) $param\s*=\s*\"?[^\" ]+\"?([ >])/\1\2/i;
		} elsif ($cmd =~ /\-\>/) { # change param
		    ($from, $to) = $cmd =~ m/^(.+)-\>(.+)$/;
		    print "$tag.$param $from -> $to\n" if ($debug);

		    if ( $line =~
			m/^(.*\<$tag[^\>]* )($param\s*=\s*\"?)([^\" >]+)(\"?.*)$/i ) {
			print "Param hit: \"$3\"\n" if ($debug);
			# param exist - change it
			if ($from eq '*' || $from eq $3) {
			    $line = "$1\U$2"."$to$4";
			    print "Replaced\n" if ($debug);
			}
		    } elsif ($from eq '*') {
			# new param in tag - insert it
			$line =~ m/^(.*\<$tag[^\>]+)(\>.*)$/i;
			$line = "$1 \U$param=\""."$to\"$2";
			print "Inserted\n" if ($debug);
		    } # else nothing is done
		}
		# Pack and remove redundand space inside tags
		$line =~ s/(\<[^\>]+) \s+([^\>]*>)/\1 \2/;
		$line =~ s/\s+\>/\>/;
		print "L2: \"$line\"\n" if ($debug);
	    }
	}
	push(@newhtml, "$line\r\n");
    }
    close(FILE);

    # Save backup and replace
    if ($oldtime == (stat("$filename"))[10]) { # Not changed while we prossessed
	print @newhtml if ($debug);
	local($backupname) = "$filename.backup";##-$$";
	if (open(BACKUP, ">$backupname") ) {
	    if (print BACKUP @html) { # backup saved - replace original
		close(BACKUP);
		if ( open(FILE, ">$filename") ) {
		    if (print FILE @newhtml) { # Original replaced - all OK
			close(FILE);
		    } else {
			close(FILE);
			warn "Unable to write to $filename.  Probably damage - check backup $backupname."
		    }
		} else {
		    warn "Unable to open $filename for writeing.  Possible damage! - check backup $backupname";
		}
	    } else {
		close(BACKUP);
		unlink $backupname;
		warn "Unable to save backup for $filename.  Nothing changed.";
	    }
	} else {
	    warn "Unable to open backupfile $filename for writeing.  Nothing changed.";
	}
    }
}

sub msdosChompLine {
    local($line) = @_;
    local($tmp) = $/;
    $/ = "\n";
    chomp($line);
    $/ = "\r";
    chomp($line);
    $/ = $tmp;
    return $line;
}
