#!/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/twowner/bin/teeworlds";
my $TEEWORLDSDIR = "/home/twowner/twserver";
my $TEEWORLDSHOMEDIR = "/home/twowner/.teeworlds";
my $BackupCommand = "/home/twowner/TeeworldsBackup/teeworldsbackup.pl";
my $FileViewer = "/usr/bin/less";

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

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

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

my $windowtitle = "Welcome to the Teeworlds Management Console!";
my $enjoyedtitle = "We hope you enjoyed using TMC!";
my $introtext =
"This is the Teeworlds Management Console, a utility for Teeworlds 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 "teeworlds_srv"|grep -v grep`;

	if ($running ne "")
	{
		$ServerStatus = "Running";
	}
	else
	{
		$ServerStatus = "Stopped";
	}

}

sub MainMenu
{
	my $WantRespawn="ON";
	CheckServerStatus();
	if (-f "$TEEWORLDSDIR/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 twserver.cfg',
                                      '5', 'Run a Backup',
                                      '6', 'Kill Server',
                                      '7', 'View Log File',
                                      'q', 'Quit TMC' ] );
}

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 TMC..." );
		exit 0;
	}
	elsif ($menuselection eq "1")
	{
		$d->msgbox( text => "To exit the Teeworlds console and return to TMC press CTRL-A CTRL-D" );
		system("screen -r Teeworlds");
	}
	elsif ($menuselection eq "2")
	{
		# Turn off respawn
		system("touch $TEEWORLDSDIR/nostart");
	}
	elsif ($menuselection eq "3")
	{
		# Turn respawn back on
		if (-f "$TEEWORLDSDIR/nostart")
		{
			unlink("$TEEWORLDSDIR/nostart");
		}
	}
	elsif ($menuselection eq "4")
	{
		# Edit server config file
		system("$FileEditor $TEEWORLDSDIR/twserver.cfg");
	}
	elsif ($menuselection eq "5")
	{
		# Run a backup
                system("clear");
                system("$BackupCommand");
                print "Press Enter To Continue";
                my $entered = <STDIN>;

	}
	elsif ($menuselection eq "6")
        {
		my $running=`ps ax|grep "teeworlds_srv"|grep -v grep`;
                if ($running ne "")
                {
                        # Process is running, kill it
                        system("killall teeworlds_srv");
                        sleep(5);
                }
        }
	elsif ($menuselection eq "7")
	{
		# View Log File
		if (-f "$TEEWORLDSDIR/teeworlds.log")
		{
			system("$FileViewer $TEEWORLDSDIR/teeworlds.log");
		}
		else
		{
			system("clear");
			print "Log file not forund...\n";
			print "Press Enter To Continue:";
			my $entered = <STDIN>;
		}
	}
}

exit 0;
