TestEnv() {

	Check=$(which $1 2> /dev/null) 
	if [[ -z ${Check} ]]
		then
			echo "$1 not found ... "	
			exit 1
	fi


}

Res() {
	if [[ $1 -eq 0 ]]
		then
			echo "$(tput setaf 2)OK$(tput sgr0)"
		else
			echo "$(tput setaf 1)FAILED$(tput sgr0)"
	fi

}


help () {
	echo "
$0 [image/repo/esl/all]

	* $(tput setaf 3)image 	$(tput sgr0): Generate docker images
	* $(tput setaf 3)image-gen	$(tput sgr0): Generate docker image for esl compilator
	* $(tput setaf 3)image-repo	$(tput sgr0): Generate docker image  for repository sync 
	* $(tput setaf 4)repo		$(tput sgr0): start docker compose and generate repositories
	* $(tput setaf 4)gen		$(tput sgr0): start docker compose and generarte esl
	* $(tput setaf 1)all		$(tput sgr0): Do everything
	* $(tput setaf 2)run-gen	$(tput sgr0): Connect and run bash on instance gen
	* $(tput setaf 2)run-repo 	$(tput sgr0): Connect and run bash on instance repo
	* $(tput setaf 2)stop 		$(tput sgr0): stop all docker image
	* $(tput setaf 5)ps 		$(tput sgr0): print all docker image

"
exit 

}



DeleteFile() {

	echo -n "Delete Dockerfile ... : "
	[[ -f ${Wai}/Dockerfile ]] && rm Dockerfile || true
	Res $?


}

DockerConnect() {

	Inst=$(docker ps|grep "esl-$1" | awk '{print $NF}')
	[[ -z "${Inst}" ]] && docker run -ti -v $(realpath $(dirname $0))/data:/data -u esl  esl\-${1}:1 bash || docker exec -ti ${Inst} bash 
echo DEBUG $Inst $1

}
DockerPs() {

	docker ps|grep $(dirname .)_esl\-$1 | awk '{print $NF}'

}



DockerStop() {
	for DockerRun in $(docker ps|grep esl\- | awk '{print $NF}')
		do
			echo -n "Stop $DockerRun ... :"
			docker stop $DockerRun &> /dev/null
			Res $?
		done
}

DeleteImg () {
	DeleteFile
	echo -n "Delte old images $1 ... : " 
	for var in $(docker image ls| grep  "esl-$1"   | awk '{print $3}'); do yes | docker image rm $var --force; done &> /dev/null
	Res $?
}

DockerGen() {
	DeleteImg $1
	echo -n "generate Dockerfile $1 ... : "
	source define.conf
	while read line
		do
			if [[ ! -z $(echo $line | grep '\$') ]]
				then
					eval echo ${line} | sed  "s#\[#\[\'#g" | sed "s#\]#\'\]#g"
				else
					echo $line
			fi
        done < Template/Dockerfile.$1 > Dockerfile
	Res $?

	echo -n "Generate $1  Docker image ... : "
	docker build --no-cache . -t esl-$1:${GenVers} 2>&1 | tee /tmp/BuildDocker-${1}.log &> /dev/null 
	Res $?

	echo "
------ Log Compil -----------
/tmp/BuildDocker-${1}.log
-----------------------------
"

}


DockerComposeRun () {
	echo "
------ Log Compil -----------
/tmp/GetRepo-${1}.log
-----------------------------
"



	[[ -f ${Wai}/docker-compose.yml ]] && rm -f ${Wai}/docker-compose.yml
	[[ "$2" == "Print" ]] && Print="-d" || Print=""
	cp ${Wai}/Template/docker-compose.yml.$1 ${Wai}/docker-compose.yml
	echo "Log is : log/${1} - use tail -f ./log/$1"
	docker run -ti -v $(realpath $(dirname $0))/data:/data -u esl  esl-repo:1 /usr/local/bin/sync.sh
	#yes | docker-compose  up  ${Print} --remove-orphans 1>&1 | tee /tmp/RunDocker-${1}.log &> /dev/null
	##yes | docker-compose  up ${Print} --remove-orphans
	Res $?
	echo "
--------- Log Esl -----------
/tmp/RunDocker-${1}.log
-----------------------------
"
}



GenAll () {
	DockerGen gen
	DockerGen repo
}

