How about this for a change log script:
-----------------------------------
#!/usr/bin/perl
# $Id: changelog.pl,v 1.1 2005/09/07 11:47:15 megglest Exp $
# $Log: changelog.pl,v $
# Revision 1.1 2005/09/07 11:47:15 megglest
# almost done
#
# given a statement
# log that statement in either the specified ChangeLog file
# or in the nearest ChangeLog file found
use Getopt::Std;
use strict;
# globals
my @months = (
'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
);
my $datepattern = '^\d\d\s+\w\w\w\s+\d\d\s+';
# parse the command line
our ($opt_h, $opt_v, $opt_u, $opt_m, $opt_f);
getopts('hvu:m:f:');
&usage if $opt_h;
#&usage if !defined($opt_m) and !defined($opt_f);
$opt_u = (getpwuid($>))[0] unless $opt_u;
###############
## main loop
###############
my $date = &today;
my $time = &now;
my $file = &findChangeLog;
my $msg = &getMessage($file, $opt_m);
my $append = &beenUpdated($file, $date);
$append = 2 if ! -f $file;
open(OUT, ">> $file") or die "$0: cannot append file '$file': $!";
print OUT "\n" if $append != 2;
print OUT "$date\n" if $append;
print OUT "\t* $time $opt_u\n";
if(ref($msg) eq 'SCALAR') {
print OUT "\t ", ${$msg}, "\n";
} else {
print OUT "\t ", join("\n\t ", @{$msg}), "\n";
}
close(OUT);
###############
## utility functions
###############
# get today's date
sub today {
my @t = localtime;
return sprintf("%02d %-3s %02d", $t[3], $months[$t[4]], $t[5] + 19
+00);
}
# get today's date
sub now {
my @t = localtime;
return sprintf("%02d:%02d", $t[2], $t[1]);
}
# find the nearest ChangeLog file
sub findChangeLog {
return $opt_f if $opt_f;
my $pwd = $ENV{'PWD'} || `pwd`;
chomp($pwd);
my @a = split('/', $pwd);
unshift(@a, '/');
my $file;
my $keep = &isroot ? '/ChangeLog' : "$pwd/ChangeLog";
foreach my $part (@a) {
$file .= '/' if length($file);
$file .= $part;
my $fn = $file . '/ChangeLog';
$fn =~ s,//,/,go;
$keep = $fn if -f $fn;
}
$keep =~ s,//,/,go;
return $keep;
}
# is the user root?
sub isroot {
return 1 if $> == 0;
return 0;
}
# has the file been updated today?
sub beenUpdated {
my $fn = shift;
my $date = shift;
my $lastdate;
return 0 unless -f $fn;
open(IN, "< $fn") or die "$0: cannot read file '$fn': $!";
while(<IN>) {
chomp;
$lastdate = $_ if /$datepattern/o;
}
close(IN);
return $lastdate eq $date;
}
# if no -m supplied, present an editor asking the user for a message
sub getMessage {
my $file = shift;
my $msg = shift;
return \$msg if defined($msg);
$msg = 'Not a terminal and no message suppled' if(system('tty -s')
+ >> 8) == 1;
$ENV{'TMPDIR'} = '/tmp' unless $ENV{'TMPDIR'}; # set a defa
+ult directory
my $tfile = "$ENV{'TMPDIR'}/changelog.$$";
$ENV{'EDITOR'} = 'vi' unless $ENV{'EDITOR'}; # set the defa
+ult editor
$ENV{'VISUAL'} = 'vi' unless $ENV{'VISUAL'}; # set the defa
+ult editor
# create the temporary file
open(OUT, "> $tfile") or die "$0: cannot create file '$tfile': $!"
+;
print OUT "CL ------------------------------------------\n";
print OUT "CL Enter a ChangeLog Message for file '$file'\n";
print OUT "CL All lines beginning with 'CL' are ignored\n";
print OUT "CL ------------------------------------------\n";
close(OUT);
# ask the user to edit the file
system("$ENV{'VISUAL'} $tfile");
# read the file contents in
# remove all the junk messages
open(IN, "grep -v '^CL' $tfile |") or die "$0: cannot read file '$
+tfile': $!";
my @lines = <IN>;
chomp(@lines);
close(IN);
# retun the message string
return \@lines;
}
# how to use this script
sub usage {
print "usage: $0: [-m MESSAGE][-f CHANGELOGFILE][-u USERNAME][-vh]
+\n";
print "\t-m MESSAGE - message to log\n";
print "\t-f CHANGELOGFILE - specific ChangeLog file to log the mes
+sage\n";
print "\t-u USERNAME - username to log the message against\n"
+;
print "\t-v - verbose\n";
print "\t-h - help (this message)\n";
exit 1;
}
__END__
=pod
TODO
=cut
-----------------------------------
|