#!/usr/bin/perl
# 
# This manages power on the host. It can set that a reboot is or is no longer required. It can also reboot or
# power off the machine. 
# 
# Examples;
# - Mark that a reboot is required           - anvil-manage-power --reboot-needed 1
# - Clear that a reboot is needed            - anvil-manage-power --reboot-needed 0
# - Report whether a reboot is needed or not - anvil-manage-power
# - Reboot the system                        - anvil-manage-power --reboot [-y]
# - Power the system off                     - anvil-manage-power --poweroff [-y]
# 
# Exit codes;
# 0 = Normal exit.
# 1 = No database connections available.
# 
# TODO: Don't reboot or power off until all external users are done with the database on this system (if 
#       applicable)
# 

use strict;
use warnings;
use Anvil::Tools;

# Disable buffering
$| = 1;

my $THIS_FILE           =  ($0 =~ /^.*\/(.*)$/)[0];
my $running_directory   =  ($0 =~ /^(.*?)\/$THIS_FILE$/)[0];
if (($running_directory =~ /^\./) && ($ENV{PWD}))
{
	$running_directory =~ s/^\./$ENV{PWD}/;
}

my $anvil = Anvil::Tools->new();

# Read switches (target ([user@]host[:port]) and the file with the target's password.
$anvil->Get->switches({list => [
	"no-wait", 
	"power-off",
	"poweroff", 
	"reboot",
	"reboot-needed",
	"y",
	"yes"], man => $THIS_FILE});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => $anvil->data->{switches}});
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 2, key => "log_0115", variables => { program => $THIS_FILE }});

if ($anvil->data->{switches}{'power-off'})
{
	$anvil->data->{switches}{'poweroff'} = 1;
}
if ($anvil->data->{switches}{'yes'})
{
	$anvil->data->{switches}{'y'} = 1;
}

# Connect to DBs.
$anvil->Database->connect;
$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 2, secure => 0, key => "log_0132"});
if (not $anvil->data->{sys}{database}{connections})
{
	# No databases, exit.
	print $anvil->Words->string({key => "error_0003"})."\n";
	$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, secure => 0, key => "error_0003"});
	$anvil->nice_exit({exit_code => 1});
}

# Clear the job in case a previous call failed.
if ($anvil->data->{switches}{'job-uuid'})
{
	$anvil->Job->clear({debug => 2, job_uuid => $anvil->data->{switches}{'job-uuid'}});
	my $task = $anvil->data->{switches}{'reboot'} ? "log_0226" : "log_0225";
	$anvil->Job->update_progress({
		debug     => 2, 
		progress  => 1, 
		message   => "message_0352", 
		job_uuid  => $anvil->data->{switches}{'job-uuid'},
		log_level => 1,
		variables => { task => $task },
	});
}

# Are we being asked to reboot or power off?
if ($anvil->data->{switches}{'reboot'})
{
	# Did the user confirm?
	if ($anvil->data->{switches}{'y'})
	{
		do_poweroff($anvil, "reboot");
	}
	else
	{
		# Not yet, ask to confirm.
		print $anvil->Words->string({key => "message_0059"})." ";
		my $answer = <STDIN>;
		chomp($answer);
		if ($answer =~ /^y/i)
		{
			do_poweroff($anvil, "reboot");
		}
		else
		{
			# Abort and exit.
			print $anvil->Words->string({key => "message_0061"})."\n";
			$anvil->nice_exit({exit_code => 0});
		}
	}
}
if ($anvil->data->{switches}{'poweroff'})
{
	# Did the user confirm?
	if ($anvil->data->{switches}{'y'})
	{
		do_poweroff($anvil, "poweroff");
	}
	else
	{
		# Not yet, ask to confirm.
		print $anvil->Words->string({key => "message_0060"})." ";
		my $answer = <STDIN>;
		chomp($answer);
		if ($answer =~ /^y/i)
		{
			do_poweroff($anvil, "poweroff");
		}
		else
		{
			# Abort and exit.
			print $anvil->Words->string({key => "message_0061"})."\n";
			$anvil->nice_exit({exit_code => 0});
		}
	}
}

my $reboot_needed = $anvil->System->reboot_needed({debug => 2});
$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { reboot_needed => $reboot_needed }});

$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { "switches::reboot-needed" => $anvil->data->{switches}{'reboot-needed'} }});
if ($anvil->data->{switches}{'reboot-needed'} eq "1")
{
	# Enable
	if (not $reboot_needed)
	{
		$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 1, key => "log_0687", variables => { reason => "#!string!log_0688!#" }});
		$reboot_needed = $anvil->System->reboot_needed({debug => 2, set => 1});
		$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { reboot_needed => $reboot_needed }});
		print $anvil->Words->string({key => "message_0048"})."\n";
	}
	else
	{
		# Was already set, do nothing
		print $anvil->Words->string({key => "message_0049"})."\n";
	}
}
elsif ($anvil->data->{switches}{'reboot-needed'} eq "0")
{
	# Disabled
	if ($reboot_needed)
	{
		$reboot_needed = $anvil->System->reboot_needed({debug => 2, set => 0});
		$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { reboot_needed => $reboot_needed }});
		print $anvil->Words->string({key => "message_0050"})."\n";
	}
	else
	{
		# Was already disabled, do nothing
		print $anvil->Words->string({key => "message_0051"})."\n";
	}
}
elsif ($anvil->data->{switches}{'reboot-needed'})
{
	# Bad call
	print $anvil->Words->string({key => "message_0052", variables => { program => $THIS_FILE }})."\n";
}

# Get the current state
if ($reboot_needed)
{
	# Report that we need to reboot
	print $anvil->Words->string({key => "message_0053"})."\n";
}
else
{
	# Report that we're not.
	print $anvil->Words->string({key => "message_0054"})."\n";
}

# We're done
$anvil->nice_exit({exit_code => 0});


#############################################################################################################
# Private functions.                                                                                        #
#############################################################################################################

# This does a reboot or power off
sub do_poweroff
{
	my ($anvil, $task) = @_;
	$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { task => $task }});
	
	if ($anvil->data->{switches}{'job-uuid'})
	{
		$anvil->Job->update_progress({
			debug     => 2, 
			progress  => 10, 
			message   => "message_0351", 
			log_level => 1,
			job_uuid  => $anvil->data->{switches}{'job-uuid'},
		});
	}
	
	# In case we're being called by another job, we'll sleep for a few second to let those close out.
	sleep 3;
	
	# We'll wait until the system has at least 5 minutes of uptime, unless '--no-wait' was given.
	my $uptime = $anvil->data->{switches}{'no-wait'} ? 0 : $anvil->Get->uptime;
	$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { 
		"switches::no-wait" => $anvil->data->{switches}{'no-wait'}, 
		uptime              => $uptime,
	}});
	
	my $say_task = $task eq "poweroff" ? "message_0062" : "message_0063";
	print $anvil->Words->string({key => $say_task})."\n";
	$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 2, secure => 0, key => $say_task});
	
	# To minimize the trouble of a problem where the reboot needed flag isn't cleared, and so the system 
	# wants to repeatedly reboot, we need to add a delay to not let anvil-daemon ask us to 
	# reboot/power-off until the system uptime is more than five minutes.
	if (($uptime) && ($uptime < 300))
	{
		# We'll wait until the system has been running for five minutes.
		
		my $difference = 300 - $uptime;
		$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 1, secure => 0, key => "log_0224", variables => {
			task       => $task eq "poweroff" ? "#!string!log_0225!#" : "#!string!log_0226!#",
			difference => $difference, 
			uptime     => $uptime,
			say_time   => $anvil->Get->date_and_time({offset => $difference, time_only => 1}),
		}});
		$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 2, secure => 0, key => $say_task});
		
		sleep $difference;
		$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 1, secure => 0, key => "log_0227", variables => { 
			task => $task eq "poweroff" ? "#!string!log_0225!#" : "#!string!log_0226!#",
		}});
	}
	
	# If I don't have a job_uuid, try to find one.
	my $job_uuid = "";
	$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { "switches::job-uuid" => $anvil->data->{switches}{'job-uuid'} }});
	if ($anvil->data->{switches}{'job-uuid'})
	{
		$job_uuid = $anvil->data->{switches}{'job-uuid'};
		$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { job_uuid => $job_uuid }});
	}
	
	# Make sure the 'reboot needed' flag is set. When 'anvil-daemon' starts, it will use this to confirm
	# that it is starting post-reboot and clear it.
	my $say_reason = $task eq "poweroff" ? "log_0689" : "log_0688";
	$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, level => 1, key => "log_0687", variables => { reason => "#!string!".$say_reason."!#" }});
	$reboot_needed = $anvil->System->reboot_needed({debug => 2, set => 1});
	$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { reboot_needed => $reboot_needed }});
	
	# Mark our power state.
	$anvil->Database->update_host_status({
		debug       => 2,
		host_uuid   => $anvil->Get->host_uuid,
		host_status => $task eq "poweroff" ? "rebooting" : "stopping",
	});
	
	# Wait if anvil-version-change is running.
	my $next_log = time - 1;
	my $waiting  = 1;
	$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { 
		next_log => $next_log, 
		waiting  => $waiting,
	}});
	while ($waiting)
	{
		my $pids = $anvil->System->pids({program_name => $anvil->data->{path}{exe}{'anvil-version-changes'}});
		my $avc_instances = @{$pids};
		$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { avc_instances => $avc_instances }});
		
		if ($avc_instances)
		{
			if (time > $next_log)
			{
				my $say_pids = "";
				foreach my $pid (@{$pids})
				{
					$say_pids .= $pid.", ";
				}
				$say_pids =~ s/, $//;
				$anvil->Log->entry({source => $THIS_FILE, line => __LINE__, 'print' => 1, level => 1, key => "message_0325", variables => { pids => $say_pids }});
				
				$next_log = time + 60;
				$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { next_log => $next_log }});
			}
			sleep 10;
		}
		else
		{
			$waiting = 0;
			$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { waiting => $waiting }});
		}
	}
	
	# If we have a job UUID, mark that we're done.
	if ($job_uuid)
	{
		$anvil->Job->update_progress({
			debug     => 2, 
			progress  => 100, 
			message   => $say_task, 
			log_level => 1,
			job_uuid  => $job_uuid,
		});
	}
	
	# If we're powering off, set the status now
	if ($task eq "poweroff")
	{
		$anvil->Database->update_host_status({
			debug       => 2,
			host_uuid   => $anvil->Get->host_uuid,
			host_status => "powered off",
		});
	}
	
	# Now do the deed.
	my $shell_call = $anvil->data->{path}{exe}{systemctl}." ".$task;
	$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { shell_call => $shell_call }});
	my ($output, $return_code) = $anvil->System->call({shell_call => $shell_call, source => $THIS_FILE, line => __LINE__});
	
	# Unlikely we're still alive, but 'poweroff' and 'reboot' do return once enqueued, so...
	$anvil->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { output => $output, return_code => $return_code }});
	$anvil->nice_exit({exit_code => 0});
}
