#!/bin/bash

qtmake="/opt/Qt5.10.1/5.10.1/gcc_64/bin/qmake"

check_result()
{
	 
	if [ "$1" != 0 ];then
    	echo -e "\e[33m*************************************************\e[0m"
    	echo -e "\e[31m    Please modify build error first,Exit!\e[0m"
    	echo -e "\e[33m*************************************************\e[0m"
 	   	exit 1
    fi

}

cd src/include/proto
sh ./protomake.sh
cd ../../../

module=$1
echo -e "\e[33m module name: \e[1;33m $module \e[0m"

projectDir=`pwd`
echo -e "project folder \e[33m$projectDir\e[0m"

MAKEOPT=-j8

mkdir bin
outputDir=$projectDir"/bin"
echo -e "output folder \e[33m$outputDir\e[0m"

cd src
sourceDir=`pwd`
echo -e "source code folder \e[33m$sourceDir\e[0m"

moduleDir=`find . -type d -name $module`
if [ $moduleDir ];then
	moduleDir=$sourceDir${moduleDir#*.}
	echo -e "module code folder \e[33m$moduleDir\e[0m"
	cd $moduleDir
	moduleName=$module".pro"
	echo -e "\e[1;35;47m START BUILD\e[0m"
	$qtmake $moduleName
	make $MAKEOPT
	check_result $? 
	make clean
	cp $module $outputDir
	rm Makefile
	rm .qmake.stash
	rm $module
	cd $projectDir
	echo -e "\e[1;33;44m Build $module Success \e[0m"
	sleep 1

	echo -e "\e[35m Start deploy\e[0m"
	cp $outputDir"/"$module ./
	bash deploy.sh $module
	if [ "$?" == 1 ];then
		exit 1
	fi
	rm $module
	echo -e "\e[1;33;44m Deploy $module Success \e[0m"

else
	echo -e "\e[31m Can't find module code, exit.....\e[0m"
	exit
fi