#!/bin/sh
# Dawnline first-run / upgrade housekeeping.

# Locations are off by default since 0.5.2 (lookups send addresses to a third
# party); a router that had them on by the old default keeps them on.
if [ -f /etc/config/dawnline ] && [ -z "$(uci -q get dawnline.settings.geo_enabled)" ]; then
	uci -q set dawnline.settings.geo_enabled=1
	uci -q commit dawnline
fi

[ -f /etc/config/dawnline ] || cat > /etc/config/dawnline <<'CFG'
config settings 'settings'
	option portal_enabled '0'
	option portal_port '8443'
	option default_device_limit '3'
	option hostnames '1'
CFG
chmod 600 /etc/config/dawnline
mkdir -p /etc/dawnline && chmod 700 /etc/dawnline
# Webhook alerts were removed.
uci -q delete dawnline.settings.webhook_url
uci -q delete dawnline.settings.alert
uci -q commit dawnline
rm -f /etc/dawnline/alerts.json
# Backups moved into Settings.
rm -f /www/luci-static/resources/view/dawnline/backups.js
# "Pause everyone" is gone: turning a server off does the same, so a
# router left paused keeps its servers off instead of letting everyone in.
if [ "$(uci -q get dawnline.settings.paused_all)" = 1 ]; then
	for s in $(uci -q show dawnline | sed -n 's/^dawnline\.\([^.=]*\)=server$/\1/p'); do uci -q set "dawnline.$s.enabled=0"; done
fi
uci -q delete dawnline.settings.paused_all
uci -q commit dawnline
# Portal assets moved out of the web root (served only with a valid code).
rm -rf /usr/share/dawnline/portal/www/assets

# "Find home devices" (0.3.0) is gone: its option and dnsmasq records too.
for s in $(uci -q show dawnline | sed -n 's/^dawnline\.\([^.]*\)\.bonjour=.*/\1/p'); do uci -q delete "dawnline.$s.bonjour"; done
uci -q commit dawnline
if ls /tmp/dnsmasq*.d/dawnline-bonjour.conf >/dev/null 2>&1; then
	rm -f /tmp/dnsmasq*.d/dawnline-bonjour.conf /var/run/dawnline/bonjour.json
	/etc/init.d/dnsmasq restart 2>/dev/null
fi

# LuCI caches: menu index, and module URLs (?v= follows the opkg status time).
rm -f /tmp/luci-indexcache* /tmp/luci-modulecache/* 2>/dev/null
touch /usr/lib/opkg/status 2>/dev/null

# The agent's heartbeat check went with the agent: procd restarts the backend.
grep -qs /usr/libexec/dawnline/heartbeat /etc/crontabs/root && {
	sed -i '\#/usr/libexec/dawnline/heartbeat#d' /etc/crontabs/root
	/etc/init.d/cron restart 2>/dev/null
}
# What the ucode backend was made of, left by a version that had it (the package manager removes what a
# package no longer ships; a tarball laid over it does not).
rm -rf /usr/share/ucode/dawnline /usr/share/rpcd/ucode/luci.dawnline /usr/libexec/dawnline/agent /usr/libexec/dawnline/cli \
	/usr/libexec/dawnline/updater.uc /usr/libexec/dawnline/heartbeat /usr/share/dawnline/portal/cgi.uc /usr/share/dawnline/portal/www
rm -f /etc/dawnline/backend-off /etc/dawnline/backend-watch /etc/dawnline/backend-portal /etc/dawnline/backend-sync /etc/dawnline/backend-apply /etc/dawnline/backend-shadow

# rpcd reads the access lists once (the backend's object is in them), and forgets the plugin that is gone.
/etc/init.d/rpcd reload
sleep 1
# A fresh install starts the service; an upgrade restarts it only where the
# admin had it on (a disabled Dawnline stays that way).
if [ "$PKG_UPGRADE" = 1 ]; then
	/etc/init.d/dawnline enabled && /etc/init.d/dawnline restart
else
	/etc/init.d/dawnline enable
	/etc/init.d/dawnline restart
fi
exit 0
