#!/bin/sh

export PATH=/bin:/sbin:/usr/bin:/usr/sbin
mount none /proc -t proc

export HOSTNAME="meraki-node"
echo $HOSTNAME > /proc/sys/kernel/hostname

/sbin/watchdog -t 1 /dev/watchdog
/usr/bin/meraki-watchdog &

/bin/mkdir -p /var/run
/bin/mkdir -p /var/log
/bin/touch /var/log/wtmp
/bin/touch /var/log/lastlog

if [ `cat /MERAKI_ARCH` == "i386" ]; then
    /bin/mount -t jffs2 /dev/mtdblock0 /storage || ( mtd erase /dev/mtd0 && /bin/mount -t jffs2 /dev/mtdblock0 /storage )
else
    /bin/mount -t jffs2 /dev/mtdblock`grep /storage /proc/mtd | head -c 4 | tail -c 1` /storage
fi
/bin/mount none /tmp -t tmpfs -o nosuid,nodev,mode=1777,size=50%
/bin/mkdir -p /dev/pts
/bin/mount none /dev/pts -t devpts
/bin/grep sysfs /proc/filesystems >&- && /bin/mount -t sysfs none /sys

syslogd -C 16 ${syslog_ip:+-L -R $syslog_ip}
klogd


/sbin/insmod diag
[ -f /proc/sys/diag ] && echo 0x01 > /proc/sys/diag
sleep 1

/sbin/vconfig set_name_type VLAN_PLUS_VID_NO_PAD

if [ -x /storage/early-init.sh ]; then
    . /storage/early-init.sh
fi

#first install modules that are depended on
for mod in ath_hal.ko wlan.ko ath_rate_sample.ko proclikefs.ko switch-core.ko; do
    [ -f /lib/modules/${mod} ] && /sbin/insmod /lib/modules/${mod}
done
#now the rest of them
for mod in /lib/modules/*.ko; do
    /sbin/insmod $mod
done

if [ `cat /MERAKI_ARCH` == "mipsel" ]; then
    ifconfig eth0 up
    sleep 1
    /sbin/vconfig set_name_type VLAN_PLUS_VID_NO_PAD
    echo "1" > /proc/switch/eth0/reset
    echo "1" > /proc/switch/eth0/enable_vlan
    echo "4 5" > /proc/switch/eth0/vlan/0/ports
    /sbin/vconfig add eth0 0
    echo "0 1 2 3 5*" > /proc/switch/eth0/vlan/1/ports
    /sbin/vconfig add eth0 1
fi
