#!/bin/sh

FORCE=
LATER=

usage()
{
	echo "$0 - wrap command for idle-only execution"
	echo
	echo "Usage: $0 [-f] command args"
	echo
}

case $1 in
	-f) FORCE=yes; shift;;
	-h) usage; exit 0;;
esac

# off line?
# ACPI: "when battery's there and discharging"
[ -z "$FORCE" -a -d /proc/acpi/battery/ ] && \
	fgrep -q discharging /proc/acpi/battery/BAT*/state && LATER=yes
# APM: I only know for sure where/what's _line_ status
[ -z "$FORCE$LATER" -a -f /proc/apm ] && \
	[ "`cut -d' ' -f 4 < /proc/apm`" = "0x00" ] && LATER=yes

# run stuff nicely with "idle" I/O and CPU priorities
[ -n "$LATER" ] || ionice -c3 -t schedtool -D -n 20 -e "$@"
