#!/usr/bin/perl -w
#
# (C) Mirko Sucker, 2000.
# Released under GPL.
#
# This script replaces all keywords found as elements in the hash
# @Contents embraced by dollar signs by the contents predefined in the
# hash.  It processes all files given at the command line.  The texts
# may not contain the dollar sign ($), as this is used to mark texts
# to replace. Example: $Author$ will be replaced by the text $ Author:
# <whatever you write below> $ The script is intended to be used in #
# C++ files, place a multiline comment around the macros.

# Some constants for clearer code:

$LicenseText= 
  "License: GPL with the following explicit addition:\n" .
  "         This code may be linked against any version of the Qt toolkit\n" .
  "         from Troll Tech, Norway.";
$ContactText=
  "Contact: mirko\@kde.org\n" .
  "         http://www.kde.org";

# This hash defines the keywords:

%Contents = 
  (
   "Author" => "Author: Mirko Sucker",
   "License" => $LicenseText,
   "Copyright" => "Copyright: (C) 2000, Mirko Sucker",
   "Contact" => $ContactText
  );

# ----- main part:
#       preparation:

$#ARGV+1>0 || die "Give filenames to search and replace keywords in!";

print "Using the following settings and keywords:\n";
foreach $key (keys %Contents)
  {
    print $key . " = " . $Contents{$key} . "\n";
  }

#       iterate over given files:

foreach $file(@ARGV)
  {
    print("Processing $file.\n");
    if(open(InFileHandle, "<".$file))
	{
	  # read complete file content into scalar text:
	  $text="";
	  @temp=<InFileHandle>;
	  foreach(@temp)
	    {
	      $text=$text . $_;
	    }
	  # start replacing:
	  foreach $key (keys %Contents)
	    {
	      $keyword=$key;
	      if($text=~ s/\$\s*$key\s*.*?\s*\$/\$ $Contents{$key} \$/s)
		{
		  print("  Found keyword $key.\n");
		}
	    }
	  close(InFileHandle);
	  if(open(OutFileHandle, ">".$file))
	    {
	      print(OutFileHandle $text);
	    } else {
	      print("  Cannot write $file: permission denied.");
	    }
	} else {
	  print("  File $file does not exist.\n");
	}
    print("Done processing $file.\n");
  }
