#!/usr/local/bin/perl
use strict;

use ANSIColor;
use WrapDBI;
package wake_up;

sub alarm {

    my $db = 'gull';
    ###############################################################
    # How many seconds more should we allow the user to continue ?
    ###############################################################
    my $dbh = WrapDBI->connect("$db", {RaiseError => 1});
    my $s = "select TO_CHAR(sysdate, 'SSSSS') from dual";
    my $sth = $dbh->prepare($s) || die "ERROR:$DBI::errstr";
    $sth->execute || die "ERROR:$DBI::errstr";
    my ($seconds) = $sth->fetchrow();
    $sth->finish();
    $dbh->disconnect;
    #warn "seconds into day -> $seconds\n";
    $seconds = (86399 - $seconds);

    # hack
    #$seconds = 10;

    #warn "Alarm started... you have $seconds seconds to comply !\n";
    $SIG{ALRM} = \&time_to_die;
    alarm ($seconds);
}

sub time_to_die {
    print Term::ANSIColor::color("bold"), "Gull has flown away !\n", Term::ANSIColor::color("reverse"), "Your gull session has been terminated for routine database maintenence.\n", Term::ANSIColor::color("reset");
    exit;
}

1;
