//   -Plugin_sank_shutdown. Stolen, coerced, silver-tongue-outta'd by Kleen13.     //
//    Thanks to Sank for wisdom, Shady Milkman for insight, and Halogen for        //
//    posting absolute rubbish at regular intervals....                            //
//   -This updated version also includes shutdown audio 6 seconds from server      //
//    termination.  Thanks to Kndroc for the Playsound function                    //                                                  
//                                                                                 //
//   -This script provides a "nice" server shutdown message (30 secs) before       //
//    exiting  HLDS.  By default, the only people authorized to use this script    //
//    are "rcon access" admins.                                                    //
//                                                                                 //
//                                                                                 //
/////////////////////////////////////////////////////////////////////////////////////
 
 
#include <core>
#include <console>
#include <string>
#include <admin>
#include <adminlib>
 
#define ACCESS_SHUTDOWN 65535
 
new STRING_VERSION[MAX_DATA_LENGTH] = "2.50.e";
 
public plugin_init()
{
	plugin_registerinfo("Sank Shutdown Plugin", "Enables soft remote shutting down of a server", STRING_VERSION);
 
	plugin_registercmd("admin_shutdown", "admin_shutdown", ACCESS_SHUTDOWN, "admin_shutdown: Starts the shutdown timer.");
 
	return PLUGIN_CONTINUE;
}
 
playShutdownSound() {
  new i;
  new iMaxPlayers = maxplayercount();
  new Name[MAX_NAME_LENGTH];
 
  if (getvar("admin_fx") != 0) {
    for (i = 1; i <= iMaxPlayers; i++) {
  		if (playerinfo(i,Name,MAX_NAME_LENGTH) != 0) {
        playsound(Name, "scientist/c1a0_sci_dis15a.wav");
      }
    }
  }
}
 
 
public admin_shutdown(HLData, HLCommand, HLUser, UserIndex)
{
	set_timer("kleen_shutdown", 1, 31);
	plugin_exec("admin_say", "Server is shutting down...Please rejoin Later.");
	return PLUGIN_HANDLED;
}
 
public kleen_shutdown(Timer, Repeat, HLUser, HLParam)
{
	new Text[MAX_TEXT_LENGTH];
 
	snprintf(Text, MAX_TEXT_LENGTH, "Shutting down server in..^n %d", Repeat-1);
	plugin_exec("admin_csay", Text);
 
	//6 seconds left to give time for the sound to be played completely
	//change 4 to a different number of seconds if desired becuse of a longer/shorter sound
	if( Repeat-1 == 6 )
	{
	   //play the shutting down sound
	   playShutdownSound();
	   //for now log a message that its happening for debugging purposes
	   log( "Clients Told to Play Shutdown Sound" );
	}
 
	if (Repeat-1 == 0)
	{
		kill_timer(Timer);
		plugin_exec("admin_say", "Have a Nice Day!");
        	exec("exit");
	}
 
 
	return PLUGIN_HANDLED;
}