#!/bin/bash
resultdir=/tmp/result;
# check if result dir exists
if [ ! -d $resultdir ]; then 
    mkdir -p $resultdir; 
    if [ $? -ne 0 ]; then 
	echo error creating result dir - $?; 
	exit -1; 
    fi; 
else
    rm -rf $resultdir;
    mkdir $resultdir;
    if [ $? -ne 0 ]; then 
	echo error creating result dir - $?; 
	exit -1; 
    fi; 
fi;
#
#if [ ! -d result ]; then mkdir ./result; if [ $? -ne 0 ]; then echo error creating result dir - $?; exit -1; fi; fi;
#-===========================================================-
#                  forktest1                                 #
lve_forktest 2>&1 >/dev/null &
if [ $? -ne 0 ]; then 
    ERROR_FORKTEST1="$?"; 
    killall -9 -q lve_forktest 2>&1 >/dev/null; 
else
    count=0;
    while (true); do
    sleep 5;
    A=0.0;
        for i in `ps -C lve_forktest -o pcpu --no-headers`; do 
            A=`echo $A+$i| bc`; 
        done;
        B=`echo $A/1 | bc`;
        if [ $count -ne 3 ]; then 
	    if [ $B -lt 90 ]; then 
		ERROR_FORKTEST1="$B";
	    fi;
    	    count=$(($count+1)); 
    	else 
    	    break; 
    	fi;
    done;
fi;
killall -9 -q lve_forktest 2>&1 > $resultdir/forktest1.res
if [ $ERROR_FORKTEST1 -ne 0 ]; then 
    echo -e "Forktest1\t\t\tERROR $ERROR_FORKTEST1";
else 
    echo -e "Forktest1\t\t\tOK"
fi;
lvectl --flush all 2>&1 >/dev/null;
#-===========================================================-
#                  forktest2                                 #
lve_forktest2 2>&1 > $resultdir/forktest2.res
ERROR_FORKTEST2="$?"; 
if [ $ERROR_FORKTEST2 -ne 0 ]; then 
    killall -9 -q lve_forktest2 2>&1 >/dev/null; 
    echo -e "Forktest2\t\t\tERROR $ERROR_FORKTEST2";
else 
    echo -e "Forktest2\t\t\tOK"
fi;
lvectl --flush all 2>&1 >/dev/null;
#-===========================================================-
#                  forktest3                                 #
lve_forktest3 2>&1 > $resultdir/forktest3.res
    ERROR_FORKTEST3="$?"; 
if [ $ERROR_FORKTEST3 -ne 0 ]; then 
    killall -9 -q lve_forktest3 2>&1 >/dev/null; 
    echo -e "Forktest3\t\t\tERROR $ERROR_FORKTEST3";
else 
    echo -e "Forktest3\t\t\tOK"
fi;
lvectl --flush all 2>&1 >/dev/null;
#-===========================================================-
#                  forktest4                                 #
lve_forktest4 2>&1 > $resultdir/forktest4.res
    ERROR_FORKTEST4="$?"; 
if [ $ERROR_FORKTEST4 -ne 0 ]; then 
    killall -9 -q lve_forktest4 2>&1 >/dev/null; 
    echo -e "Forktest4\t\t\tERROR $ERROR_FORKTEST4";
else 
    echo -e "Forktest4\t\t\tOK"
fi;
lvectl --flush all 2>&1 >/dev/null;

#-===========================================================-
#                   API TEST 1                               #
lve_sanity
#-===========================================================-
#                   API TEST 2                               #
lve_sanity2


#-===========================================================-
#                  lvectl_test                               #
lvectl_test -c -t -r
echo Results may be found in $resultdir