#!/bin/sh

. /etc/profile
. /etc/init.d/functions
. /etc/init.d/globals

SHOWINFO "start"

mount -t proc proc /proc

# init system
SHOWINFO "creating and mounting system directories..."
mount -t sysfs sys /sys
mount -t tmpfs tmp /tmp
mount -t tmpfs media /media
mount -t tmpfs mnt /mnt
for dir in epg movies music pictures streaming autofs plugins; do
	mkdir -p /mnt/${dir}
done
mount -t tmpfs srv /srv
mkdir -p /dev/pts
mount -t devpts devpts /dev/pts
mkdir -p /dev/shm/usb

# mount var-partition
# FIXME

# for nfsd
mkdir -p /var/lib/nfs
mount -t tmpfs nfs /var/lib/nfs

# for samba
mkdir -p /var/samba
mount -t tmpfs samba /var/samba

# for wget
mkdir -p /tmp/wget

dmesg -n 1

# set dummy time
date -s "2017-01-01 00:00"

# directory for wireless drivers
mkdir -p /var/run/wpa_supplicant

# automatic restore
if [ -e /var/backup_flash.tar.gz ]; then
	/bin/restore_flash.sh
fi

# update system
if [ -x /etc/init.d/sys_update.sh ]; then
	/etc/init.d/sys_update.sh
fi

service hostname start

# logging as much as possible
service syslogd start

# load modules / create nodes
load_module extra/hd51_1.ko
load_module extra/hd51_2.ko
load_module extra/hd51_3.ko
load_module extra/hd51_4.ko

# show bootlogo
showiframe.sh bootlogo.m2v

## mdev coldplug for node permissions
LOGINFO "mdev coldplug ..."
echo >/dev/mdev.seq
echo $(which mdev) > /proc/sys/kernel/hotplug
mdev -s

# mdev -s does not poke usb devices, so we need to do it here.
LOGINFO "scanning /sys/bus/usb/devices/ to help mdev with usb-coldplug"
for i in /sys/bus/usb/devices/*; do
	case "${i##*/}" in
		*-*:1.0)
			LOGINFO "usb device $i found"
			echo add >$i/uevent
		;;
	esac
done

service networking start
service ntpdate start

# say hi to everyone
echo "Booting... NI" > /dev/dbox/oled0

# starting services and daemons in order of the symlink names
LOGINFO "run initscripts start ..."
run_initscripts start

service coredump start

SHOWINFO "done"
