#!/usr/bin/perl -wT

# Apache::LowCal::Admin - Command-line tool for LowCal recurring event
#                         generation and disposal
# Copyright (C) 2001 Dave Sherohman (dave@sherohman.org)
#                                                       
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or   
# (at your option) any later version.                              
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
# GNU General Public License for more details.                 
#                                             
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software      
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

use strict;

use Apache::LowCal::CalCron;

my $version = "1.1.0";
my ($appname) = $0 =~ m~([^/]+)$~;    # Strip path info off of $0

$ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin';
delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};

my ($evt_path, $clear_to, $fake, $verbose);
$fake = 0;
$verbose = 0;

# Parse command-line parameters
while (!defined $evt_path) {
  my $param = shift || show_usage();

  if ($param eq '-c') {
    my ($month, $year) = (localtime)[4..5];
    $year += 1900;
    $month += 1;

    $clear_to = sprintf "%04d%02d01", $year, $month;

  } elsif ($param eq '-C') {
    my ($day, $month, $year) = (localtime)[3..5];
    $year += 1900;
    $month += 1;

    $clear_to = sprintf "%04d%02d%02d", $year, $month, $day;

  } elsif ($param eq '-f') {
    $fake = 1;
    $verbose = 1;

  } elsif ($param =~ '^-v(\d*)') {
    if ($1)   { $verbose = $1 }
    else      { $verbose = 1  }

  } elsif ($param =~ /^-/) {
    die "Unrecognized option: $param\n";

  } else {
    $evt_path = $param;
  }
}

# Propagate verbose and fake settings into CalCron module
$Apache::LowCal::CalCron::verbose = $verbose;
$Apache::LowCal::CalCron::fake    = $fake;

if ($#ARGV > -1) {
  print "Creating repeating events for " . (join ", ", @ARGV) . "\n"
    if $verbose;
  fill_month($evt_path, @ARGV);
}

if (defined $clear_to) {
  print "Clearing all events prior to $clear_to\n";
  clear_events($evt_path, $clear_to);
}

exit 0;


sub show_usage {
print <<EOT
$appname $version
usage:
  $appname eventdir [options] month[ month...]

  $appname generates recurring event listings for the listed months
  within the specified event directory.

  Months are given in YYYYMM format (e.g., 200012 = December 2000) or as a
  nonnegative offset from the current month (e.g., if today is 1/1/2001, +1 =
  February, 2001).

  Options:
    -c    Clear (delete) all events prior to the first of this month
    -C    Clear all events prior to today
    -f    Fake it - state what will be done, but don't actually do anything
          (implies -v)
    -v[N] Verbosely report all actions that are being taken.  Larger values
          of N will produce more verbose output.  If N is omitted, defaults
          to -v1.
EOT
;

exit 1;

}

