#!/bin/sh

files=`find $1 -name "*.bb"`
total=`find $1 -name "*.bb" | wc -l`
top=`pwd`/$1
skip_nums=$2
suffix=`date +%Y%m%d_%H-%M-%S`
result_report=$1/result_${suffix}_report.txt
bitbake_log=$1/bitbake_${suffix}_log.txt

function print_red(){
    echo $1 >>$bitbake_log
    echo -e '\033[0;31;1m'$1'\033[0m'
}

function print_green(){
    echo $1 >>$bitbake_log
    echo -e '\033[0;32;1m'$1'\033[0m'
}

function find_dep(){

 local path="${BUILDDIR}/tmp-glibc/deploy/deb/"
 
 local dep_num=`find $path -name "*$1*" | wc -l`
 print_green "========= [$1] deb file($dep_num) =======  "
    result=`find $path -name "*$1*"`
    for file in $result
    do
	    print_green ${file##*/}
	done
 print_green "========= output end ==================  "
 return $dep_num
} 

function run_command()
{
    local start_time=$(date +"%s")
    $1 $2 >/dev/null
    local ret=$?
    local end_time=$(date +"%s")
    local tdiff=$(($end_time-$start_time))
    local hours=$(($tdiff / 3600 ))
    local mins=$((($tdiff % 3600) / 60))
    local secs=$(($tdiff % 60))
    echo
    if [ $ret -eq 0 ] ; then
        print_green "#### make completed successfully "
		find_dep $2		
		printf "%-03s\t%-050s\t%-012s\t%-10s\t%s\n" $3 $2 "OK"  $? $4 >>$result_report
    else
        print_red "#### make failed to build [$2]"
		printf "%-03s\t%-050s\t%-012s\t%-10s\t%s\n" $3 $2 "NG"  0 $4  >>$result_report
    fi
    if [ $hours -gt 0 ] ; then
        printf "(%02g:%02g:%02g (hh:mm:ss))" $hours $mins $secs
    elif [ $mins -gt 0 ] ; then
        printf "(%02g:%02g (mm:ss))" $mins $secs
    elif [ $secs -gt 0 ] ; then
        printf "(%s seconds)" $secs
    fi
    echo -e " ####"
    return $ret
}

function report_result() {
   local ok_num=`cat $result_report|grep "OK"|wc -l`
   local ng_num=`cat $result_report|grep "NG"|wc -l`
   print_green "#############bitbake result:################"
   print_green "Total number of files:[$total]"
   print_green "OK number of files:[$ok_num]"
   print_green "NG number of files:[$ng_num]"
   print_green "#############bitbake end####################"
   print_green "Detailed report results are as follows:"
   print_green "############################################"
   print_red $result_report
   print_red $bitbake_log
   print_green "############################################"
}

function bitbake_run() {
    print_red "Total number of files:[$total]"
	print_red "Working directory:[$top]"
    local num=1
    for file in $files
    do
        filename=${file##*/}
		temp_filename=${filename%.*}
    	bbname=${temp_filename%%_*}
		if [ "x$1" = "x" ] || [ $num -ge $1 ]; then	
            print_green "[$num/$total] = $file"
    	    print_green "[bitbake $bbname]"
		    run_command  bitbake $bbname $num $file
		fi
  
		num=$(($num+1))
    done
    report_result
}

if [ "x$skip_nums" = "x" ];then
    if [ -f $result_report ];then 
        rm -f $result_report
    fi
    touch $result_report
else
    print_red "Skip $skip_nums files"
fi

Build_time=`date`
print_red "Build time:$Build_time"
bitbake_run $skip_nums
