#!/usr/bin/perl
#----------------------------------------------------------------
# Copyright (c) 2002 Benjamin Crowell, all rights reserved.
#
# This software is available under version 2 of the GPL license.
# The software is copyrighted, and you must agree to the
# license in order to have permission to copy it. The full
# text of the license is given in the file titled Copying.
#
#----------------------------------------------------------------


use strict;

use FindBin;
use lib $FindBin::RealBin;
   # RealBin follows symbolic links, as opposed to Bin, which doesn't.

use GradeBook;
use TermUI;
use Term::ReadKey;

package ogr;

# We maintain the following list so that we can panic-save
# when we get an interrupt.
our @open_files = ();

catch_signals();

my ($command_line_file_argument,$gui) = parse_command_line(@ARGV);
if ($gui) {
  require Browser;
  Browser::main_loop($command_line_file_argument);
}
else {
  TermUI::main_loop($command_line_file_argument);
}

# Now exit the program.

#----------------------------------------------------------------
# helper routines:
#----------------------------------------------------------------

sub catch_signals {
  $SIG{TERM} = sub{panic('term')};
  $SIG{INT}  = sub{panic('int')};
  $SIG{QUIT} = sub{panic('quit')};
  $SIG{TSTP} = sub{panic('tstp')};
  $SIG{HUP}  = sub{panic('hup')};
  $SIG{ABRT} = sub{panic('abrt')};
#  $SIG{SEGV} = sub{panic('segv')};
      # ... segmentation violation could indicate data are corrupted, in which
      # case you wouldn't want to save to disk; however, the data is all
      # pure Perl, and when segvs occur, they're presumably occurring in
      # Perl/Tk, which means saving the data is the right thing to do.
 # However, I'm trying to debug this, so I want core dumps, so comment this out.
}

sub parse_command_line {
  my @ARGV = @_;
  my $command_line_file_argument = "";
  my $gui = 1;
  foreach my $arg(@ARGV) {
    if ($arg =~ m/\-([a-z])/) {
      my $option = $1;
      if ($option eq "t") {$gui=0}
    }
    else {
      $command_line_file_argument = $arg;
    }
  }
  return ($command_line_file_argument,$gui);
}


sub add_to_list_of_open_files {
    my $gb = shift;
    push @open_files,$gb;
}

sub remove_from_list_of_open_files {
    my $gb = shift;
    for (my $j=0; $j<=$#open_files; $j++) {
        my $x = $open_files[$j];
        if ($gb==$x) {
					  for (my $k=$j; $k<=$#open_files-1; $k++) {
                $open_files[$k] = $open_files[$k+1];
					  }
            $#open_files = ($#open_files)-1; 
        }
    }
}

# Try to do an auto-save when we get a TERM signal or something like that.
sub panic {
    my $signal = shift;
    my $list = clean_up_before_exiting();
    die "\nOpenGrade has been terminated, signal=$signal. $list\n";
}


sub clean_up_before_exiting {
    my $list = close_all();
    Term::ReadKey::ReadMode("normal"); # Otherwise the terminal can be left in a goofy mode.
    return $list;
}


sub close_all {
    my $list = "";
    foreach my $gb(@open_files) {
        if (ref($gb)) {
          $gb->auto_save();
          $list = $list . ", " . $gb->autosave_filename();
        }
    }
    if ($list eq "") {
        $list = "";
    }
    else {
        $list =~ s/^, //;
        $list = "The following files have been auto-saved: ".$list;
    }
    return $list;
}

