
#改变工作目录到当前脚本所在路径
if [[ "$0" =~ / ]]; then cd "${0%/*}"; fi
export CWD=`pwd`

filescount=`./mql files? log.level=error "$@" | wc -l`
if [[ "${filescount}" == "0" || "${filescount}" == "1" ]]; then
    ./mql "$@"
    exit $?
fi

echo "" > changed.log

parseargs() {
    while [[ "$*" != "" ]]; do
        if [[ "$1" == "init=no" ]]; then
            noinit=1
        fi
        shift
    done
}
parseargs "$@"

if [[ -e /opt/matrix/odbserver/odbserver ]]; then
    last_odbserver=`/opt/matrix/odbserver/odbserver -version`
fi
keyspace=`./mql keyspace? log.level=error "$@"`
if [[ `echo "${keyspace}" | wc -w` == "1" ]]; then
    logfile="${keyspace}.test.log"
else
    logfile="test.log"
fi

wait_odbserver_change() {
    while true; do
        if [[ -e ~/odbserver/odbserver ]]; then
            cp -f ~/odbserver/odbserver ~/odbserver/odbserver.new
            odbserver=`~/odbserver/odbserver.new -version 2>&1`
            if [[ "${odbserver:0:12}" == "omdb version" ]]; then
                if [[ "$odbserver" != "$last_odbserver" ]]; then
                    # wait for sync files complete
                    sleep 10
                    return 0
                fi
            fi
        fi
        sleep 2
    done
}

n=0
while [[ true ]]; do
    ret=0
    n=`expr $n + 1`
    if [[ -e ~/odbserver/odbserver ]]; then
        cp -f ~/odbserver/odbserver ~/odbserver/odbserver.new
        odbserver=`~/odbserver/odbserver.new -version`
        if [[ "${odbserver:0:12}" == "omdb version" ]]; then
            if [[ "$odbserver" != "$last_odbserver" ]]; then
                echo "odbserver changed" >> changed.log
                echo "$last_odbserver" >> changed.log
                echo "$odbserver" >> changed.log
                ./sync.sh
                cp -pf ~/odbserver/odbserver /opt/matrix/odbserver/odbserver
                if [[ "${odbserver:42:14}" == "${last_odbserver:42:14}" ]]; then
                    verchanged=0
                else
                    verchanged=1
                fi
                last_odbserver="$odbserver"

                if [[ "$noinit" != "1" && "$verchanged" == "1" ]]; then
                    ./initkeyspace.sh $keyspace
                    ret=$?
                    if [[ "$ret" != "0" ]]; then
                        n=0
                    else
                        n=1
                    fi
                else
                    n=1
                fi
            fi
        fi
    fi 
    if [[ "$n" != "0" ]]; then
        cd "$CWD"
        PROCFLAG="test$n"
        endflag="$odbserver end of test $n return"
        ttt=`date +'%Y%m%d%H%M%S'`
        check_odbserver() {
            sleep 10
            while true; do
                mqlproc=`ps -ef | grep -v grep | grep mql | grep "proc.flag=${PROCFLAG}"`
                if [[ "$mqlproc" == "" ]]; then
                    return 0
                fi
                if [[ -e ~/odbserver/odbserver ]]; then
                    cp -f ~/odbserver/odbserver ~/odbserver/odbserver.new
                    odbserver=`~/odbserver/odbserver.new -version`
                    if [[ "${odbserver:0:12}" == "omdb version" ]]; then
                        if [[ "$odbserver" != "$last_odbserver" ]]; then
                            ps -ef | grep -v grep | grep mql | grep "proc.flag=${PROCFLAG}" | awk '{print $2}' | xargs kill -9
                            return 0
                        fi
                    fi
                fi
                sleep 10
            done
        }
        run() {
            ./mqltest.sh "proc.flag=${PROCFLAG}" "$@"
            ret=$?
            echo "$endflag $ret" >> $logfile
            if [[ "$ret" == "0" ]]; then
                sleep 5
            fi
            return $ret
        }
        check_odbserver &
        run "$@"
        ret=$?
    fi
    
    if [[ "$ret" != "0" && "$ret" != "99" ]]; then
        echo "wait for odbserver upgrad"
        wait_odbserver_change
    fi
done
