#!/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="$(ls)"
	[ -z "$files" ] && return 0
	mkdir -p /tmp/.uci
	for file in $files; do
		( . "./$(basename $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/lock
	chmod 1777 /var/lock
	mkdir -p /var/log
	mkdir -p /var/run
	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
	[ "$FAILSAFE" = "true" ] && touch /tmp/.failsafe

	EDS5K=`cat /proc/device-tree/lantronix/hwcfg 2>/dev/null | grep 'EDS50' | awk -F'sku=' '{print $2}' | awk -F',' '{print $1}' | awk '{print substr($0,0,7)}' | tr -d '\n'`

	if [[ "$EDS5K" != "EDS5032" ]]; then
	    if [[ "$EDS5K" == "EDS5016" ]]; then
	        instance=17
	    else
		instance=9
	    fi

	    while [ $instance -le 32 ]; do
	        j=`expr $instance - 1`
		rm /etc/config/percepxion_line$instance
		rm /etc/config/serial$j
		rm /usr/sbin/cfscripts/serial$j.*
		rm /usr/sbin/cfscripts/modbus_rtu_tcp_$instance.*
		rm /usr/sbin/cfscripts/percepxion_line$instance.*
		instance=`expr $instance + 1`
	    done
	fi

	/sbin/kmodloader

	[ ! -f /etc/config/wireless ] && {
		# compat for bcm47xx and mvebu
		sleep 1
	}

	[ -f /etc/serial_initialization_done ] && {
		rm /etc/serial_initialization_done
	}

        [ -f /sbin/rootfs_adj_file ] && {
                rm /sbin/rootfs_adj_file
        }

	/bin/config_generate
	uci_apply_defaults
	sync
	
	# temporary hack until configd exists
	/sbin/reload_config
}
