log.pl
changeset 13 d9694ca1b7fc
parent 12 b3edfead728b
child 14 744cb8934861
equal deleted inserted replaced
12:b3edfead728b 13:d9694ca1b7fc
    18 use strict;
    18 use strict;
    19 use warnings;
    19 use warnings;
    20 use File::Basename;
    20 use File::Basename;
    21 use File::Temp qw(tempfile);
    21 use File::Temp qw(tempfile);
    22 use File::stat;
    22 use File::stat;
    23 use File::Which;
       
    24 use Getopt::Long;
    23 use Getopt::Long;
    25 use Mail::Mailer;
    24 use Mail::Mailer;
    26 use DBI;
    25 use DBI;
    27 use MIME::QuotedPrint;
    26 use MIME::QuotedPrint;
       
    27 
       
    28 use Logbuch::HG;
    28 
    29 
    29 use lib "/etc/logbuch";
    30 use lib "/etc/logbuch";
    30 use config;
    31 use config;
    31 
    32 
    32 # print @config::mailto, "\n";
    33 # print @config::mailto, "\n";
    51 
    52 
    52 my $LOG = "$ENV{HOME}/LOG";
    53 my $LOG = "$ENV{HOME}/LOG";
    53 my $EDITOR = $ENV{VISUAL} || $ENV{EDITOR} || "vim";
    54 my $EDITOR = $ENV{VISUAL} || $ENV{EDITOR} || "vim";
    54 my $MAGIC = "#--- all changes below are ignored ---#\n";
    55 my $MAGIC = "#--- all changes below are ignored ---#\n";
    55 
    56 
    56 my $opt_db = 0;
    57 my $opt_db = 1;
    57 my $opt_mail = 0;
    58 my $opt_mail = 1;
    58 my $opt_message = "";
    59 my $opt_message = "";
    59 my $opt_apt = "";
    60 my $opt_apt = "";
    60 my $opt_initdir = "";
    61 my $opt_initdir = "";
    61 
    62 
    62 my $Dbh;
    63 my $Dbh;
    97     if (defined @config::notify_dirs || $opt_initdir) {
    98     if (defined @config::notify_dirs || $opt_initdir) {
    98 	check_hg_bin();
    99 	check_hg_bin();
    99     }
   100     }
   100 
   101 
   101     if ($opt_initdir) {
   102     if ($opt_initdir) {
   102 	print "$ME: Trying to initialize $opt_initdir as mercurial repository.\n";
   103 	my $repo = Logbuch::HG->new( repo => $opt_initdir );
   103 	-d $opt_initdir or
   104 
   104 	    die "$ME: directory $opt_initdir does not exist!";
   105 	$repo->is_repository() and
   105 
       
   106 	system("hg status $opt_initdir > /dev/null 2>&1");
       
   107 	if (($? >> 8)  == 0) {
       
   108 	    die "$ME: directory already initialized, skipping\n";
   106 	    die "$ME: directory already initialized, skipping\n";
   109 	}
   107 
   110 
   108 	$repo->init() or
   111 	system("hg init $opt_initdir");
       
   112 	if ( ($? >> 8) != 0) {
       
   113 	    die "E: initialization failed\n";
   109 	    die "E: initialization failed\n";
   114 	} else {
   110 	
   115 	    system("cd $opt_initdir && hg addremove && hg commit -m 'initial autocommit'");
   111 	$repo->addremove();
   116 	    print "$ME: initialization done.\n";
   112 	$repo->commit("initial check in");
   117 	} 
       
   118 
   113 
   119 	exit 0;
   114 	exit 0;
   120     }
   115     }
   121 
   116 
   122     my $hg_status_text = "";
   117     my $hg_status_text = "";
   123     if (defined @config::notify_dirs) {
   118     if (defined @config::notify_dirs) {
   124 	foreach my $dir (@config::notify_dirs) {
   119 	foreach my $dir (@config::notify_dirs) {
   125 	    -d $dir or next;
   120 	    -d $dir or next;
   126 
   121 
   127 	    print "$ME: Checking $dir for modifications\n";
   122 	    print "$ME: Checking $dir for modifications\n";
   128 	    -d "$dir/.hg" or 
   123 
   129 		die "$ME: directory $dir not initialized pleas call: \n",
   124 	    my $repo = Logbuch::HG->new( repo => $dir );
       
   125 	    $repo->is_repository() or
       
   126 		die "$ME: directory $dir not initialized please call: \n",
   130 		    "  # $ME --init-dir $dir \n";
   127 		    "  # $ME --init-dir $dir \n";
   131 
   128 
   132 	    system("hg addremove $dir");
   129 	    $repo->addremove();
   133 
   130 	    $hg_status_text .= $repo->status();
   134 	    $hg_status_text .= `cd / && hg status $dir`;
       
   135 	}
   131 	}
   136     }
   132     }
   137 
   133 
   138     if ($opt_db) {
   134     if ($opt_db) {
   139 	$Dbh = DBI->connect($DSN, $USER, $PW, {RaiseError => 1})
   135 	$Dbh = DBI->connect($DSN, $USER, $PW, {RaiseError => 1})
   234 
   230 
   235 	if (defined @config::notify_dirs) {
   231 	if (defined @config::notify_dirs) {
   236 	    foreach my $dir (@config::notify_dirs) {
   232 	    foreach my $dir (@config::notify_dirs) {
   237 		-d $dir or next;
   233 		-d $dir or next;
   238 
   234 
   239 		system("cd $dir && hg commit -m 'autocommit by logbuch'");
   235 		my $repo = Logbuch::HG->new( repo => $dir );
       
   236 		$repo->commit();
   240 	    }
   237 	    }
   241 	}
   238 	}
   242     }
   239     }
   243 
   240 
   244     # Und jetzt das aus der alten Datei dort anhängen
   241     # Und jetzt das aus der alten Datei dort anhängen
   272 }
   269 }
   273 
   270 
   274 
   271 
   275 sub check_hg_bin()
   272 sub check_hg_bin()
   276 {
   273 {
   277     if (not defined which('hg')) {
   274     if (not Logbuch::HG::hg_available()) {
   278 	print STDERR << 'EOF';
   275 
       
   276 	die <<'EOF';
   279 
   277 
   280 You requested an operation based on hg/mercurial but this tool is 
   278 You requested an operation based on hg/mercurial but this tool is 
   281 not installed!
   279 not installed!
   282 
   280 
   283 Either you could change the configuration in /etc/lobbuch/config.pm and
   281 Either you could change the configuration in /etc/lobbuch/config.pm and
   284 remove lines starting with @notify_dirs, or you could simply install the
   282 remove lines starting with @notify_dirs, or you could simply install the
   285 required packages:
   283 required packages:
   286 
   284 
   287     # aptitude install mercurial
   285     # aptitude install mercurial rcs
   288 
   286 
   289 Exiting!
   287 Exiting!
   290 EOF
   288 EOF
   291 	exit 1;
       
   292     }
   289     }
   293 }
   290 }
   294 
   291 
   295 
   292 
   296 # vim:sts=4 sw=4 aw ai sm:
   293 # vim:sts=4 sw=4 aw ai sm: