#!/usr/local/bin/perl
#
# ----------------------------------------------------------------------
# Defaults
# ----------------------------------------------------------------------

# Name of the MM export text file

$MM = "export.txt";

$debug = 0;

# ----------------------------------------------------------------------
# Main program
# ----------------------------------------------------------------------
#
# MM flat file report field order (these are the Index's we use
# to access our internal data structure.  There is actually a whole
# lot of code in the read_MM routine to map things just in case
# the order of things changes
#
$TitleIdx		= 1;
$LocationIdx		= 2;
$DateIdx		= 3;
$StartTimeIdx		= 4;
$DurationIdx		= 5;
$PrivateIdx             = 6;
$FlexibleIdx            = 7;
$LabelIdx               = 8;
$AgendaIdx              = 9;
$MMLineIdx		= 10;	# Track the line number that this came from


# Temp Output to what File name. 
$OUTFILE = "convdb.txt";
open( OUTPUT, ">$OUTFILE") || do {
    print STDERR "Unable to open '$OUTFILE' for output\n";
    exit(1);
};

#
# Read & Dump out the MM to format convdb can use
print "Reading MM Database\n";
&dump_MM;
print "    converted ", $softwareCount, " records\n";
close( OUTPUT );
exit( 0 );

# ----------------------------------------------------------------------
# End of Main routine
# ----------------------------------------------------------------------


# ----------------------------------------------------------------------
# &dump_MM()
#
# Generate a platform support cross reference
#
sub dump_MM {

    &read_MM();

    foreach $pn (@parts) {

	@date_items = split (m|/|, $MMData{$pn,$DateIdx} );
	if ( $date_items[2] < 90 ) {
	    $year = "20" . $date_items[2];
	} else {
	    $year = "19" . $date_items[2];
	}

	$month = "Jan" if ( $date_items[0] == 1 );
	$month = "Feb" if ( $date_items[0] == 2 );
	$month = "Mar" if ( $date_items[0] == 3 );
	$month = "Apr" if ( $date_items[0] == 4 );
	$month = "May" if ( $date_items[0] == 5 );
	$month = "Jun" if ( $date_items[0] == 6 );
	$month = "Jul" if ( $date_items[0] == 7 );
	$month = "Aug" if ( $date_items[0] == 8 );
	$month = "Sep" if ( $date_items[0] == 9 );
	$month = "Oct" if ( $date_items[0] == 10 );
	$month = "Nov" if ( $date_items[0] == 11 );
	$month = "Dec" if ( $date_items[0] == 12 );

	$day =$date_items[1] ;

	next if ( $day <= 0); # skip if continuation record from meeting maker

	@start_items  = split ( m|:|,$MMData{$pn,$StartTimeIdx});
	$start_hour   = $start_items[0];
	$start_minutes = $start_items[1];

	@duration_items  = split ( m|:|,$MMData{$pn,$DurationIdx});
	$duration_hours   = $duration_items[0];
	$duration_minutes = $duration_items[1];

	$end_minutes = $start_minutes + $duration_minutes;
	if ( $end_minutes >= 60 ) {
	    $end_minutes = $end_minutes - 60;
	    $duration_hours++;
	}

	$end_hour = $start_hour + $duration_hours;

	if ( $end_hour > 23 ) {
	    print OUTPUT "# Wrapping days not allowed...set to 11:59 p.m.\n";
	    $end_hour = 23;
	    $end_minutes = 59;
	}

	$desc = $MMData{$pn,$TitleIdx} . " @ " . $MMData{$pn,$LocationIdx};

# here is what output to convdb wants 
# 1997     Nov     20     8     0     9     0     None     Delete IE4.0 

	printf OUTPUT "%d\t%s\t%d\t%d\t%d\t%d\t%d\tNone\t%s\n",
	     $year, $month, $day,
             $start_hour,$start_minutes,
             $end_hour,$end_minutes,
             $desc;

	&display_product($pn, OUTPUT) if ($debug >= 2);
    }
}

# ----------------------------------------------------------------------
# &read_MM()
# ----------------------------------------------------------------------

sub read_MM {
    open(MM, "<$MM") || do {
	print STDERR "Couldn't open the MM, $MM\n";
	exit( 1 );
    };

    $state = 0;
    $softwareCount = 0;
    $MMLine = 0;

    while (<MM>) {
	$MMLine++;
	chop;
	next if /^\s*$/;

        if (m|^Events|) {
	    $state = 1;
	    next;
	}

        if (m|^Todos|) {
	    $state = 2;
	    next;
	}

	&extractEvent($_)  if ($state == 1 );
    }
    close(MM);
}


sub extractEvent {
    local($_) = @_;

    if( $softwareCount == 0 ) {

# If the first time, Load the index mapping based on the header.  
# This ensures that IF the order of fields is changed, we can still 
# find the data we wnat ( as long as the title of the "column" stays the 
# same)

	$f_title = $f_location = $f_date = $f_start = $f_duration = -1;
	$f_private = $f_flexible = $f_label = $f_agenda =  -1;
	
	@fields = split( /\t/, $_ );
	# For all the fields on the line, split it up, and match them
	for (0..$#fields) {
	    $target = $fields[$_];

	    
	    $f_title       = $_, next if( $target =~ m|Title|i );
	    $f_location    = $_, next if( $target =~ m|Location|i );
	    $f_date        = $_, next if( $target =~ m|Date|i );
	    $f_start       = $_, next if( $target =~ m|Start Time|i );
	    $f_duration    = $_, next if( $target =~ m|Duration|i );
	    $f_private     = $_, next if( $target =~ m|Private|i );
	    $f_flexible    = $_, next if( $target =~ m|Flexible|i );
	    $f_label       = $_, next if( $target =~ m|Label|i );
	    $f_agenda      = $_, next if( $target =~ m|Agenda/Notes|i );
        }			# End of for loop
    }				# End of initialization test
    $softwareCount++;	# Increment count of lines processed


# Split the input line (TAB Delimited text), and map them based on the above
# index values we created
#
    @items  = split( /\t/, $_ );

# Don't store the titles when processing software
    return if( $items[ $f_title ] EQ "Title" );

# Increment index, and create entry
    $pn++;
    push(@parts,$pn);
    
    for ($idx = 1; $idx <= $MMLineIdxx; $idx++ ) {
	$MMData{$pn,$idx} = "";  
    }				 

# Done with filters, so ADD this to the data structure
    $MMData{$pn,$TitleIdx}      = $items[ $f_title ];
    $MMData{$pn,$LocationIdx}   = $items[ $f_location ];
    $MMData{$pn,$DateIdx}       = $items[ $f_date ];
    $MMData{$pn,$StartTimeIdx}  = $items[ $f_start ];
    $MMData{$pn,$DurationIdx}   = $items[ $f_duration ];
    $MMData{$pn,$PrivateIdx}    = $items[ $f_private ];
    $MMData{$pn,$FlexibleIdx}   = $items[ $f_flexible ];
    $MMData{$pn,$LabelIdx}      = $items[ $f_label ];
    $MMData{$pn,$AgendaIdx}     = $items[ $f_agenda ];
    
# These are just for keeping track of where/what's going on...
    $MMData{$pn,$reportedIdx}	    = 0;
    $MMData{$pn,$MMLineIdx}	    = $pn;
    
    
# Debug output
# Just the input line
    if( $debug >= 1 ) {
	($line = $_) =~ s/\t/|/g;
	printf "#%s\n#\n", $line;
    }
}
			       
#
# This is used to dump out a formatted report of the MM entry (software).
# It is used in a report, and also as part of debug (if debug >=3)
#
sub display_product {
    local($pn, *DEFFILE) = @_;

    print OUTPUT "#\n#Title:         $MMData{$pn,$TitleIdx}\n";
    print OUTPUT "#Location:      $MMData{$pn,$LocationIdx}\n";
    print OUTPUT "#Date:          $MMData{$pn,$DateIdx}";
    print OUTPUT "\tStart Time:    $MMData{$pn,$StartTimeIdx}";
    print OUTPUT "\tDuration:      $MMData{$pn,$DurationIdx}\n";
    print OUTPUT "#Private:       $MMData{$pn,$PrivateIdx}"  if ($debug >= 3);
    print OUTPUT "\t\tFlexible:      $MMData{$pn,$FlexibleIdx}" if ($debug >= 3);
    print OUTPUT "\tLabel:         $MMData{$pn,$LabelIdx}\n" if ($debug >= 3);
    print OUTPUT "#Agenda/Notes:  $MMData{$pn,$AgendaIdx}\n" if ($debug >= 3);
    print OUTPUT "#------------------------------------------------------\n#\n";
}

__END__
