#!/bin/sh
#
# /etc/init.d/sysinit
#
# (C)opyright Teco a.s.
#
# 2017/06/23 Hosek Martin <hosek@tecomat.cz>
#

echo "--- Booting from NAND ---"

echo -n "Loading modules ... "

#    modprobe rtc-omap
    modprobe option
    modprobe ppp_generic

echo "done"

echo -n "Updating bootlog ... "

    bootlog -u &

echo "done"

cpufreq max

. /etc/.mbinfo

case "$Module_Name" in
    "TXN 054 44")
		    IDSTRING="ID-31"
		    ;;
    "TXN 054 45")
		    IDSTRING="ID-32"
		    ;;
    *)
		    IDSTRING="UNKNOWN"
		    ;;
esac

echo -n "Setting hostname ... "

    echo "$IDSTRING" > /etc/hostname

    hostname -F /etc/hostname

echo "done"

echo -n "Updating hosts file ... "

    HOST_NAMES=`cat /etc/hosts`

    APPEND_LINE="127.0.0.1        $IDSTRING.localhost.localdomain $IDSTRING"

    HOST_NAMES=`echo "$HOST_NAMES" | sed '/^127.0.0.1*/a APPEND_LINE' | sed s/'APPEND_LINE'/"$APPEND_LINE"/`

    echo "$HOST_NAMES" > /etc/hosts

echo "done"

echo -n "Setting network ... "

    modprobe rtl8192cu

    ifdown --force lo >/dev/null 2>&1
    ifdown --force eth0 >/dev/null 2>&1

    ifconfig eth0 hw ether `/sbin/genmac`

    ifconfig eth0 up
    ifconfig eth0 down
    ifconfig eth0 up

    ifup lo
    ifup eth0

echo "done"

echo -n "Syncing system time ... "

    hwclock --utc --hctosys >/dev/null 2>&1

    ntpdate-debian >/dev/null 2>&1 &

echo "done"

BACKUP_FILE="/mnt/root/.backup.tgz"

if [ -f "$BACKUP_FILE" ]; then
    echo -n "Restoring backup file ... "
	tar xfz "$BACKUP_FILE" -C / >/dev/null 2>&1
	rm -f "$BACKUP_FILE" >/dev/null 2>&1
    echo "done"
fi

. /etc/default/display

backlight $DISPLAY_BRIGHTNESS

. /etc/default/touchscreen

echo -n "Starting Xserver ... "

    modprobe ws-ft5x06 detect_treshold=$DETECT_TRESHOLD peak_detect_treshold=$PEAK_DETECT_TRESHOLD >/dev/null 2>&1

    if [ $? == 1 ]; then
	modprobe ar1020_i2c >/dev/null 2>&1

	if [ $? == 1 ]; then
	    modprobe ti_tscadc
#	    modprobe ti_am335x_tsc
	    ln -s /usr/local/etc/pointercal.tscadc /etc/pointercal
	else
	    ln -s /usr/local/etc/pointercal.ar1020 /etc/pointercal
	fi
    else
	ln -s /usr/local/etc/pointercal.ft5x06 /etc/pointercal
    fi

    xloop >/dev/null 2>&1 &

echo "done"

echo -n "Starting HTTP server ... "

    if [ -d /srv/www ]; then
	rm -rf /srv/www
	ln -s /usr/local/srv/www /srv/www
    fi

    httpd -p 8080 -u root -h /srv/www

echo "done"

echo -n "Starting FTP server ... "

    ftpd -D

echo "done"

echo -n "Starting TELNET server ... "

    telnetd

echo "done"

#echo -n "Starting SSH server ... "

#    if [ -d /etc/ssh ]; then
#	rm -rf /etc/ssh
#	ln -s /mnt/root/etc/ssh /etc/ssh
#    fi

#    if [ ! -f /etc/ssh/ssh_host_dsa_key ]; then
#	echo
#	echo -n " Generating DSA key ... "
#	ssh-keygen -q -t dsa -f /etc/ssh/ssh_host_dsa_key -N ""
#	echo -n "done"
#	newkey="yes"
#    fi

#    if [ ! -f /etc/ssh/ssh_host_rsa_key ]; then
#	echo
#	echo -n " Generating RSA key ... "
#	ssh-keygen -q -t rsa -f /etc/ssh/ssh_host_rsa_key -N ""
#	echo -n "done"
#	newkey="yes"
#    fi

#    if [ ! -f /etc/ssh/ssh_host_ecdsa_key ]; then
#	echo
#	echo -n " Generating ECDSA key ... "
#	ssh-keygen -q -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N ""
#	echo -n "done"
#	newkey="yes"
#    fi

#    if [ $newkey ]; then
#	echo
#    fi

#    if [ ! -d /var/run/sshd ]; then
#	mkdir /var/run/sshd
#    fi

#    /usr/sbin/sshd

#echo "done"

#echo -n "Starting STUNNEL services ... "

#    if [ -d /etc/stunnel ]; then
#	rm -rf /etc/stunnel
#	ln -s /mnt/root/etc/stunnel /etc/stunnel
#    fi

#    stunnel

#echo "done"
