#!/usr/bin/perl # Versioning control program # Given filename arguments, searches for existing ..### files # and increments ### accordingly. $debug = 0; sub usage { print "Usage:\n"; print "versioner [-c cache_dir] file(s)\n"; exit; } # end subroutine usage definition unless($ARGV[0]) { usage(); }; if($ARGV[0] eq "-c") { # cache directory shift(@ARGV); $cache_dir = shift(@ARGV); $use_cache = 1; } foreach $name (@ARGV) { # To allow for files coming from multiple directories, # need to get the name from the file path. if($debug){print "filename is: $name\n";}; unless(-e $name) { warn "\"$name\" does not exist\n"; next; } if(-l $name) { print "$name is a link\n"; next; } if($name =~ m/(.*\/)(.*?)$/) { $directory = $1; $name = $2; } else { $directory = "./"; }; if($debug){print "directory is: $directory\n";}; if($debug){print "name is: $name\n";}; unless($use_cache) { $cache_dir = $directory; # now use $cache_dir for all output and oldfiles } opendir(DIRHANDLE,$cache_dir); $#filelist = 0; $filelist[0] = ""; @filelist = readdir(DIRHANDLE); $new_num = "000"; foreach $file (@filelist) { if($file =~ m/\.$name\.(\d{3})/) { $num = $1; if($num >= $new_num) { $new_num = sprintf("%03d",$num+1); }; }; }; if($debug){print "number is: $new_num\n";}; # got number and path, etc so now we can open the output # and input files and make our copy open(INFILE, "$directory/$name"); # this is the original if($new_num ne "000") { $last_num = sprintf("%03d",$new_num - 1); open(LASTFILE, "$cache_dir.$name.$last_num"); $nothing = ; # read the first line to get rid of the date and note. $last = join("", ); if($nothing !~ m/^original/) { $last = $nothing . $last; # backup was created before we started adding comments and date strings. }; } else { $last = 0; }; $line = join("", ); if( ($last != 0) || ($line ne $last) ) # file has changed, so make a backup copy { # only add note if it is a text file if(0 or -T "$directory/$name") { # Mon Aug 21, 2006 -- now I know better ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime( (stat("$directory/$name") )[9] ); if($debug){print "stat gives: ", (stat("$directory/$name") )[8], "\n"; }; $year +=1900; $created_date = sprintf("%02d/%02d/%02d",$mon+1,$mday,$year); ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime; $year +=1900; $now_date = sprintf("%02d/%02d/%02d",$mon+1,$mday,$year); $note = "original file last modified on $created_date" . " backed up by versioner on: $now_date\n"; } else { $note = ""; } (open(OUTFILE, ">$cache_dir.$name.$new_num") ) or die "couldn't open outfile\n"; print OUTFILE "$note" . "$line"; print "$cache_dir.$name.$new_num\t"; $success++; } # end if new else { print "no\t"; $success++; # not really success, but better than failure }; }; #end foreach $success && print "revisions saved\n";