#!/bin/sh /etc/rc.common
# Copyright (C) 2018 Ycarus (Yannick Chabanois) <ycarus@zugaina.org>
# Released under GPL 3. See LICENSE for the full terms.

START=11
STOP=98

boot() {
	. /lib/functions/system.sh

	cd /etc/uci-defaults || return 0
	source /etc/os-release

	OMRVERSION=$(echo $VERSION | sed 's/v//')
	files="$(ls /etc/uci-defaults/)"
	[ -n "$files" ] && {
		mkdir -p /usr/share/omr-update
		if [ "$(grep rom /etc/mtab)" = "" ]; then
			rm /usr/share/omr-update/*
			cp /etc/uci-defaults/* /usr/share/omr-update
		fi
		uci -q set openmptcprouter.settings=settings
		uci -q set openmptcprouter.settings.version=${OMRVERSION}
		uci commit
		return 0
	}
	if [ "$(uci -q get openmptcprouter.settings.version)" != "${OMRVERSION}" ]; then
		uci -q set openmptcprouter.settings=settings
		uci -q set openmptcprouter.settings.version=${OMRVERSION}
		uci commit
		cd /rom/etc/uci-defaults || cd /usr/share/omr-update || return 0
		files="$(ls)"
		[ -z "$files" ] && return 0
		for file in $files; do
			( . "./$(basename $file)" )
		done
	fi
	# temporary hack until configd exists
	/sbin/reload_config
}
