#/usr/bin/perl die "Usage: fixscripts [files]\n" unless @ARGV; # Configuration parameters. $BIN = '/usr/local/bin'; $SCRIPTS = '/u/scripts'; $BAK = 'bak'; # Process each file. foreach $file (@ARGV) { unless (-T $file) { warn "Can't process binary file: $file\n"; next; } open(FILE, $file) || do { warn "Can't open $file: $!\n"; next; }; # Now process the file. $contents = ''; $changed = 0; while () { if (m#$BIN#o) { print STDERR "<$file:\t",$_; $changed++; if (m#:/#o) { # a $path s#$BIN#$BIN:$SCRIPTS#o; } elsif (m# /usr# && /set\s/) { # a $PATH s#$BIN([^/])#$BIN $SCRIPTS$1#o; } else { # Try substitutions until one works. s#$BIN/(\w+)# -f "$SCRIPTS/$1" ? "$SCRIPTS/$1" : "$BIN/$1"#oeg # a program || s# $BIN# $BIN $SCRIPTS#o # $PATH? || s#$BIN #$BIN $SCRIPTS #o # $PATH? || s#:$BIN#:$BIN:$SCRIPTS#o # $path? || s#$BIN:#$BIN:$SCRIPTS:#o # $path? ; } print STDERR ">$file:\t",$_; } $contents .= $_; } close FILE; if ($changed) { rename($file,"$file.$BAK") || do { warn "Can't rename $file: $!\n"; next; }; open(FILE, ">$file") || do { warn "Can't make $file: $!\n"; next; }; print FILE $contents; close FILE; } }