#!/bin/bash

# @file:    $RCSfile: start_fe.sh,v $
# @brief:   $系统应用启动脚本 
# @version: $Revision: 1.1 $
# @date:    $Date: 2018/12/26 07:19:31 $
# @author:  $Author: lizhongming $
# @state:   $State: Exp $
# @latest:  $Id: start_fe.sh,v 1.1 2018/12/26 07:19:31 lizhongming Exp $

QTDIR=/qt-4.8.4
export QTDIR

FEP_ENV=/FeProject
export FEP_ENV

PATH=$FEP_ENV/bin:$QTDIR/bin:$PATH
export PATH

LD_LIBRARY_PATH=$FEP_ENV/lib:$FEP_ENV/lib3rd:$QTDIR/lib:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH

check_log_file()
{
if [ -n "$1"  ] ; then
    FILE_SIZE=0
    FILE_SIZE=`du $1 | awk '{print $1}'`
    if [ $FILE_SIZE -ge 5120 ]
    then
        if [ -f "$1".3 ]; then 
            rm -f "$1".3
        fi
        if [ -f "$1".2 ]; then 
            mv "$1".2 "$1".3
       fi
        if [ -f "$1".1 ]; then 
            mv "$1".1 "$1".2
        fi
        mv "$1" "$1".1
    fi
fi
}

LOGFILE="$FEP_ENV/dat/log/start_fe.log"
check_log_file $LOGFILE


echo "" ; echo "" 
echo "******* `date "+%F %R:%S"` Start Processes *******"
echo "" >>"$LOGFILE"
echo "" >>"$LOGFILE"
echo "******* `date "+%F %R:%S"` Start Processes *******" >>"$LOGFILE"
#sysctl -w "kernel.core_pattern=/usr/local/sascore/%e.%p.%t.core" &> /dev/null

#执行程序路径^可执行程序名 启动参数 ^重启特有参数^首次启动特有参数^程序首次启动前的延时秒数^重启是否忽略^首次启动是否忽略

RUNTIME_CF_FILE="$FEP_ENV/etc/runtime.cf"

echo "Using config file name: $RUNTIME_CF_FILE"
echo "Using config file name: $RUNTIME_CF_FILE">>"$LOGFILE"
CURDIR=`pwd`
while read LINE ; do
 	if [ -n "$LINE"  ] ; then
		if [  ${LINE:0:1} != "#" ]; then
			BINPATH=`echo $LINE | awk -F '^' '{printf $1}'`
			BINPATH=`echo "$BINPATH" | grep -o "[^ ]\+\( \+[^ ]\+\)*"`
#			echo $BINPATH
			BIN=`echo $LINE | awk -F '^' '{printf $2}'`
			BIN=`echo "$BIN" | grep -o "[^ ]\+\( \+[^ ]\+\)*"`
#			echo $BIN
			SPECIAL=`echo $LINE | awk -F '^' '{printf $4}'`
			SPECIAL=`echo "$SPECIAL" | grep -o "[^ ]\+\( \+[^ ]\+\)*"`
#			echo $SPECIAL
			
			SLEEPSEC=`echo $LINE | awk -F '^' '{printf $5}'`
			SLEEPSEC=`echo "$SLEEPSEC" | grep -o "[^ ]\+\( \+[^ ]\+\)*"`
#			echo $SLEEPSEC
			STARTFLAG=`echo $LINE | awk -F '^' '{printf $7}'`
			STARTFLAG=`echo "$STARTFLAG" | grep -o "[^ ]\+\( \+[^ ]\+\)*"`
#			echo $STARTFLAG	

			RESTARTFLAG=`echo $LINE | awk -F '^' '{printf $6}'`
			RESTARTFLAG=`echo "$RESTARTFLAG" | grep -o "[^ ]\+\( \+[^ ]\+\)*"`
			echo $RESTARTFLAG		

			if [ "$RESTARTFLAG" = "IGNORE_RESTART" ]; then
#				echo "" ; 
				CMD_STR="    sleep `echo $SLEEPSEC` "
				echo "$CMD_STR"
				echo "$CMD_STR" >>"$LOGFILE"
				$CMD_STR
		        
				CMD_STR="    `echo $BINPATH``echo $BIN` `echo $SPECIAL` "
				echo "$CMD_STR"
				echo "$CMD_STR" >>"$LOGFILE"
				$CMD_STR &>/dev/null
			fi
		fi
	fi		
done < $RUNTIME_CF_FILE

cd $CURDIR

DT=`date "+%F %R:%S.%N"`
echo "*******  ${DT:0:23} Start Processes Succcessfully *******" 
echo "*******  ${DT:0:23} Start Processes Succcessfully *******"  >>"$LOGFILE"
