#! /usr/bin/perl -w 

use strict;

	my $first_arg = shift (@ARGV);
	
	if ($first_arg =~ /^button\/lid$/) {
		system ('/usr/X11R6/bin/xset -display :0 dpms force off');
	}
	
	if ($first_arg =~ /^button\/sleep$/) {
		shift (@ARGV);
		shift (@ARGV);
		if (check_ignore ('/etc/acpi/flags/suspend')) {
			clear_ignore ('/etc/acpi/flags/suspend');
		} else {
			set_ignore ('/etc/acpi/flags/suspend');
			system ('/usr/X11R6/bin/xset -display :0 dpms force off');
#			system ('rmmod tg3');
			system ('rmmod bcm5700');
			system ('rmmod usb_storage usbhid uhci_hcd ehci_hcd');
			system ('/sbin/service mysqld stop');
			system ('echo 4 > /proc/acpi/sleep && hwclock --hctosys');
			system ('echo "Resuming proceses....');
#			system ('modprobe tg3');
			system ('modprobe bcm5700');
			system ('modprobe usb_storage');
			system ('modprobe ehci_hcd');
			system ('modprobe uhci_hcd');
			system ('modprobe usbhid');
			system ('/sbin/service mysqld start');
	                }
	}

	if ($first_arg =~ /^ac_adapter$/) {
		handle_power_change ();
	}	

	if ($first_arg=~ /^processor$/){
		#handle_power_change();
		ignore_event();
	}

############################################################
sub handle_power_change {

	open (ACSTATUS, '</proc/acpi/ac_adapter/AC/state');
	my $state = <ACSTATUS>;
	close (ACSTATUS);

	chomp $state;

	if ($state =~ /^state:\s+on-line$/) {
		# This is already handled by cpufreqd
		#if ac is on-line, make sure the processor is at top speed.
		#system ('echo performance > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor');
		system ('echo 1400000 > /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq');
		system ('speedfreq -p dynamic');

		#turn power managment on the hard drive off.
		system ('/sbin/hdparm -B 255 /dev/hda');
	
		# Set The HD spindown time using hdparm (2 Hours)
		system ('/sbin/hdparm -S 244 /dev/hda > /dev/null 2>&1');
		
		# we stop laptop_mode
		system('/sbin/laptop_mode stop');

	} else {
		#if ac is off-line, make sure the processor is at some slower speed (just set it here).
		#system ('echo userspace > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor');
		#system ('echo 600000 > /sys/devices/system/cpu/cpu0/cpufreq/scaling_setspeed');
		
		# Since we're now using speedfreqd, then we use this call instead
		system ('speedfreq -p powersave');
		
		# turn power managment on the hard drive to the most agressive level.
		system ('/sbin/hdparm -B 1 /dev/hda > /dev/null 2>&1');
		
		# Set the HD spindown time using hdparm (30 seconds)
		system ('/sbin/hdparm -S 6 /dev/hda > /dev/null 2>&1');
		
		#we call the laptop_mode bash script
		system ('/sbin/laptop_mode start');
	}
		
}

############################################################
sub ignore_event {
	# Not sure why but ACPID calls the processor directive 2 times
	# Hence the need for this little rule
	# system ('echo 600000 > /sys/devices/system/cpu/cpu0/cpufreq/scaling_setspeed');
}

############################################################
sub check_ignore {
	my $name = shift;
	
	if (-e $name) {
		return 1;
	} else {
		return 0;
	}
	
}
############################################################
sub set_ignore {
	my $name = shift;
	
	system ("touch $name");
}
############################################################
sub clear_ignore {
	my $name = shift;
	
	system ("rm $name");
}
############################################################

