log.pl
changeset 12 b3edfead728b
parent 11 80890c874a90
child 13 d9694ca1b7fc
--- a/log.pl	Wed Sep 03 19:31:51 2008 +0000
+++ b/log.pl	Tue Feb 03 11:00:50 2009 +0000
@@ -3,9 +3,10 @@
 # $URL$
 my $USAGE = <<'#';
 $ME [options]
-    --[no]db	insert into log database [$opt_db]
-    --[no]mail	send mails to @config::mailto [$opt_mail]
-    --message=s message
+    --[no]db	 insert into log database [$opt_db]
+    --[no]mail	 send mails to @config::mailto [$opt_mail]
+    --init-dir=s initialize specified directory with VCS repository
+    --message=s  message
                 - if the message starts with '\@' it's considered
 		  to be a message FILE.
 		- message can be: apt[:text]\@<FILE> - this will
@@ -19,6 +20,7 @@
 use File::Basename;
 use File::Temp qw(tempfile);
 use File::stat;
+use File::Which;
 use Getopt::Long;
 use Mail::Mailer;
 use DBI;
@@ -51,18 +53,18 @@
 my $EDITOR = $ENV{VISUAL} || $ENV{EDITOR} || "vim";
 my $MAGIC = "#--- all changes below are ignored ---#\n";
 
-my $opt_db = 1;
-my $opt_mail = 1;
+my $opt_db = 0;
+my $opt_mail = 0;
 my $opt_message = "";
 my $opt_apt = "";
-
-
+my $opt_initdir = "";
 
 my $Dbh;
 
 sub identity();
 sub hostname();
 sub mailto();
+sub check_hg_bin();
 
 MAIN: {
 
@@ -70,6 +72,7 @@
 	"mail!" => \$opt_mail,
 	"message=s" => \$opt_message,
 	"type=s" => \$opt_apt,
+	"init-dir=s" => \$opt_initdir,
     ) or die eval "\"$USAGE\"";
 
     if ($opt_message =~ /^@(.*)/) {
@@ -91,6 +94,47 @@
 	$opt_message =~ s/\n/\n    /g;
     }
 
+    if (defined @config::notify_dirs || $opt_initdir) {
+	check_hg_bin();
+    }
+
+    if ($opt_initdir) {
+	print "$ME: Trying to initialize $opt_initdir as mercurial repository.\n";
+	-d $opt_initdir or
+	    die "$ME: directory $opt_initdir does not exist!";
+
+	system("hg status $opt_initdir > /dev/null 2>&1");
+	if (($? >> 8)  == 0) {
+	    die "$ME: directory already initialized, skipping\n";
+	}
+
+	system("hg init $opt_initdir");
+	if ( ($? >> 8) != 0) {
+	    die "E: initialization failed\n";
+	} else {
+	    system("cd $opt_initdir && hg addremove && hg commit -m 'initial autocommit'");
+	    print "$ME: initialization done.\n";
+	} 
+
+	exit 0;
+    }
+
+    my $hg_status_text = "";
+    if (defined @config::notify_dirs) {
+	foreach my $dir (@config::notify_dirs) {
+	    -d $dir or next;
+
+	    print "$ME: Checking $dir for modifications\n";
+	    -d "$dir/.hg" or 
+		die "$ME: directory $dir not initialized pleas call: \n",
+		    "  # $ME --init-dir $dir \n";
+
+	    system("hg addremove $dir");
+
+	    $hg_status_text .= `cd / && hg status $dir`;
+	}
+    }
+
     if ($opt_db) {
 	$Dbh = DBI->connect($DSN, $USER, $PW, {RaiseError => 1})
 	    or die $DBI::errstr;
@@ -102,6 +146,12 @@
     END { unlink $file if $file; }
     ($fh, $file) = tempfile(DIR => "/tmp");
 
+    my $auto_message = (not $hg_status_text) ? "" :
+	"\n"
+	. " Modified config files since last log entry listend below...\n"
+	. $hg_status_text
+	. "\n";
+
     # Kopftext eintragen
     print $fh 
 	    "Date: ", scalar(localtime()), "\n",
@@ -110,6 +160,7 @@
 	    "\n",
 	    "  * $opt_message",
 	    "\n",
+	    $auto_message,
 	    "\n", $MAGIC, "\n";
 
     if (!-e $LOG) {
@@ -180,6 +231,14 @@
 	    close $mailer;
 	    print STDERR "Mail sent (to $mailto).\n";
 	}
+
+	if (defined @config::notify_dirs) {
+	    foreach my $dir (@config::notify_dirs) {
+		-d $dir or next;
+
+		system("cd $dir && hg commit -m 'autocommit by logbuch'");
+	    }
+	}
     }
 
     # Und jetzt das aus der alten Datei dort anhängen
@@ -212,5 +271,27 @@
     return join(", ", @config::mailto);
 }
 
+
+sub check_hg_bin()
+{
+    if (not defined which('hg')) {
+	print STDERR << 'EOF';
+
+You requested an operation based on hg/mercurial but this tool is 
+not installed!
+
+Either you could change the configuration in /etc/lobbuch/config.pm and
+remove lines starting with @notify_dirs, or you could simply install the
+required packages:
+
+    # aptitude install mercurial
+
+Exiting!
+EOF
+	exit 1;
+    }
+}
+
+
 # vim:sts=4 sw=4 aw ai sm: