#!/bin/sh

PATH=/sbin:/bin:/usr/sbin:/usr/bin

export PATH=$PATH:/usr/local/bin

runlevel=S
prevlevel=N
umask 022
export PATH runlevel prevlevel

export QTDIR=/usr/local/qt-4.8.6
export T_ROOT=/usr/local/tslib-1.4
export QT_QWS_FONTDIR=$QTDIR/fonts
export LD_LIBRARY_PATH=$QTDIR/lib:$T_ROOT/lib

#
#	Trap CTRL-C &c only in this shell so we can interrupt subprocesses.
#

mount -a
mkdir -p /dev/pts
mount -t devpts devpts /dev/pts
#mount -n -t usbfs none /proc/bus/usb
echo /sbin/mdev > /proc/sys/kernel/hotplug
mdev -s
mkdir -p /var/lock


echo linux_name=`uname -r` > /tmp/linux_name
source /tmp/linux_name


/home/ftp/rtc_set.sh &

#depmod

ln -s /home/ip /bin/ip

#################### ethernet ####################
# loopback
ifconfig lo 127.0.0.1
sleep 2

usbWifiName=$(lsusb| grep 2357:011f| awk '{print $6}')
if [[ "${usbWifiName}"x = "2357:011f"x ]]; then
	modprobe wlan_T2U_V3
else
	usbWifiName=$(lsusb|grep 2357:0111| awk '{print $6}')
	if [ "${usbWifiName}"x = "2357:0111"x ]; then
		modprobe wlan_727N_V5
	else
		usbWifiName=$(lsusb|grep 2357:0109| awk '{print $6}')
		if [[ "${usbWifiName}"x = "2357:0109"x ]]; then
			modprobe wlan_821N_V6
		else
			usbWifiName=$(lsusb|grep 2357:0107| awk '{print $6}')
			if [[ "${usbWifiName}"x = "2357:0107"x ]]; then
				modprobe wlan_821N_V6
			else
				usbWifiName=$(lsusb| grep 148f:7601| awk '{print $6}')
				if [[ "${usbWifiName}"x = "148f:7601"x ]]; then
					modprobe rtutil7601Uap
					modprobe mt7601Uap
					modprobe rtnet7601Uap
					sleep 15
				fi
			fi
		fi
	fi
fi

# for fec net
#ifconfig eth0 192.168.1.6	# EtherCAT
ifconfig eth0 down

sleep 1
ifconfig eth1 192.168.1.6
ifconfig eth2 192.168.2.6
ifconfig eth3 192.168.100.1

sleep 1
wfName=$(ip link show |grep wlan2|awk '{print $2}'|cut -d: -f 1)
if [ "${wfName}"x = "wlan2"x ]; then
	hostapd -B -d /etc/hostapd.conf
	ifconfig wlan2 192.168.9.1 netmask 255.255.255.0 broadcast 192.168.9.255 up
	udhcpd /etc/udhcpd.conf
fi

wfName1=$(ip link show |grep ra0|awk '{print $2}'|cut -d: -f 1)
if [ "${wfName1}"x = "ra0"x ]; then
	ifconfig ra0 192.168.9.1 netmask 255.255.255.0 broadcast 192.168.9.255 up
	sleep 2
	udhcpd /etc/udhcpd1.conf
fi

#################### ssh #############################
echo "ssh start" > /dev/null
rsaFile="/usr/local/etc/ssh_host_rsa_key"
if [ ! -f $rsaFile ]; then
 cd /usr/local/etc && ssh-keygen -t rsa -f ssh_host_rsa_key -N ""
else
 echo 'rsaFile existing' > /dev/null 
fi


dsaFile="/usr/local/etc/ssh_host_dsa_key"
if [ ! -f $dsaFile ]; then
 cd /usr/local/etc && ssh-keygen -t dsa -f ssh_host_dsa_key -N ""
else
 echo 'dsaFile existing' > /dev/null
fi


ecdsaFile="/usr/local/etc/ssh_host_ecdsa_key"
if [ ! -f $ecdsaFile ]; then
 cd /usr/local/etc && ssh-keygen -t ecdsa -f ssh_host_ecdsa_key -N ""
else
 echo 'ecdsaFile existing' > /dev/null
fi


edFile="/usr/local/etc/ssh_host_ed25519_key"
if [ ! -f $edFile ]; then
 cd /usr/local/etc && ssh-keygen -t dsa -f ssh_host_ed25519_key -N ""
else
 echo 'rsaFile existing' > /dev/null
fi

cd /usr/local/etc && chmod 600 ssh_host_ed25519_key

chown -R root:root /var/empty
chmod 711 /var/empty

echo "ssh end" > /dev/null


################################################
echo "vmcore try"
vmcFile="/proc/vmcore"
if [ ! -f $vmcFile ]; then
	echo "normal Start"
else
	destFile="`date +%y%m%d%k%M`_vmcore.txt"
	vmcore-dmesg /proc/vmcore > /home/ftp/$destFile
	touch /home/ftp/kPanic
	sync
	reboot
fi

################################################

/usr/local/bin/sshd &
sleep 1
/etc/rc.d/init.d/netd start
/etc/rc.d/init.d/httpd start

/bin/hostname -F /etc/sysconfig/HOSTNAME

echo 101 > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio101/direction
echo "1" > /sys/class/gpio/gpio101/value

sleep 1

if [ -f "/home/ftp/dbStart.sh" ]; then
	mv /home/ftp/dbStart.sh /etc/init.d/
	dos2unix /etc/init.d/dbStart.sh
	chmod +x /etc/init.d/dbStart.sh
	sync
fi

cd /etc/init.d
./dbStart.sh &

ifconfig eth0 up


sleep 20
cd /home/ftp/affinity
./Start.sh 
