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

BEGIN{($_=$0)=~s![\\/][^\\/]+$!!;push@INC,$_}

$name = $0;
$name =~ s/.+\/.+\///; # for unix
$name =~ s/.+\\.+\\//; # for windows

$path = $0;
$path =~ s/(.+\/).+/$1/g; # for unix
$path =~ s/(.+\\).+/$1/g; # for windows

if ($path ne "")
{
chdir $path;
push@INC,$path;
}


my $old_events_file = "events.dat";
my $old_orgs_file = "orgs.dat";
$rightnow=time();

# it's possible to convert straight from .dat to sql, but 
# I think the 3 step .dat -> .xml  -> sql process is better.
$data_storage_mode=0;


my @events_lines;
my @orgs_lines;
my @new_lines;  # used for both events and calendars
print "content-type:text/html\n\n";

print <<p1;
  This utility converts data from old versions of 
  plans (pre-5.0) to the post-5.0 XML format.

  It requires the files "$old_events_file" and "$old_orgs_file" to be 
  in the current directory (where you run plans_convert.pl from).

  It automatically makes backup copies of these 
  files ("$old_events_file.bak" and "$old_orgs_file.bak").

  It then creates two new files:
   $events_file
   $calendars_file

p1

# check to see whether the input files exist.    if (!(-e "$default_template_file"))
if (-e "$old_events_file")
{
    open (datafile, "$old_events_file") or die "Error!  unable to read from $old_events_file!";
    flock datafile,2;
    @events_lines=<datafile>;
    close datafile;
}
else
  {die "Error!  can't find $old_events_file in the current directory!";}
  
if (-e "$old_orgs_file")
{
    open (datafile, "$old_orgs_file") or die "Error!  unable to read from $old_orgs_file!";
    flock datafile,2;
    @orgs_lines=<datafile>;
    close datafile;
}
else
  {die "Error!  can't find $old_orgs_file  in the current directory!";}

# make the backups
open (datafile, ">$old_orgs_file.bak") or die "Error!  unable to write to $old_orgs_file.bak!";
flock datafile,2;
print datafile @orgs_lines;
close datafile;

open (datafile, ">$old_events_file.bak") or die "Error!  unable to write to $old_events_file.bak!";
flock datafile,2;
print datafile @events_lines;
close datafile;

  #get rid of first line (not a record, but a row of field descriptors)
  shift @orgs_lines;
  #get rid of first line (not a record, but a row of field descriptors)
  shift @events_lines;
  
# create the calendars data structure.
foreach $line (@orgs_lines)
{
    #ignore blank lines
    if ($line !~ /\S/)
      {next;}
      
    my ($cal_id, $cal_label, $shared_calendars, $cal_details, $cal_password, $timestamp,$cal_link)= split ('\|', $line);
    
    $cal_label = &decode($cal_label);
    $cal_details = &decode($cal_details);
    $cal_link = &decode($cal_link);
    
    # add local background calendars
    my (@cal_shared_cal_ids) = split (',',$shared_calendars);
    my %local_background_calendars;
    foreach $id (@cal_shared_cal_ids)
    {
      $local_background_calendars{$id} = 1;
    }
    
    $calendars{$cal_id} = {id => $cal_id, 
                          title => $cal_label, 
                          details => $cal_details,
                          link => $cal_link,
                          local_background_calendars => \%local_background_calendars,
                          selectable_calendars => {},
                          list_background_calendars_together => "no",
                          background_events_display_style => "normal",
                          background_events_fade_factor => 1,
                          background_events_color => "#ffffff",
                          default_number_of_months => 1,
                          max_number_of_months => 24,
                          gmtime_diff => 0,
                          date_format => "mm/dd/yy",
                          week_start_day => "0",
                          info_window_size => "400x400",
                          custom_template => "",
                          custom_stylesheet => "",
                          password => $cal_password,
                          update_timestamp => $rightnow};                
                  
}
# make all calendars selectable by all other calendars
foreach $cal_id (keys %calendars)
{
  foreach $cal_id2 (keys %calendars)
  {
    $calendars{$cal_id}{selectable_calendars}{$cal_id2}=1;
  }
}

# update the calendars file(the library routines handle all xml assembly)
&add_calendars();



# create the events data structure.
foreach $line (@events_lines)
{
    #ignore blank lines
    if ($line !~ /\S/)
      {next;}
      
    my ($evt_id, $evt_label, $evt_start_date, $evt_length, $evt_details, $evt_cal_id, $evt_attributes, $evt_icon, $timestamp)= split ('\|', $line);
    $evt_label = &decode($evt_label);
    $evt_details = &decode($evt_details);
    $evt_icon = &decode($evt_icon);
    
    my ($evt_icon, $evt_bgcolor, $evt_unit_number) = split (",", $evt_icon);

    #convert date from silly human format to timestamp (seconds since 1970) format
    my ($mon, $mday, $year) = split ('/', $evt_start_date);
    $mon --;
    my $evt_start = timegm(0,0,0,$mday,$mon,$year);
    my $evt_end = $evt_start + (86400 * $evt_length) - 1;
    
    my @evt_cal_ids = ($evt_cal_id);


    $events{$evt_id} = {id => $evt_id, 
                        cal_ids => \@evt_cal_ids, 
                        start => $evt_start, 
                        end => $evt_end, 
                        days => $evt_length, 
                        title => $evt_label, 
                        details => $evt_details,
                        icon => $evt_icon,
                        bgcolor => $evt_bgcolor,
                        unit_number => $evt_unit_number,
                        update_timestamp => $rightnow};
}


# update the events file(the library routines handle all xml assembly)
my @temp_event_ids = keys %events;
&add_events(\@temp_event_ids);
