#!/bin/sh

. /mod/etc/conf/mod.cfg

log=''

status=$(/mod/etc/init.d/rc.webcfg status)
[ "$status" = "running" -o "$status" = "inetd" ] || \
	log="$(/mod/etc/init.d/rc.webcfg start 2>&1)"

# Use HTTP_REFERER to determine target host (could be a name or an IP)
TARGET_HOST=$(env | sed -n -r 's/^HTTP_REFERER=[^:]+:\/\/([^/:]+).*/\1/p')

# Try HTTP_HOST
if [ -z "$TARGET_HOST" ]; then
TARGET_HOST=$(env | sed -n -r 's/^HTTP_HOST=(.*)/\1/p')
fi

# Use fritz.box as fallback
if [ -z "$TARGET_HOST" ]; then
TARGET_HOST=fritz.box
fi

location="http://$TARGET_HOST:$MOD_HTTPD_PORT/"

cat << EOF
Content-type: text/html; charset=iso-8859-1

<html>
<head>
<meta http-equiv="refresh" content="0;url=$location">
</head>
<body style="margin: 0px: pading: 0px; color: #b0b0b0; font-size: 10px;">
$([ -n "$log" ] && echo "$log<br>")
Redirecting...<a style="color: #b0b0b0;" href="$location">Freetz</a>
</body>
</html>
EOF
