#!/bin/sh
#
# Starts lighttpd.
#

migrate_config() {
    # make sure httpd.ssl section exists
    uci set httpd.ssl='section' > /dev/null 2>&1
    uci commit
}

start() {
    migrate_config
    
	echo -n "Starting lighttpd: "
	
	# protection against cleared .passwd
    HTTPD_PWD=`/sbin/uci -q get httpd.webserver.password_set`
    if [[ ! -f /barix/local/etc/.passwd ]] && [[ "$HTTPD_PWD" == "true" ]]; then
        /sbin/uci set httpd.webserver.password_set=
        /sbin/uci commit
    fi
    
    if [ "$(cat /barix/info/SYSTEM-TYPE)" = "DEVELOPMENT" ]; then
        /sbin/uci set httpd.webserver.password_set=false
        /sbin/uci commit
    fi
    

	# generate configuration file
	/lib/config/gen_config.sh httpd

	# start up lighttpd
	start-stop-daemon -S -q -p /var/run/lighttpd.pid --exec /usr/sbin/lighttpd -- -f /etc/lighttpd/lighttpd.conf
	echo "OK"
}
stop() {
	echo -n "Stopping lighttpd: "
	start-stop-daemon -K -q -p /var/run/lighttpd.pid
	echo "OK"
}
restart() {
	stop
	start
}

case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart|reload)
	restart
	;;
  *)
	echo "Usage: $0 {start|stop|restart}"
	exit 1
esac

exit $?

