#!/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/argentumowner/bin/argentumage";
my $ARGENTUMDIR = "/home/argentumowner/anura";
my $BackupCommand = "/home/argentumowner/ArgentumBackup/argentumbackup.pl";

###################################################
# 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 argentum";

my $AMC_ver = "1.1";
my $ServerStatus = "foo";

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

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

my $windowtitle = "Welcome to the Argentum Age Management Console!";
my $enjoyedtitle = "We hope you enjoyed using AMC!";
my $introtext =
"This is the Argentum Age Management Console, a utility for Argentum Age 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 citadel|grep -v grep`;
	if ($running ne "")
	{
		$ServerStatus = "Running";
	}
	else
	{
		$ServerStatus = "Stopped";
	}

}

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

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

	$menuselection = $d->menu( title => "Main Menu", text => "Server is $ServerStatus and respawn is $WantRespawn - Select one:",
                            list => [ '1', 'Server Console',
                                      '2', 'Turn Off Respawn',
                                      '3', 'Turn Respawn On',
                                      '4', 'Edit run.sh',
                                      '5', 'Run a Backup',
                                      '6', 'Kill Argentum Age',
                                      'q', 'Quit AMC' ] );
}

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 AMC..." );
		exit 0;
	}
	elsif ($menuselection eq "1")
	{
		$d->msgbox( text => "To exit the Argentum Age console and return to AMC press CTRL-A CTRL-D" );
		system("screen -r ArgentumAge");
	}
	elsif ($menuselection eq "2")
	{
		# Turn off respawn
		system("touch $ARGENTUMDIR/nostart");
	}
	elsif ($menuselection eq "3")
	{
		# Turn respawn back on
		if (-f "$ARGENTUMDIR/nostart")
		{
			unlink("$ARGENTUMDIR/nostart");
		}
	}
	elsif ($menuselection eq "4")
	{
		# Edit run.sh file
		system("$FileEditor $ARGENTUMDIR/run.sh");
	}
	elsif ($menuselection eq "5")
	{
		# Run a backup
		system("$BackupCommand");
	}
	elsif ($menuselection eq "6")
	{
		# Kill Argentum Age server process
		my $running=`ps ax|grep citadel|grep -v grep`;
	        if ($running ne "")
		{
			# Process is running, kill it
			print "Killing process, please wait...";
			KillArgentum();
		}
	}
}

exit 0;
