+sub start_track ($$;$)
+{
+ my $start_time = shift;
+ my $title = shift;
+
+ my $trk_id = shift;
+ my $wrk_dir = $trk_dir;
+ $wrk_dir = $trk_dir . "/" . $trk_id if $trk_id;
+
+ my $current = get_current_id($trk_id);
+ if ( not $current )
+ {
+ if ( not $title )
+ {
+ $current = get_last_id( $trk_id );
+ }
+ else
+ {
+ $current = get_track_id( $title, $trk_id );
+ if ( not $current )
+ {
+ printf("No track by that name! Creating a new one.\n");
+ $current = create_track($title, $trk_id);
+ }
+ }
+
+ # Break off here if we haven't gotten an ID yet.
+ return undef if not $current;
+
+ set_current_id($current, $trk_id);
+
+ # First iteration is VERY naive: simply add the start time to the bottom of the tracking file
+ # Will have to do more logic: if the start point is before one of the times already in the track,
+ # the file will have to be manipulated to get coherent tracking!
+ open (TRACK, ">>" . $wrk_dir . "/" . $current . "/tracking" ) or die ("Unable to open file, $!");
+ printf(TRACK "[%s]", time2str($start_time));
+ close (TRACK);
+
+ return $current;
+ }
+
+ return undef;
+}
+
+sub close_track ($;$)
+{
+
+ my $stop_time = shift;
+ my $trk_id = shift;
+ my $wrk_dir = $trk_dir;
+ $wrk_dir = $trk_dir . "/" . $trk_id if $trk_id;
+
+ my $current = get_current_id( $trk_id );
+
+ die ("Project exists, but tracking file does not!") if ( not -f $wrk_dir . "/" . $current . "/tracking" );
+
+ # First iteration is VERY naive: simply add the stop time to the bottom line of the tracking file
+ # Will have to do more logic: if the start point is before one of the times already in the track,
+ # the file will have to be manipulated to get coherent tracking!
+ # In addtion to this: actually do some file sanity checking!
+ open (TRACK, ">>" . $wrk_dir . "/" . $current . "/tracking" ) or die ("Unable to open file, $!");
+ printf(TRACK " to [%s]\n", time2str($stop_time));
+ close (TRACK);
+
+ unlink ( $wrk_dir . "/current" );
+}
+
+
+sub report ($$;$)
+{
+ my $current = shift;
+ my $silent = shift;
+ my $trk_id = shift;
+ my $wrk_dir = $trk_dir;
+ $wrk_dir = $trk_dir . "/" . $trk_id if $trk_id;
+
+ my $total = 0;
+
+ my $name = get_track_name( $current, $trk_id );
+ printf("# Report for '%s':\n\n", $name) unless $silent;
+
+ open (TRACK, "<" . $wrk_dir . "/" . $current . "/tracking" ) or die ("Unable to open file, $!");
+ while ( <TRACK> )
+ {
+ next if not $_ =~ m/\[(.*)\] to \[(.*)\]/;
+ my $start = $1;
+ my $end = $2;
+ my $t_start = str2time( $start );
+ my $t_end = str2time( $end );
+ my $delta = $t_end - $t_start;
+
+ if ( not $silent )
+ {
+ my $t = $delta;
+ my $hours = $t / 3600;
+ $t = $delta % 3600;
+ my $minutes = $t / 60;
+ printf(" %s to %s => %d hours %d minutes\n", $start, $end, $hours, $minutes);
+ }
+ $total += $delta;
+
+ }
+ close ( TRACK );
+ return $total;
+
+}
+