#!/usr/bin/perl
use Time::Local;
require "plans_config.pl";
require "plans_lib.pl";

my @language_files = split(',', $options{language_files});
foreach $language_file (@language_files)
{
  require $language_file;
}

my $old_events_file = "events_old.xml";
my $new_events_file = "events_new.xml";

print <<p1;

 This utility splits your events.xml file by date into two files:
 "$old_events_file" and "$new_events_file"

 It does not modify your original data.

 Date to split at: 
  (Events starting on or after this date go into "$new_events_file".
 Enter year:
p1

my $start_year = <STDIN>;

print <<p1;
 Enter month:
  0 - January    6 - July
  1 - February   7 - August
  2 - March      8 - September
  3 - April      9 - October
  4 - May        10 - November
  5 - June       11 - December
p1
my $start_month = <STDIN>;


my $split_time = timegm(0,0,0,1,$start_month,$start_year);


&load_events("all");

my %new_events;
my %old_events;

foreach $event_id (keys %events)
{
  my $date = &nice_date_range_format($events{$event_id}{start},$events{$event_id}{end});

  if ($events{$event_id}{start} > $split_time)
  {
    print "moved event $event_id ($date) to $new_events_file\n";
    $new_events{$event_id} = $events{$event_id};
  }
  else
  {
    print "moved event $event_id ($date) to $old_events_file\n";
    $old_events{$event_id} = $events{$event_id};
  }
}

# create new and old events files
    open (FH, ">$new_events_file") || die("unable to create $new_events_file for writing!\n");
    flock FH,2;
    print FH "";
    close FH;
    
    open (FH, ">$old_events_file") || die("unable to create $old_events_file for writing!\n");
    flock FH,2;
    print FH "";
    close FH;


%events = %new_events;
$options{events_file} = $new_events_file;
&update_events();

%events = %old_events;
$options{events_file} = $old_events_file;
&update_events();


