#!/usr/bin/perl 
#
#  Execute from the same directory as CISM_DX-X.YY
#  i.e. ~/CISM_DX-X.YY/Perl/setpaths.pl 0.50
# 
#  Usage: set_paths.pl VERSION
#  show what will be changed
#
#  Usage: set_paths.pl VERSION 1
#  make changes
#
#  R.S. Weigel 03/05/2005;

use File::Find;
use Cwd;

###############################################################################
$num = scalar(@ARGV); # number of input arguments.
if ($num < 1) {
    die "Usage: set_paths.pl VERSION\n";
}

$VERSION    = $ARGV[0];
$OLDPATTERN = "/usr/local";
$NEWPATTERN = cwd();
$cur        = $NEWPATTERN;


print "Pattern to replace: $OLDPATTERN\n";
print "New Pattern       : $NEWPATTERN\n";

if ($ARGV[1]==1){
    print "Modify files?     : yes\n";
} else {
    print "Modify files?     : no\n";
}
###############################################################################
$dir1      = "./CISM_DX-$VERSION";
@directory = ($dir1);

if (-e $dir1 == 0) {
    die "Did not find directory named $dir1 in current directory ($cur)\n";

} else {
    find(\&wanted, @directory);
}

sub wanted {

    # Modify files with these extensions
    if ( ($_ =~ m/\.net|\.cfg|\.general|\.mdf|\.htm|\.html|\.tex|\.animate|cismdx|spdx|CISM_DX_startup.m/) & ($File::Find::name !~ m/octave-forge/) ){

	open(DATA_in,"$_");

	if ($ARGV[1] == 1) {
	    system("cp --preserve $_ $_.tmp");
	    open(DATA_out,">$_.tmp");
	}

	while ($line = <DATA_in>){

	    $line_o = $line;

	    if ($line =~ s/$OLDPATTERN/$NEWPATTERN/) {
		print "\nFile: $File::Find::name\n";
		print "-- Original $_: $line_o\n";
		print "-- Modified $_: $line\n";
	    }
	    
	    print DATA_out "$line";
	    
	}
	close(DATA_in);

	if ($ARGV[1] == 1) {
	    close(DATA_out);
	    system("cp --preserve $_.tmp $_");
	    system("rm -f $_.tmp");
	}

    }

}
