#  ShutDownSample.pl
#  Example 8.11:
#  ----------------------------------------
#  From "Win32 Perl Scripting: Administrators Handbook" by Dave Roth
#  Published by New Riders Publishing.
#  ISBN # 1-57870-215-1
#
#  This script is a Win32 daemon script that demonstrates how
#  the SERVICE_CONTROL_SHUTDOWN control message can be processed.
#
print "From the book 'Win32 Perl Scripting: The Administrator's Handbook' by Dave Roth\n\n";

use Win32::Daemon;
Win32::Daemon::StartService();
while( SERVICE_STOPPED != ( $State = Win32::Daemon::State() ) )
{
  if( SERVICE_START_PENDING == $State )
  {
    Win32::Daemon::State( SERVICE_RUNNING );    
  }
  elsif( SERVICE_PAUSE_PENDING == $State )
  {
    Win32::Daemon::State( SERVICE_PAUSED );
  }
  elsif( SERVICE_CONTINUE_PENDING == $State )
  {
    Win32::Daemon::State( SERVICE_RUNNING );
  }
  elsif( SERVICE_STOP_PENDING == $State )
  {
    Win32::Daemon::State( SERVICE_STOPPED );
  }
  elsif( SERVICE_CONTROL_SHUTDOWN == $State )
  {
    # Request 45 seconds to shutdown...
    Win32::Daemon::State( SERVICE_STOP_PENDING, 45 );
    # Add code here to shut down
    Win32::Daemon::State( SERVICE_STOPPED );
  }
  elsif( SERVICE_RUNNING == $State )
  {
    # Add code for the running state
  }
  else
  {
    # Un-handled control messages
  }
}
Win32::Daemon::StopService();
