# reboot.pl - reboot Win32 computers

my $VERSION = '1.00';

use strict;
use Win32;
use Getopt::Long;
use FindBin qw($Script);

my %opt;
my %param;

GetOptions(
	"help|?"		=> \$opt{Help},
	"timeout=i"		=> \$opt{Timeout},
	"shutdown"		=> \$opt{Shutdown},
	"message=s"		=> \$opt{Message},
	"debug"         => \$opt{Debug},
);

Help() and exit(0) if defined $opt{Help};

%param = (    
	Computer    => $ARGV[0] || Win32::NodeName(),
	Message		=> $opt{Message} || "System is shutting down!",
	Timeout		=> $opt{Timeout} || 30,
	ForeClose	=> 1,  # 0 = do not force open applications to close, 1 = force
	Shutdown	=> 1,  # 0 = power down, do not restart, 1 = restart
);
$param{Shutdown} = 0 if $opt{Shutdown};


if ( defined $opt{Debug} )
{
	print "-Computer   : $param{Computer}\n",
		  "-Message    : $param{Message}\n",
		  "-Timeout    : $param{Timeout}\n",
		  "-ForeClose  : $param{ForeClose} (0 = no force   | *1 = force)\n",
		  "-Shutdown   : $param{Shutdown} (0 = power down | *1 = restart)\n",
		  " * = default";
}
else
{
	print "Sending System Shutdown Command to \U$param{Computer}\E...\n";
	Win32::InitiateSystemShutdown(
				$param{Computer},
				$param{Message},
				$param{Timeout},
				$param{ForeClose},
				$param{Shutdown},
	) || die "Failed to initiate shutdown.\n $^E\n";
}


# functions
sub Help
{
print qq(
\U$Script\E, v$VERSION - reboot Win32 computers
by Lior P. Abitbol <labitbol\@cpan.org>

Usage: 

 $Script [Computer] [OPTIONS]

Options:

  computer    : Name or IP address of the computer to reboot. Local
                computer will be rebooted if you don't specify a
                computer name.
  
  --help      : displays this screen

  --timeout   : amount in seconds before initiating the restart (by default
                the timeout period is 30 seconds).

  --shutdown  : turns off the computer (by default the computer is restarted).

  --message   : a short message to display to the currently logged on user.
                Message must be encapsulated with double quotes (\"\").
  
  All options can be passed in the short form too (e.g. -t for --timeout,
   -s for --shutdown, -h for --help and so on).

  * Note that you will need administrative privileges to use this script.
);
}

# pod documenation

=head1 NAME

Reboot.pl - reboot Win32 computers

=head1 README

Reboot or shutdown local or remote Win32 based computers. 

Run "reboot.pl -h" for options and help.

=head1 DESCRIPTION

This program can be used to restart or shutdown the local computer
or a remote computer.

Please note that this program will force all opened programs to 
close without saving. Use caution. Also you will need administrative 
privileges to use this program.

SYNTAX:

   reboot.pl [computer] [OPTIONS]

OPTIONS:

   computer - Name or IP address of the computer to reboot. Local
              computer will be rebooted if you don't specify a
              computer name.

   timeout  - interval in seconds before the computer reboots. A
              countdown screen will appear on the computer being
              rebooted. By default the program counts to 30 seconds.
   
   shutdown - leaves the computer powered off after the reboot. By
              default the computer will restart.
			  
   message  - displays a short message to the logged on user before 
              rebooting. By default the "System is shutting down!"
              message will be displayed to the currently logged on user.

These options may be abbreviated. Here are two examples:

   reboot Computer1 -t 60 -m "Shutting Down for Maintenance" -s

   reboot Computer2 --timeout 0

=head1 PREREQUISITES

PERL 5.6.1 or greater

This script requires: 	
	Win32,	
	Getopt::Long,
	FindBin,

=head1 COREQUISITES

None

=head1 OSNAMES

MSWin32

=head1 SCRIPT CATEGORIES

Win32
Win32/Utilities

=head1 AUTHOR

Lior P. Abitbol (labitbol@cpan.org).

=cut