#!/bin/bash
#Dominic White
#http://singe.za.net/
#Jan 17th 2009
#GPL'ed

hostfile=/etc/hosts
blocklist="$HOME"/.worktime

function modfile {
	echo -e "127.0.0.1\t$1\t#Worktime" >> $hostfile
}

function status {
	grep "#Worktime" $hostfile >/dev/null
}

function block {
	if status; then
		echo "It's already work time."
		exit 1
	else
		for domain in $(<$blocklist); do
			modfile "$domain"
		done
		echo "Get to work!"
		exit 0
	fi
}

function unblock {
	if status; then
		sed -i '/#Worktime/ d' $hostfile
		echo "Work's done, let's play."
		exit 0
	else
		echo "You're already playing."
		exit 1
	fi
}

function usage {
	echo -e "Invalid argument!\nThe options are \"on\" or \"off\" e.g.:\n $0 on\t#to enable work time\n $0 off\t#to enable play time"
	exit 1
}

for cmdopt in $*; do
	if [ "$cmdopt" == "on" ]; then	
		block
	elif [ "$cmdopt" == "off" ]; then
		unblock
	else
		usage
	fi
done

if [ $# -ne 1 ]; then
	usage
fi

echo "Why are we here? Invalid exit detected."
exit 1

