#!/usr/bin/perl -wT

#----------------------------------------------------------------------
# copyright (C) 1999-2003 Mitel Networks Corporation
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
#
# Technical support for this program is available from Mitel Networks
# Please visit our web site www.mitel.com/sme/ for details.
#----------------------------------------------------------------------
package esmith::console;

use strict;

use POSIX;
use Locale::gettext;
use Errno;

use esmith::util;
use esmith::ConfigDB;
use esmith::I18N;
use esmith::console;

use constant TRUE  => 1;
use constant FALSE => 0;

BEGIN
{
    # Set PATH explicitly and clear related environment variables so that calls
    # to external programs do not cause results to be tainted. See
    # "perlsec" manual page for details.

    $ENV {PATH} = "/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin";
    $ENV {SHELL} = "/bin/bash";
    delete $ENV {ENV};
    delete $ENV {BASH_ENV};
}

my $console = esmith::console->new();

my $release = esmith::util::determineRelease();
my $release_string = "Mitel Networks server ${release}";


my $i18n = new esmith::I18N;
$i18n->setLocale("server-console");

#-------------------------------------------------------------
# check whether a backup in process and incomplete
#-------------------------------------------------------------
my $restore_state;
my $restore_db = esmith::ConfigDB->open_ro('/etc/e-smith/restore');
if ($restore_db)
{
    $restore_state = $restore_db->get_prop('restore', 'state');
}

$restore_state ||= 'idle';

exit(0) unless ($restore_state eq 'running');

my ($rc, $choice) = $console->message_page
	(
         title => gettext("Inconsistent system state"),
         text  =>
         gettext("********** Inconsistent system state detected ***********") .
         "\n\n" .
         gettext("The restoration of a system backup was running and " .
	 "incomplete at the time of the last reboot. " .
	 "The system should not be used in this state.") .
         "\n\n" .
         gettext("Consult the User Guide for further instructions."),
        );

($rc, $choice) = $console->yesno_page
        (
         title => gettext("System will be halted"),
         text  =>
         gettext("The server will now be halted.") .
         "\n\n" .
         gettext("Consult the User Guide for recovery instructions.") .
         "\n\n" .
         gettext("Do you wish to halt the system right now?"),
        );

if ($rc == 0)
{
    system("/usr/bin/tput", "clear");
    system("/sbin/e-smith/signal-event", "halt");

    # A bit of a hack to avoid the console restarting before the
    # reboot takes effect.
    pause();
}
exit 0;
