#!/bin/sh

/bin/rm -rf /etc
/bin/ln -s /app/etc /etc

#insmod /lib/modules/2.6.9-n2100/kernel/drivers/i2c/chips/thecus_rtc.ko
modprobe sata_sil

echo "Buzzer 1" > /proc/thecus_io
echo "Freq 1 3" > /proc/thecus_io
echo "Freq 2 1" > /proc/thecus_io
echo "Busy 2" > /proc/thecus_io
echo "Init completed!"

/app/bin/setfan.sh 
#/app/bin/setDuty.sh &
/opt/bin/redboot_config get /dev/mtdblock4 serial_number > /img/serial_number

HW_STATUS=`/opt/bin/redboot_config get /dev/mtdblock4 hw_status`
if [ "$HW_STATUS" = "1" ]; then
  /app/cfg/rc.factory
  exit 0
fi

/app/bin/btn_dispatcher > /dev/null 2>&1 &

/sbin/hwclock -s
/bin/hostname `cat /etc/HOSTNAME |cut -d . -f1`
/sbin/syslogd

if [ -f "/app/conf.tar.gz" ]; then
  tar xzf /app/conf.tar.gz -C /
  rm -f /app/conf.tar.gz
fi


is_reset=`/opt/bin/redboot_config get /dev/mtdblock4 reset_default`
if [ "$is_reset" = "Reset_1" ]; then
  /app/bin/btn_reset
fi

##
/opt/bin/redboot_config get /dev/mtdblock4 mac_addr > /img/mac
MAC=`cat /img/mac`
if [ "$MAC" = "" ]; then
  MAC="00:50:8D:30:FF:B2"
  echo $MAC > /img/mac
fi
ifconfig eth0 hw ether $MAC
/app/cfg/cfg_nic0

/opt/bin/redboot_config get /dev/mtdblock4 mac_addr2 > /img/mac2
MAC2=`cat /img/mac2`
if [ "$MAC2" = "" ]; then
  MAC2="00:50:8D:30:FF:B3"
  echo $MAC2 > /img/mac2
fi
ifconfig eth1 hw ether $MAC2
/app/cfg/cfg_nic1

WLAN=`cat /proc/bus/pci/devices | grep 17fe2220 | wc -l`
if [ $WLAN = 1 ]; then
  modprobe ipt_MASQUERADE
  modprobe ip_nat_ftp
  echo 1 > /proc/sys/net/ipv4/ip_forward
  /opt/bin/iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
  modprobe IPN2220AP
  /app/bin/cfg_wlan0
fi

##
/app/bin/raid_start 
/app/bin/raid_m >/dev/null 2>&1 &
/bin/rm -rf /raid/usbhdd/*

##
/opt/apache/bin/apachectl startssl
/app/bin/ntpdate.sh ntp-1.mcs.anl.gov; /app/bin/ntpdate.sh clock1.unc.edu ; 
/app/bin/upnp_init start
/app/bin/ftpd_init boot
/app/bin/iTune.sh 
(/opt/samba/sbin/smbd; /opt/samba/sbin/nmbd) &
(/app/bin/udpr) &
#
if [ `ps | grep [p]ost_create | wc -l` -eq 0 ]; then
  echo "Busy 0" > /proc/thecus_io
fi

/usr/sbin/crond 
/usr/bin/crontab /app/cfg/crond.conf -u root

#/opt/bin/smartd -c /img/bin/smartd.conf -i 86400

/usr/sbin/hdparm -S 241 /dev/sda
/usr/sbin/hdparm -S 241 /dev/sdb

warning=`/bin/ls -l /app/ | grep "warning" | wc -l`
if [ $warning -ge 0 ];then
/bin/mv /app/warning /tmp/warning
fi

(sleep 5; modprobe usbcore; modprobe ehci-hcd; modprobe uhci-hcd; modprobe usb-storage)&

/app/bin/mkswap.sh &


