# This is an upstart script for bitmessage for when using daemon mode
# Bitmessage forks more than twice before daemonizing, so a workaround is
# necessary

description "bitmessage"
author      "Peter Surda"

start on (local-filesystems and net-device-up)
stop on runlevel [!2345]

setuid bitmessage
setgid bitmessage

chdir /home/bitmessage
env HOME="/home/bitmessage"

pre-start script
	/usr/src/PyBitmessage/src/bitmessagemain.py
end script

script
	while [ ! -f $HOME/.config/PyBitmessage/singleton.lock ]; do
		sleep 1
	done
	while [ -f $HOME/.config/PyBitmessage/singleton.lock ]; do
		sleep 1
	done
end script

post-stop script
	if [ -f $HOME/.config/PyBitmessage/singleton.lock ]; then
		pid=`lsof -F p $HOME/.config/PyBitmessage/singleton.lock|cut -b2-`
		if [ -n "$pid" ]; then
			kill $pid
		fi
	fi
end script