#!/bin/sh /etc/rc.common
# Copyright (C) 2006-2014 OpenWrt.org

#start before lighttpd
START=49

#. /lib/functions/network.sh

boot() {
	start
	return
}

start() {
	config_load "webui"
	local HTTPS RemoteMgt RemoteMgtHTTPS RemoteMgtPort
	
	config_get_bool HTTPS administration HTTPS 1

	# use this way will get empty at boot stage
	# network_get_ipaddr ip lan
	config_load "network"
	config_get ip lan ipaddr
	
	echo "ip : $ip" > /dev/console
	
	#cp /etc/webui/vhosts.template /etc/lighttpd/conf.d/http.conf
	sed 's/%HOST_IP4/'$ip'/g' /etc/webui/vhosts.template  > /etc/lighttpd/conf.d/http.conf
	
	if [ "$HTTPS" -eq 1 ]; then
	#	cp /etc/webui/vhosts.template2 /etc/lighttpd/conf.d/https.conf
		sed 's/%HOST_IP4/'$ip'/g' /etc/webui/vhosts.template2 > /etc/lighttpd/conf.d/https.conf
	else
		rm /etc/lighttpd/conf.d/https.conf
	fi
	
	[ -s /etc/lighttpd/ssl/server.pem ] && return 1
	
	mkdir -p /etc/lighttpd/ssl/
		
	openssl req -new -x509 -keyout /etc/lighttpd/ssl/server.pem -out /etc/lighttpd/ssl/server.pem -days 7300 -nodes -subj "/C=TW/ST=Taiwan/L=Taipei/O=D-Link Corporation/OU=D-Link WRPD/CN=General Root CA/emailAddress=webmaster@localhost"

	
	return
}

stop() {
	return
}
