#! /bin/sh
### BEGIN INIT INFO
# Provides:          runtime-pm-pcie
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Modify/restore runtime pm settings
# Description:       Debian init script to restore runtime pm
#                    placed in /etc/init.d.
### END INIT INFO

# Author: Foo Bar <foobar@baz.org>
#
# Please remove the "Author" lines above and replace them
# with your own name if you copy and modify this script.

# Do NOT "set -e"

# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="Set or restore runtime PCI-E power management settings"
NAME=runtime-pm-pcie
SCRIPTNAME=/etc/init.d/$NAME

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
# and status_of_proc is working.
. /lib/lsb/init-functions


case "$1" in
  start)
	[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
	for i in /sys/bus/pci/devices/*; do echo "auto" > "$i"/power/control ; done
	echo "PCI-E power management set to auto."
	case "$?" in
		0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
		2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
	esac
	;;
  stop)
	[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
	for i in /sys/bus/pci/devices/*; do echo "on" > "$i"/power/control ; done
	echo "PCI-E power management settings reset to default."
	case "$?" in
		0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
		2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
	esac
	;;
  status)
  	for i in /sys/bus/pci/devices/*; do echo Checking power management status for bus "$i"...; cat "$i"/power/control ; done
	;;
  *)
	echo "Usage: $SCRIPTNAME {start|stop|status|}" >&2
	exit 3
	;;
esac

exit 0
