#!/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/wesnothowner/bin/wesnoth-server";
my $WESNOTHDIR = "/home/wesnothowner/wesnoth";
my $BackupCommand = "/home/wesnothowner/WesnothBackup/wesnothbackup.pl";

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

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

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

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

}

sub MainMenu
{
	my $WantRespawn="ON";
	CheckServerStatus();
	if (-f "$WESNOTHDIR/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 server.cfg',
                                      '5', 'Run a Backup',
                                      '6', 'Reset Wesnoth Server',
                                      'q', 'Quit WMC' ] );
}

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 WMC..." );
		exit 0;
	}
	elsif ($menuselection eq "1")
	{
		$d->msgbox( text => "To exit the Wesnoth Server console and return to WMC press CTRL-A CTRL-D" );
		system("screen -r Wesnoth");
	}
	elsif ($menuselection eq "2")
	{
		# Turn off respawn
		system("touch $WESNOTHDIR/nostart");
	}
	elsif ($menuselection eq "3")
	{
		# Turn respawn back on
		if (-f "$WESNOTHDIR/nostart")
		{
			unlink("$WESNOTHDIR/nostart");
		}
	}
	elsif ($menuselection eq "4")
	{
		# Edit server config file
		system("$FileEditor $WESNOTHDIR/wesnothC");
	}
	elsif ($menuselection eq "5")
	{
		# Run a backup
		system("$BackupCommand");
	}
	elsif ($menuselection eq "6")
	{
		# Reset Wesnoth Server
		system("$BackupCommand");
		print "Killing Wesnoth process, please wait...\n";
		system("killall wesnothd");
		sleep(5);
	}
}

exit 0;
