# ShutdownServer.pm by Daniel Butler (kefa on slim forums) December 2005 v0.02
# based on RunScript.pm plugin by Larry Narraway

# This code is derived from code with the following copyright message:
#
# SliMP3 Server Copyright (C) 2001 Sean Adams, Slim Devices Inc.
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License,
# version 2.

package Plugins::ShutdownServer;

use strict;

our %functions = ();
our @menuOptions;
our %menuSelection;

sub getDisplayName {
	return 'PLUGIN_NAME';
}

sub myscriptname {
	# This part is dodgy - need to find out if path characters change with OS and handle accordingly.
	my @filepath = split(m;/;, shift);	
	return $filepath[$#filepath];
}

sub lines {
	my $client = shift;
	my $menuText;

	# Display Shutdown timer menu unless timer is already set in which case show cancel menu
	if (Slim::Utils::Timers::pendingTimers(0, \&shutdownServer) > 0) {
		$menuText = $client->string('PLUGIN_CANCEL_SHUTDOWN');	
		$menuText .= $client->string('PLUGIN_MENU_PRESS_PLAY');
	} else {
        	$menuText = $client->string('PLUGIN_MENU_TEXT');
        	$menuText .= $menuOptions[$menuSelection{$client}];
		$menuText .= $client->string('PLUGIN_MENU_PERIOD');
		$menuText .= $client->string('PLUGIN_MENU_PRESS_PLAY');
	}

	return ($client->string('PLUGIN_NAME'), $menuText);
}


sub setMode {
	my $client = shift;

	# Provide a list of shutdown delay options
	@menuOptions = (0, 5, 10, 20, 30, 45, 60);

	unless (defined($menuSelection{$client})) {
		$menuSelection{$client} = 0;
	}

	$client->lines(\&lines);
}

sub addMenu {
	return "PLUGINS";
}

sub initPlugin {
	%functions = (
		'up' => sub {
			my $client = shift;
			my $newPos = Slim::Buttons::Common::scroll($client, -1, $#menuOptions + 1, $menuSelection{$client});
			if ($newPos != $menuSelection{$client}) {
				$menuSelection{$client} = $newPos;
				$client->pushUp();
			}
		},

		'down' => sub {
			my $client = shift;
			my $newPos = Slim::Buttons::Common::scroll($client, +1, $#menuOptions + 1, $menuSelection{$client});
			if ($newPos != $menuSelection{$client}) {
				$menuSelection{$client} = $newPos;
				$client->pushDown();
			}
		},

		'left' => sub {
			my $client = shift;
			Slim::Buttons::Common::popModeRight($client);
		},

		'right' => sub {
			my $client = shift;
			$client->bumpRight();
		},

		'play' => sub {
			my $client = shift;
			my $runningText;

			# This isn't completely multi-client safe but it shouldn't error out 
			if (Slim::Utils::Timers::pendingTimers(0, \&shutdownServer) > 0) {
				$runningText = $client->string('PLUGIN_NOTIFY_CANCELLING_SHUTDOWN');
				Slim::Utils::Timers::killTimers(0, \&shutdownServer);
			} else {
				$runningText = $client->string('PLUGIN_NOTIFY_RUNNING');
        			$runningText .= $menuOptions[$menuSelection{$client}];
				$runningText .= $client->string('PLUGIN_MENU_PERIOD');
				$runningText .= "...";

				# Kick off slimserver timer that will call shutdownServer
				Slim::Utils::Timers::setTimer(0, Time::HiRes::time() + ($menuOptions[$menuSelection{$client}] * 60), \&shutdownServer); 
			}

			# Display action notification
			Slim::Buttons::Common::popModeRight($client);
			$client->showBriefly('', $runningText);
		}
	);
}

sub shutdownServer {
	my $client = shift;
	my $pid;
	my $scriptfile = Slim::Utils::Prefs::get("plugin_shutdown_name");
	if ($pid = fork) {
		# in the parent branch - do nothing except show message and continue execution
		$client->showBriefly('', $client->string('PLUGIN_NOTIFY_SHUTTING_DOWN'));
	} else {
		exec $scriptfile;
	}

}

sub getFunctions {
	\%functions;
}

sub setupGroup {
	my %Group = (
		PrefOrder => ['plugin_shutdown_name'],
		GroupHead => Slim::Utils::Strings::string('PLUGIN_NAME'),
		GroupDesc => Slim::Utils::Strings::string('PLUGIN_DESCRIPTION'),
		GroupLine => 1,
		GroupSub => 1,	
		Suppress_PrefSub => 1,
		Suppress_PrefLine => 1,
	);

	my %Prefs = (
		plugin_shutdown_name => {
			'validate' => \&Slim::Web::Setup::validateIsFile,
			'PrefChoose' => Slim::Utils::Strings::string('PLUGIN_SHUTDOWN_SCRIPT_SET'),
                        'changeIntro' => Slim::Utils::Strings::string('PLUGIN_SHUTDOWN_SCRIPT_SET'),
			'PrefSize' => 'large'
		}
	); 

	return( \%Group, \%Prefs );
}

sub strings {
	return '

PLUGIN_NAME
	EN	Shutdown Server

PLUGIN_DESCRIPTION
	EN	Provides a sleep function by shutting down the server after a duration configured by the user via the remote control. It works by running the operating system specific script configured below. Slimserver must have sufficient permissions to run the script. 

PLUGIN_MENU_TEXT
	EN	Shutdown in 

PLUGIN_MENU_PERIOD
	EN	 mins

PLUGIN_MENU_PRESS_PLAY
	EN	 (press Play)

PLUGIN_NOTIFY_RUNNING
	EN	Server will shutdown in 

PLUGIN_NOTIFY_SHUTTING_DOWN
	EN	Shutting down now... 

PLUGIN_SHUTDOWN_SCRIPT_SET
	EN	Location of the server shutdown script  

PLUGIN_CANCEL_SHUTDOWN
	EN	Cancel shutdown

PLUGIN_NOTIFY_CANCELLING_SHUTDOWN
	EN	Cancelling server shutdown...

SETUP_PLUGIN_SHUTDOWN_NAME
	EN	Shutdown Script Location

';}

1;

__END__
