#!/usr/bin/perl -w
#
# Script to update a repistory and log updates
#
# cvslogger Copyright (C) Dorando 2004
#
# WIP   /             /
#
# Permission is granted to copy, distribute, and/or modify any part of this file
# as long as the original author is credited properly.

use strict;
my $version='20040220';

# loading required modules
use POSIX qw(strftime);
use Getopt::Long qw(:config gnu_getopt no_ignore_case);
use Pod::Usage;

# defining global variables
use vars qw(
$opt_config $opt_cvs $opt_input $opt_output $opt_time $opt_verbose
);

# defining local variables
my (@log);

# default values for options
my $opt_time = "%a %b %e %H:%M:%S UTC %Y";

# load an ArgvFile if this module is available, otherwise try a perl script
if(eval 'require Getopt::ArgvFile qw(argvFile);'){argvFile(home=>1);}
else{eval "require '$ENV{'HOME'}/.cvslogger'";}

# parse command line options
GetOptions('config|c=s'        => \$opt_config,
           'cvs|options|C=s'   => \$opt_cvs,
           'help|h|?'          => sub {pod2usage(1)},
           'input|i=s'         => \$opt_input,
           'man|M'             => sub {pod2usage(-verbose => 2)},
           'output|o=s'        => \$opt_output,
           'verbose|V'         => \$opt_verbose,
           'version|v'         => sub {print "cvslogger $version\n"; exit},
          ) || pod2usage(1);

# load additional config file
if($opt_config){eval "require '$opt_config'"};

# bail if we don't have an output dir
if(!$opt_output){print "--output required\n"; exit;}

# get data
if($opt_input) # from an input file
{
	open(LOG,$opt_input) or die "Error: Could not open $opt_input";
	@log=<LOG>;
	close(LOG);
}
else           # from cvs
{
#	$opt_cvs||='-dA -D "tomorrow 00:00:00 UTC"';
	$opt_cvs||='-dA';
	@log = `cvs -q update 2>&1 $opt_cvs`;
}

if(!@log){@log = "*** Nothing ***\n"};

open(LOGFILE,">>$opt_output") or die "Error: Could not create $opt_output\n";
	print LOGFILE strftime "$opt_time\n", gmtime;
	print LOGFILE @log;
	print LOGFILE "\n";
close LOGFILE;

#my @time = gmtime;
#$time[3]--;

#open(LOGFILE,">>$opt_output/".strftime("plain-%Y%m%d\n",@time)) or die "Error: Could not create file in $opt_output\n";
#	print LOGFILE @log;
#close LOGFILE;


#open(LOGFILE,">>$opt_output/".strftime("plain-%Y%m\n",@time)) or die "Error: Could not create file in $opt_output\n";
#   print LOGFILE strftime "%a %b %e %H:%M:%S UTC %Y\n", gmtime;
#   print LOGFILE @log;
#   print LOGFILE "\n";
#close LOGFILE;
		
