#!/bin/sh

runlevel=S
prevlevel=N
umask 022
export runlevel prevlevel

# source defaults
. /etc/default/rcS

. /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 run /var/run
mount -t tmpfs media /media
mount -t tmpfs srv /srv
mount -t tmpfs mnt /mnt
for dir in autofs epg logos logos/events movies music pictures plugins; do
	mkdir -p /mnt/${dir}
done
mkdir -p /dev/pts
mount -t devpts devpts /dev/pts
mkdir -p /dev/shm/usb
mount -t usbfs none /proc/bus/usb

# 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 /var/run/wget

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

# set level of messages printed to console
dmesg -n 1

# set dummy date
test -e /etc/date-dummy && date -s "$(cat /etc/date-dummy)"

# create nodes
# <name>		<type>	<mode>	<uid>	<gid>	<major>	<minor>	<start>	<inc>	<count>
makedevs /dev << EONODES
watchdog		c	644	0	0	10	130
cnxt			d	755	0	0
cnxt/cnxt_gen_drv	c	644	0	0	102	0
notifyq			c	644	0	0	102	1
user_kal		c	644	0	0	102	2
display			c	644	0	0	238	0
cs_control		c	644	0	0	249	0
input			d	755	0	0
input/nevis_ir		c	644	0	0	240	0
input/event0_uinput	c	644	0	0	10	223
input/mouse		c	644	0	0	13	32	0	1	4
input/event2		c	644	0	0	13	66
input/event3		c	644	0	0	13	67
EONODES

ln -sf nevis_ir /dev/input/event0
ln -sf nevis_ir /dev/input/input0

# set hostname
service hostname start

# logging as much as possible
service syslogd start

service sysctl start

LOGINFO "init frontpanel ..."
load_module cs_frontpanel.ko

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

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

# mdev coldplug for node permissions
service mdev start

# initialize hardware
LOGINFO "init hardware ..."
dt -t"INIT HARDWARE"
load_module cnxt_kal.ko
load_module cnxt_base.ko init=1

if [ -e /var/etc/.scart_osd_fix ]; then
	load_module cnxt_fb.ko cnxtfb_standalone=1 cnxtfb_width=720 cnxtfb_height=576	#PAL
elif [ -e /var/etc/.scart_osd_fix_wide ]; then
	load_module cnxt_fb.ko cnxtfb_standalone=1 cnxtfb_width=1024 cnxtfb_height=576	#PAL widescreen
else
	load_module cnxt_fb.ko cnxtfb_standalone=1 cnxtfb_width=1280 cnxtfb_height=720	#HDTV
fi

load_module cnxt_lnx.ko
load_module cnxt_alsa.ko

if [ -e /var/etc/.hddpower ]; then
	load_module cs_control.ko hdd_power=1
else
	load_module cs_control.ko
fi

load_module cnxt_i2c.ko
load_module cnxt_sata_drv.ko

LOGINFO "init drivers ..."
dt -t"INIT DRIVERS"

load_module 8712u.ko
load_module 8192cu.ko
load_module rt2870sta.ko

# fire up network/wlan
service network start
service chrony start

# load rest of modules
load_module cifs.ko
load_module fuse.ko
load_module usbserial.ko
load_module ftdi_sio.ko
load_module tun.ko
load_module pl2303.ko
load_module stv6110.ko verbose=0
load_module stv090x.ko verbose=0
load_module tda10023.ko
load_module avl2108.ko
load_module max2112.ko
load_module cs_frontend_prop.ko
load_module dvb-core.ko
load_module cs_frontend.ko

# say hi to everyone
dt -ls01
dt -ls02
dt -c
dt -t"BOOT NI-IMAGE"

# trap CTRL-C only in this shell so we can interrupt subprocesses
trap ":" INT QUIT TSTP

# call all parts in order
exec /etc/init.d/rc S

SHOWINFO "done"
