#!/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/osowner/bin/opensim";
my $OPENSIMDIR = "/home/osowner/opensim";

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

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

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

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

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

}

sub MainMenu
{
	my $WantRespawn="ON";
	CheckServerStatus();
	if (-f "$OPENSIMDIR/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', 'Edit GridCommon.ini',
                                      '8', 'Edit Regions.ini',
                                      '9', 'Back Up Opensim',
                                      'q', 'Quit OMC' ] );
}

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 OMC..." );
		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 Opensim console and return to OMC press CTRL-A CTRL-D" );
		system("screen -r");
	}
	elsif ($menuselection eq "4")
	{
		# Turn off respawn
		system("touch $OPENSIMDIR/nostart");
	}
	elsif ($menuselection eq "5")
	{
		# Turn respawn back on
		if (-f "$OPENSIMDIR/nostart")
		{
			unlink("$OPENSIMDIR/nostart");
		}
	}
	elsif ($menuselection eq "6")
	{
		# Edit OpenSim.ini file
		system("$FileEditor $OPENSIMDIR/bin/OpenSim.ini");
	}
	elsif ($menuselection eq "7")
	{
		# Edit bin/config-include/GridCommon.ini file
		system("$FileEditor $OPENSIMDIR/bin/config-include/GridCommon.ini");
	}
	elsif ($menuselection eq "8")
	{
		# Edit Regions/Region.ini file
		system("$FileEditor $OPENSIMDIR/bin/Regions/Regions.ini");
	}
	elsif ($menuselection eq "9")
	{
		# Run a backup
		system("/home/osowner/OpensimBackup/opensimbackup.pl -snapshot");
	}
}

exit 0;
