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

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

my $FileEditor = "/bin/nano";
my $InitDName = "/home/megamek/bin/megamek";
my $PAGER = "/usr/bin/less";
my $MEGAMEKDIR = "/home/megamek/megamek";

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

my $MMC_ver = "1.1.2";
my $ServerStatus = "foo";

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

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

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

}

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

	$menuselection = $d->menu( title => "Main Menu", text => "Server is $ServerStatus and respawn is $WantRespawn - Select one:",
                            list => [ '1', 'Start Server',
                                      '2', 'Stop Server',
                                      '3', 'Server Console',
                                      '4', 'Turn Off Respawn',
                                      '5', 'Turn On Respawn',
                                      '6', 'Edit Unit Roles',
                                      '7', 'Back Up MegaMek',
                                      '8', 'MegaMek Log',
                                      '9', 'Kill MegaMek',
                                      'q', 'Quit MEGAMC' ] );
}

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 MEGAMC..." );
		exit 0;
	}
	if ($menuselection eq "1")
	{
		system("$InitDName start");
	}
	elsif ($menuselection eq "2")
	{
		if ($d->yesno( text => "Confirm stopping the server", text => "Are you sure you want to shut down the server?" ))
		{
			system("$InitDName stop");
			sleep(5);
		}
	}
	elsif ($menuselection eq "3")
	{
		$d->msgbox( text => "To exit the MegaMek console and return to MEGAMC press CTRL-A CTRL-D" );
		system("screen -r");
	}
	elsif ($menuselection eq "4")
	{
		# Turn off respawn
		system("touch $MEGAMEKDIR/nostart");
	}
	elsif ($menuselection eq "5")
	{
		# Turn respawn back on
		if (-f "$MEGAMEKDIR/nostart")
		{
			unlink("$MEGAMEKDIR/nostart");
		}
	}
	elsif ($menuselection eq "6")
	{
		# Edit OpenSim.ini file
		system("$FileEditor $MEGAMEKDIR/data/unit_roles.txt");
	}
	elsif ($menuselection eq "7")
	{
		# Run a backup
		system("/home/megamek/MegamekBackup/megamekbackup.pl");
	}
	elsif ($menuselection eq "8")
	{
		# View the main log file
		system("$PAGER $MEGAMEKDIR/logs/megamek.log");
	}
	elsif ($menuselection eq "9")
	{
		# Kill the MegaMek server process
		print "Killing process, please wait...";
		#system("killall /home/megamek/minetest/bin/startminetest");
		system("killall MegaMek.jar");
                sleep(5);
	}
}

exit 0;
