#!/bin/dash
# Entropy generator
# Description: Entropy generator for /dev/random - run once at boot. Either run this or haveged, but not both.
#

PIDFILE=/var/run/haveged.pid
PARAMS= # default is good enough
RUNTIME=60 # in seconds

start() {
	haveged $PARAMS
}

stop() {
	if [ -f $PIDFILE ]; then
		read p < $PIDFILE
		kill $p	
	fi
}

is_running() {
	if [ -f $PIDFILE ]; then
		read p < $PIDFILE
		kill -0 $p
	else
		return 1
	fi
	
}

# $1-how long to wait
autostop() {
	sleep $1
	stop
}

case $1 in
	start|restart)
		stop
		sleep 1
		start
		autostop $RUNTIME &
		;;
		
	stop)
		stop
		;;
		
	status)
		is_running && echo "haveged is running" || echo "haveged is stopped"
		;;
esac

