#!/usr/bin/perl -w 
#################################################################
# Script name:  subout.pl
# Author(s):    Marc Elliot Hall <marc@hallmarc.net>
# Version:      0.2.5
# Created:      2005-ish   0.1.1	Just searched-n-replaced text 
# Modified:     2010-01-08 0.1.2	Bugfixes 
#               2010-01-17 0.2.1	Added debugging
#               2010-01-18 0.2.3	Added timestamp preservation 
#               2010-01-19 0.2.5	Cleaned up cruft 
#
# Description:  Script to manipulate the contents of text files while 
#		leaving their datestamps intact. 
#
# Usage:        Modify the regexp and paths to reflect what you
#               prefer. Accepts one or more arguments, the name(s) of 
#		the file(s) you want to modify.
#
#               e.g., $> /usr/local/scripts/subout.pl ./foo/*.txt
#
#               The files can be wildcarded or simply named one at,
#               a time, provided the filesnames have no spaces in them.
#
# Notes:        Needs flags to preserve or not preserve timestamps
#			(Getopt::Std or Getopt::Long).
#		Needs to support filenames with whitespaces.
#		Needs systemcalls replaced with native Perl constructs.
#		Needs to accept the old and new strings from the commandline.
#			(Getopt::Std or Getopt::Long again).
#
# FIXME: 	doesn't gracefully handle filenames with spaces in them!
#
# License:      Made available under GNU GPL v 2.0 as found here:
#               http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
#               (or a later version at the original author's
#               discretion)
#
#
#################################################################

unless (@ARGV >= "1") {die "usage: $0 <list of files> (wildcards OK)\n"}

# Variables
#@files = glob("*") ;
@files = (@ARGV) ;

my $DEBUG = "1" ;

my $oldstring = "other other" ;
my $newstring = "or other" ;

#my $newstring = "frobbit" ;
#my $oldstring = "dagnabbit!" ;

foreach $file (@files) {
	print "$file is a file\n" if $DEBUG > 1 ;
	my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat($file);
	print "dev is $dev\n" if $DEBUG > 1 ;
	print "ino is $ino\n" if $DEBUG > 1 ;
	print "mode is $mode\n" if $DEBUG > 1 ;
	print "nlink is $nlink\n" if $DEBUG > 1 ;
	print "uid is $uid\n" if $DEBUG > 1 ;
	print "gid is $gid\n" if $DEBUG > 1 ;
	print "rdev is $rdev\n" if $DEBUG > 1 ;
	print "size in bytes is $size\n" if $DEBUG > 1 ;
	print "atime is $atime\n" if $DEBUG > 1 ;
	print "mtime is $mtime\n" if $DEBUG > 1 ;
	print "ctime is $ctime\n" if $DEBUG > 1 ;
	print "blksize is $blksize\n" if $DEBUG > 1 ;
	print "blocks is $blocks\n" if $DEBUG > 1 ;
	print "\n" ;
	#$systemcall = `cp $file $file.old` || die "Error! $!\n" ;
	$systemcall = `cp -p $file $file.old` ;
	print "$systemcall\n" if $DEBUG > 1 ;
	print "Copied $file to $file.old\n" if $DEBUG >= 1 ;
	#open IN,"<$file" || die "Error! $!\n" ;
	open IN,"<$file" ;
	#open OUT,">$file.new" || die "Error! $!\n" ;
	open OUT,">$file.new" ;
	while (<IN>) {
		s/$oldstring/$newstring/g ;
#		s/$oldstring/$newstring/g or die "Error! $!\n" ;
#		s/$oldstring/$newstring/g || die "Error! $!\n" ;
		print "REGEX in progress ...\n" if $DEBUG >= 1 ;
		print "Replacing \"$oldstring\" with \"$newstring\"\n" if $DEBUG >= 1 ;
		print OUT $_ ;
	} # End while loop
#	close OUT or die "Error! $!\n" ;
	close OUT ;
#	close IN or die "Error! $!\n" ;
	close IN ;
#	$systemcall = `cp $file.new $file` || die "Error! $!\n" ;
	$systemcall = `cp -p $file.new $file` ;
	utime($atime, $mtime, $file) ;
	print "Copied $file.new to $file\n" if $DEBUG >= 1 ;
#	$systemcall = `rm $file.new` || die "Error! $!\n" ;
	$systemcall = `rm $file.new` ;
	print "Removed $file.new\n" if $DEBUG >= 1 ;
} # End foreach loop


