#!/bin/sh

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

insmod /lib/modules/2.6.9-n2100/kernel/drivers/i2c/i2c-core.ko
insmod /lib/modules/2.6.9-n2100/kernel/drivers/i2c/busses/i2c-iop3xx.ko
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 

/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

##
/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

echo 1 > /proc/sys/net/ipv4/ip_forward
/opt/bin/iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
/opt/bin/iptables -t nat -A POSTROUTING -o ppp0 -j MASQUERADE

is_reset=`/opt/bin/redboot_config get /dev/mtdblock4 reset_default`
if [ "$is_reset" = "Reset_1" ]; then
  ifconfig eth0 192.168.1.100 netmask 255.255.255.0 broadcast +
  /app/bin/raid_start FACTORY
  /app/bin/raid_m >/dev/null 2>&1 &
  /app/bin/mkswap.sh start
  echo "<?header('Location: /factory/index.html');?>" > /img/htdocs/index.html
  ln -fs /img/factory /img/htdocs/factory
  /opt/apache/bin/apachectl startssl
  echo "Buzzer 0" > /proc/thecus_io
  sleep 1
  echo "Buzzer 1" > /proc/thecus_io
  echo "Busy 0" > /proc/thecus_io
  exit
fi

/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

MINIPCI=`cat /proc/bus/pci/devices | grep 17fe2220 | wc -l`
if [ $MINIPCI = 1 ]; then
  modprobe IPN2220AP
  /app/bin/cfg_wlan0 $MINIPCI
  MINIPCI=2
else
  MINIPCI=`cat /proc/bus/pci/devices | grep 1814030 | wc -l`
  if [ $MINIPCI = 1 ]; then
    modprobe rt61ap
    /app/bin/cfg_wlan0 $MINIPCI
    MINIPCI=2
  fi
fi

##
echo "clear queue" > /proc/thecus_eventc
/app/bin/raid_start 
/app/bin/raid_m2 >/dev/null 2>&1 &
/bin/rm -rf /raid/usbhdd/*

##
/opt/apache/bin/apachectl startssl
/app/bin/ntp_cfg; 
/app/bin/rc/rc.ftpd boot
/app/bin/rc/rc.udhcpd_eth1 boot
/app/bin/rc/rc.cupsd
/app/bin/mp3_ripper &
(/app/bin/udpr) &
#
/usr/sbin/crond -L /dev/null
/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

# for temperature shutdown ui admin first login use
warning=`/bin/ls -l /app/ | grep "warning" | wc -l`
if [ $warning -ge 0 ];then
/bin/mv /app/warning /tmp/warning
fi

(sleep 1; modprobe usbcore; modprobe ehci-hcd; modprobe uhci-hcd; modprobe usb-storage; modprobe usblp; /app/bin/cfg_wlan0 $MINIPCI; /opt/samba/sbin/smbd; /opt/samba/sbin/nmbd; /app/bin/rc/rc.upnpd start; /app/bin/rc/rc.daapd boot; /app/bin/rc/rc.atalk boot; touch /tmp/boot_ok1 )&

/app/bin/mkswap.sh start &
/img/bin/logevent/event 102 &
/opt/bin/ifplugd -fwI -u0 -d1 -p -i eth0
/opt/bin/ifplugd -fwI -u0 -d1 -p -i eth1

/app/bin/module_boot.sh

/img/bin/boot_ok &
