#!/bin/bash
# made by Ryccardo <ricardovotonss@gmail.com> 28/1/2011
# thanks to blumiere for reporting a bug
#

if [ $# == 0 ]
then
	echo "Usage: airplay {start|stop|off|on}"
	echo "'off' and 'on' persist across reboots"
	echo "Default: on"
	exit 1
else
	if [ "$(whoami)" != 'root' ]; then
    echo "You must have root privileges to do this."
    exit 2
    else
		case $1 in
		off)
			echo "Stopping airtunesd..."
			launchctl unload -w /System/Library/LaunchDaemons/com.apple.airtunesd.plist
   			sleep .7s
   			exit 0
			;;
		on)
			echo "Starting airtunesd..."
			launchctl load -w /System/Library/LaunchDaemons/com.apple.airtunesd.plist
   			sleep .7s
			exit 0
		    ;;
		stop) 
   			echo "Stopping airtunesd..."
   			launchctl unload /System/Library/LaunchDaemons/com.apple.airtunesd.plist
   			sleep .7s
			exit 0
			;;
		start) 
   			echo "Starting airtunesd..."
   			launchctl load /System/Library/LaunchDaemons/com.apple.airtunesd.plist
   			sleep .7s
			exit 0
			;;
		*)
   			echo "Usage: airplay {start|stop|off|on}"
			echo "'off' and 'on' persist across reboots"
			echo "Default: on"
	   		;;
		esac
	fi
fi
