#!usr/local/bin/perl

###############################################################################
#							CONVERT_DB.PL
#
# Version: 1.0.1
# Last Updated: 04 DEC 98
#
# This program was written by Michael E. Schechter (mschechter@earthlink.net).
#
# You may copy this under the terms of the GNU General Public
# License or the Artistic License which is distributed with
# copies of Perl v5.x for UNIX.
#
# This script will take your original calendar.events file, and reformat it
# for the type of database files used with FrameCal version 2.0.
#
# You may run this script through your web browser by calling it directly in
# the address line of your browser.
#
# To run this file, first place it in the same directory as your
# calendar.events file.  Create an "events" subdirectory underneath the
# directory in which you have your calendar.events file.  Then, call the
# script through your web browser as:
#
#		http://www.yoursite.com/cgi-bin/dbs/yourcal/convert_db.pl
#
# The script will break your calendar.events file into multiple .events files.
# each file will hold events for a particular month, in the format
# yyyymm.events.  The details of each event will be held in a file in the
# events subdirectory, with the id number of the event being used as
# the file name.  After the script is run, you will presented with a report
# detailing what the script has done.  Your calendar.events file (original)
# will NOT be deleted by the script, just in case anything goes wrong with
# this script and you need to run it again.  After you have run the script,
# remove it from the directory, so a "visitor" to your site won't try to
# run it again.
###############################################################################

$| = 1;
$path_to_db = ".";
$path_to_events = "./events";
print "Content-type: text/html\n\n";
&header;
open (DATABASE, "$path_to_db/calendar.events") || &open_error ( "calendar.events" );
while (<DATABASE>) {
	unless (/^COMMENT:/) {
		($day,$month,$year,$username,$name,$email,$subject,$time,$timezone,
		 $location,$body,$duration,$id) = split (/\|/, $_);
		chomp ($id);
		$new_db = "$year".sprintf("%.2d", $month).".events";
		if (-e "./$new_db") {
			open (NEWDB, ">>$path_to_db/$new_db") || &open_error ( "$new_db" );
		} else {
			open (NEWDB, ">$path_to_db/$new_db") || &open_error ( "$new_db" );
		}
		$dbs_added{"$new_db"} = 1;
		$new_entry = "$day\|$time\|$username\|$id\n";
		print (NEWDB "$new_entry");
		close (NEWDB);
		($dur_hrs,$dur_mins) = split (/\./, $duration);
		$dur_mins = $dur_mins * 6;
		$new_entry = "";
		$new_entry .= "time->$time\ntimezone->$time_zone\ndurationhrs->$dur_hrs\n";
		$new_entry .= "durationmins->$dur_mins\nname->$name\nemail->$email\n";
		$new_entry .= "notified->\nlinkurl->\nlinktitle->\nlocation->$location\n";
		$new_entry .= "subject->$subject\nbody->$body\n";
		$opened = open (EVENTFILE, ">$path_to_events/$id");
		if ($opened) {
			print (EVENTFILE "$new_entry");
			close (EVENTFILE);
		} else {
			push (@missed_files, "$id");
		}
	}
}
close (DATABASE);
print "<center><h3>Conversion Results</h3></center>";
@dbs_added = keys (%dbs_added);
print "<b>The following database files were created:</b><p><blockquote>";
print "@dbs_added</blockquote><p>";
print "<b>Their were problems with the following individual event files:</b><p><blockquote>";
if (@missed_files) {
	print "@missed_files";
} else {
	print "None.  All files converted successfully.";
}
print "</blockquote><p>";
print "If there were problems with event files, please correct manually.  See existing ",
      "files in the ./events directory for examples.  The calendar.events file should ",
      "be deleted manually when satisfied with results.";
&footer;

sub open_error {
    my ($filename) = @_;
    print "I am really sorry, but for some reason I was unable to open
    <b>$filename<b>.  Please read through the source for this script and
    verify that all file paths are correct for your implementation.  Thanks!";
    &footer;
    die;
}

sub header {
	print qq!<html><head><title>Conversion Results</title></head><body>!;
}

sub footer {
	print qq!</body></html>!;
}
