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

use UI::Dialog;
use Term::ReadKey;
use Term::ANSIScreen qw(cls);
use Proc::ProcessTable;

my $FileEditor = "/bin/nano";
my $InitDName = "/home/empowner/bin/empire";
my $EMPIREDIR = "/home/empowner/empire";
my $BackupCommand = "/home/empowner/EmpireBackup/empirebackup.pl";
my $ServerLogFile = "/home/empowner/empire/var/empire/server.log";
my $ScheduleFile = "/home/empowner/empire/etc/empire/schedule";
my $ServerConfigFile = "/home/empowner/empire/etc/empire/econfig";
my $Pager = "/usr/bin/less";

###################################################
# No changes below here
###################################################

# Read in host name
my $HOSTFILE = "/etc/hostname";
open FILE, $HOSTFILE or die "Couldn't open file: $HOSTFILE";
my $HOST = <FILE>;
close FILE;
chop($HOST);

my $KILL_COMMAND = "killall emp_server";

my $EMC_ver = "1.0.1";
my $ServerStatus = "foo";

if ($ENV{'EDITOR'})
{
        $FileEditor = $ENV{'EDITOR'};
}

my $d = new UI::Dialog ( backtitle => "Empire Management Console v$EMC_ver", height => 20, width => 65, listheight => 5,
	order => [ 'ascii', 'cdialog', 'xdialog' ]);

my $windowtitle = "Welcome to the Empire Management Console!";
my $enjoyedtitle = "We hope you enjoyed using EMC!";
my $introtext =
"This is the Empire Management Console, a utility for Empire operators
to manage their servers from a text GUI rather than the command line.";

$d->msgbox( title => $windowtitle, text => $introtext );

if (($d->state() eq "ESC") || ($d->state() eq "CANCEL"))
{
	exit 0;
}

my $menuselection = "";

sub CheckServerStatus
{
	my $running=`ps ax|grep emp_server|grep -v grep`;
	if ($running ne "")
	{
		$ServerStatus = "Running";
	}
	else
	{
		$ServerStatus = "Stopped";
	}

}

sub KillEmpire
{
	system($KILL_COMMAND);
	sleep (10);
}

sub MainMenu
{
	my $WantRespawn="ON";
	CheckServerStatus();
	if (-f "$EMPIREDIR/nostart")
	{
		$WantRespawn="OFF";
	}

	$menuselection = $d->menu( title => "Main Menu", text => "Server is $ServerStatus and respawn is $WantRespawn - Select one:",
                            list => [ '1', 'Start Watchdog',
                                      '2', 'Stop Watchdog',
                                      '3', 'Server Console',
                                      '4', 'Turn Off Respawn',
                                      '5', 'Turn Respawn On',
                                      '6', 'Edit econfig',
                                      '7', 'Edit Schedule',
                                      '8', 'Run a Backup',
                                      '9', 'Kill Empire',
                                      '10', 'View Log',
                                      'q', 'Quit EMC' ] );
}

while (-1)
{
	MainMenu();
	if (($menuselection eq "CANCEL") || ($menuselection eq "ESC") || ($menuselection eq "") || ($menuselection eq "q") || ($menuselection eq "Q"))
	{
		$d->msgbox( title => $enjoyedtitle, text => "Thanks for using EMC..." );
		exit 0;
	}
	if ($menuselection eq "1")
	{
		system("$InitDName start");
	}
	elsif ($menuselection eq "2")
	{
		if ($d->yesno( text => "Confirm stopping the watchdog", text => "Are you sure you want to shut down the Empire watchdog?" ))
		{
			system("$InitDName stop");
			sleep(5);
		}
	}
	elsif ($menuselection eq "3")
	{
		$d->msgbox( text => "To exit the Empire console and return to EMC press CTRL-A CTRL-D" );
		system("screen -r EmpireServer");
	}
	elsif ($menuselection eq "4")
	{
		# Turn off respawn
		system("touch $EMPIREDIR/nostart");
	}
	elsif ($menuselection eq "5")
	{
		# Turn respawn back on
		if (-f "$EMPIREDIR/nostart")
		{
			unlink("$EMPIREDIR/nostart");
		}
	}
	elsif ($menuselection eq "6")
	{
		# Edit server.cfg file
		system("$FileEditor $ServerConfigFile");
	}
	elsif ($menuselection eq "7")
	{
		# Edit schedule file
		system("$FileEditor $ScheduleFile");
	}
	elsif ($menuselection eq "8")
	{
		# Run a backup
		system("$BackupCommand");
	}
	elsif ($menuselection eq "9")
	{
		# Kill Empire server process
		my $running=`ps ax|grep emp_server|grep -v grep`;
	        if ($running ne "")
		{
			# Process is running, kill it
			print "Killing process, please wait...";
			KillEmpire();
		}
	}
	elsif ($menuselection eq "10")
	{
		# Display Log File
                system("$Pager $ServerLogFile");
	}
}

exit 0;
