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

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

my $FileEditor = "/bin/nano";
my $InitDName = "/etc/init.d/megamek";
my $MEGAMEKDIR = "/root/megamek";

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

my $MMC_ver = "1.0.0";
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 MegaMekProcess|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 OpenSim.ini',
                                      '7', 'Back Up 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/bin/OpenSim.ini");
	}
	elsif ($menuselection eq "7")
	{
		# Run a backup
		system("/root/MegamekBackup/megamekbackup.pl");
	}
}

exit 0;
