#!/bin/sh

#set application's name
app_name="AudioPlayer"

/bin/mount -a

for initscript in /etc/init.d/S[0-9][0-9]*
do
    if [ -x $initscript ] ;
    then
        echo "[RCS]: $initscript"
        $initscript
    fi
done

cd /komod
#./loadgk7205v200 -i -sensor imx307 -osmem 48M -board demo -yuv0 0
./loadgk7205v200 -i -sensor imx307 -osmem 56M -board demo -yuv0 0
insmod ./gk7205v200_wdt.ko
cd /

#set env var
export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/opt/shell
export LD_LIBRARY_PATH=/lib:/usr/lib:/config/lib

alias ll='ls -l'

haveged

#date
#export TZ=CST-8
#ntpd -q -p cn.pool.ntp.org
#/opt/$app_name/date.sh

#enable lo & eth0
#ifconfig lo up
#ifconfig eth0 up

#set eth0's mac addr
#ifconfig eth0 hw ether xx:xx:xx:xx:xx:xx
#/opt/$app_name/mac.sh
/opt/$app_name/iproute.sh

#check application's audio dir
if [ ! -e "/opt/$app_name/audio" ]; then
    mkdir -p /opt/$app_name/audio
fi

#check tfcard & udisk connection status (priority: tfcard > udisk)
#app_parm_0=" "
#if [ -e "/dev/mmcblk0p1" ]; then
#    if [ ! -e "/tfcard" ]; then
#        mkdir -p /tfcard
#    fi
#    #mount /dev/mmcblk0p1 /tfcard
#    #mount -o iocharset=cp936 /dev/mmcblk0p1 /tfcard
#    mount -o iocharset=utf8 /dev/mmcblk0p1 /tfcard
#    if [ ! -e "/tfcard/audio" ]; then
#        mkdir -p /tfcard/audio
#    fi
#    mount /tfcard/audio /opt/$app_name/audio
#    app_parm_0="-tfcard"
#elif [ -e "/dev/mmcblk0" ]; then
#    if [ ! -e "/tfcard" ]; then
#        mkdir -p /tfcard
#    fi
#    #mount /dev/mmcblk0 /tfcard
#    #mount -o iocharset=cp936 /dev/mmcblk0 /tfcard
#    mount -o iocharset=utf8 /dev/mmcblk0 /tfcard
#    if [ ! -e "/tfcard/audio" ]; then
#        mkdir -p /tfcard/audio
#    fi
#    mount /tfcard/audio /opt/$app_name/audio
#    app_parm_0="-tfcard"
#elif [ -e "/dev/sda1" ]; then
#    if [ ! -e "/udisk" ]; then
#        mkdir -p /udisk
#    fi
#    #mount /dev/sda1 /udisk
#    #mount -o iocharset=cp936 /dev/sda1 /udisk
#    mount -o iocharset=utf8 /dev/sda1 /udisk
#    if [ ! -e "/udisk/audio" ]; then
#        mkdir -p /udisk/audio
#    fi
#    mount /udisk/audio /opt/$app_name/audio
#    app_parm_0="-udisk"
#fi

#run telnetd
#proc_id=`pgrep telnetd`
#if [ -z "$proc_id" ]; then
#    telnetd -p 601 -l /bin/login &
#fi

#run dropbear
proc_id=`pgrep dropbear`
if [ -z "$proc_id" ]; then
    /opt/dropbear/sbin/dropbear -p 602 -r /opt/dropbear/key/host_rsa &
fi

#run ftpd
#proc_id=`pgrep tcpsvd`
#if [ -z "$proc_id" ]; then
#    tcpsvd -vE 0 603 ftpd /opt/$app_name/audio -w &
#fi

#run webserver
#/opt/lighttpd/sbin/lighttpd -f /opt/lighttpd/config/lighttpd.conf &
rm -rf /opt/lighttpd/log/*
mount /opt/lighttpd/ /work/gk7205v200/opt/lighttpd/
/opt/lighttpd/sbin/lighttpd-angel -D -f /opt/lighttpd/config/lighttpd.conf &

#run reset application
proc_id=`pgrep ResetDetector`
if [ -z "$proc_id" ]; then
    /opt/ResetDetector/ResetDetector &
fi

#run application
truncate -s 0 /opt/Onvif/rtsp_connect.log
app_ver=$(\/opt\/$app_name\/$app_name -v 2>&1)
if [ ! ${#app_ver} -eq 3 ]; then
    echo "rollback libstdc..."
    cp -drf /opt/$app_name/libstdc++.so.6.0.23 /lib
fi
app_ver=$(\/opt\/$app_name\/$app_name -v 2>&1)
if [ ! ${#app_ver} -eq 3 ]; then
    echo "rollback app..."
    cp -drf /opt/$app_name/$app_name.bak /opt/$app_name/$app_name
fi
proc_id=`pgrep Daemon`
if [ -z "$proc_id" ]; then
    /opt/$app_name/Daemon $app_parm_0 &
fi

