#!/bin/bash
ttn_stack()
{
	systemctl stop ttnstack
	docker-compose -f /usr/local/chirpstack/docker-compose.yml down --rmi all 2> /dev/null
	docker-compose -f /usr/lib/ttn-stack/docker-compose.yml down --rmi all 2> /dev/null

	key_32=$(openssl rand -hex 32)
	key_64=$(openssl rand -hex 64)
	hostname=$(hostname)
    touch /tmp/instal_status.txt
	apt update &> /dev/null;
	
    if [[ ! -f /usr/bin/docker ]]; then
		echo "-----------------------------------------------------------------------"
	        echo "Start installing the ttn-stack, which first will install the dependcies on docker, docker-commpose"
                apt install docker.io -y &> /dev/null;
                if [[ -f /usr/bin/docker ]]; then
                        echo "docker install successfully"
			echo "-----------------------------------------------------------------------"
                else
                        echo "docker install fail, please try again"  && exit 1
                fi
        fi

    if [[ ! -f /usr/bin/docker-compose ]]; then
		apt install docker-compose -y &> /dev/null;
		if [[ -f /usr/bin/docker-compose ]]; then
			echo "docker-compose install successfully"
			echo "-----------------------------------------------------------------------"
		else
			echo "docker-compose install fail, please try again" && exit 1
		fi
	fi

    [[ ! -d /usr/lib/ttn-stack/config/stack/  ]] && mkdir /usr/lib/ttn-stack/config/stack/ -p
    [[ ! -d /usr/lib/ttn-stack/docker-compose.yml  ]] && curl  http://repo.dragino.com/release/tool/ttnstack/docker-compose.yml -o /usr/lib/ttn-stack/docker-compose.yml &> /dev/null

	
		
    if [[ ! -f /usr/lib/ttn-stack/config/stack/ttn-lw-stack-docker.yml  ]]; then
		curl http://repo.dragino.com/release/tool/ttnstack/ttn-lw-stack-docker.yml -o /usr/lib/ttn-stack/config/stack/ttn-lw-stack-docker.yml &> /dev/null
		sed 's/10\.130\.2\.18/'$hostname'/g' /usr/lib/ttn-stack/config/stack/ttn-lw-stack-docker.yml -i;
		sed 's/BLOCKKEY/'$key_32'/g' /usr/lib/ttn-stack/config/stack/ttn-lw-stack-docker.yml -i;
		sed 's/HASHKEY/'$key_64'/g' /usr/lib/ttn-stack/config/stack/ttn-lw-stack-docker.yml -i;
	fi
		
    if [[ -f /usr/lib/ttn-stack/docker-compose.yml ]]; then
        cd /usr/lib/ttn-stack/;
        docker-compose pull;
        if [[ $? = 0 ]]; then
            echo "Pull done, Starting init ttn-stack"
        fi
	fi

	export DOCKER_CLIENT_TIMEOUT=120
	export COMPOSE_HTTP_TIMEOUT=120

	sleep 2;

	if [[ `cat /tmp/instal_status.txt | grep init -c` != 1 ]]; then  
		docker-compose run --rm stack is-db migrate
		if [[ $? != 0 ]]; then
			echo "init fail, please try again" && exit 1 
		else
			echo "init done" >> /tmp/instal_status.txt
		fi
	fi
	sleep 2;

	if [[ `cat /tmp/instal_status.txt | grep "create-admin-user" -c` != 1 ]]; then  
		docker-compose run --rm stack is-db create-admin-user \
		--id admin \
		--email ${hostname}@dragino.com \
		--password dragino
		if [[ $? != 0 ]]; then
			echo "create-admin-user fail, please try again" && exit 1 
		else
			echo "create-admin-user done" >> /tmp/instal_status.txt
		fi
	fi
	sleep 5;

	# if [[ `cat /tmp/instal_status | grep "create-oauth-client" -c` != 1 ]] then  
		# docker-compose run --rm stack is-db create-oauth-client \
		# --id cli \
		# --name "Command Line Interface" \
		# --owner admin \
		# --no-secret \
		# --redirect-uri "local-callback" \
		# --redirect-uri "code"
		
		# if [[ $? != 0 ]]; then
			# echo "create-oauth-client fail, please try again" && exit 1 
		# else
			# echo "create-oauth-client done" >> /tmp/instal_status.txt
		# fi
	# fi
	sleep 2;

	if [[ `cat /tmp/instal_status.txt | grep "create-oauth-client" -c` != 1 ]]; then  
		docker-compose run --rm stack is-db create-oauth-client \
		--id console \
		--name "Console" \
		--owner admin \
		--secret "console" \
		--redirect-uri "http://${hostname}:8080/console/oauth/callback" \
		--redirect-uri "/console/oauth/callback" \
		--logout-redirect-uri "http://${hostname}:8080/console" \
		--logout-redirect-uri "/console"
		if [[ $? != 0 ]]; then
			echo "create-oauth-client fail, please try again" && exit 1 
		else
			echo "create-oauth-client done" >> /tmp/instal_status.txt
		fi
	fi
	sleep 2;
	
	docker-compose -f /usr/lib/ttn-stack/docker-compose.yml up -d
}


case "$1" in
		ttn-stack)
		ttn_stack
                ;;
        *)
esac

exit