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

START=10
STOP=90

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

	cd /etc/uci-defaults || return 0
	files="$(find . -type f | sort)"
	[ -z "$files" ] && return 0
	mkdir -p /tmp/.uci
	for file in $files; do
		( . "./$file" ) && rm -f "$file"
	done
	uci commit
}

boot() {
	[ -f /proc/mounts ] || /sbin/mount_root
	[ -f /proc/jffs2_bbc ] && echo "S" > /proc/jffs2_bbc

	mkdir -p /var/run
	mkdir -p /var/log
	mkdir -p /var/lock
	mkdir -p /var/state
	mkdir -p /var/tmp
	mkdir -p /tmp/.uci
	chmod 0700 /tmp/.uci
	touch /var/log/wtmp
	touch /var/log/lastlog
	mkdir -p /tmp/resolv.conf.d
	touch /tmp/resolv.conf.d/resolv.conf.auto
	ln -sf /tmp/resolv.conf.d/resolv.conf.auto /tmp/resolv.conf
	grep -q debugfs /proc/filesystems && /bin/mount -o noatime -t debugfs debugfs /sys/kernel/debug
	grep -q bpf /proc/filesystems && /bin/mount -o nosuid,nodev,noexec,noatime,mode=0700 -t bpf bpffs /sys/fs/bpf
	grep -q pstore /proc/filesystems && /bin/mount -o noatime -t pstore pstore /sys/fs/pstore

	# mount all entries in fstab
	/bin/mount -a &

	# /log directory might be created on preinit
	# symlink /storage to /log on TRB14X devices
	[ -d /storage -a ! -h /log ] && {
		rm -rf /log
		ln -sf /storage /log
	}

	# Wifi ---
	param=$(/sbin/mnf_info "--name")
	router_name=${param:0:6}
	if [ $router_name == "RUTX08" ] || [ $router_name == "RUTX09" ]; then
		rm /etc/modules.d/ath10k
	fi

	/bin/board_detect

	/sbin/kmodloader

	/bin/config_generate
	uci_apply_defaults

	[ -f "/etc/config/teltonika" ] && rm /etc/config/teltonika

	# temporary hack until configd exists
	/sbin/reload_config
	# leave finished boot script indication
	touch /var/run/boot-done
}
