#!/bin/bash

cd "$(dirname "$0")"

#echo "SMARTKIT_PRINTING_SERVICE_DIR" $1
#echo "PRN_SVC_NAME" $2

export TF_VAR_base_dir="${1}/${2}"
export TF_VAR_container_name="$2"

#echo "TF_VAR_base_dir" $TF_VAR_base_dir
#echo "TF_VAR_container_name" $TF_VAR_container_name


echo Removing obsolete installations
./service.sh rm smeup-ps
./service.sh rm smeup-ps-test
./service.sh rm smeup-ps-stage
./service.sh rmi smeup/rdsv6

./service.sh rm $TF_VAR_container_name

echo Initializing modules
sudo -E terraform init

export TF_VAR_container_port=99999

echo Unistalling current infrastructure and container
sudo -E terraform destroy -auto-approve -var docker_host=unix:///var/run/docker.sock

unset TF_VAR_container_port
echo Building infrastructure, container, and starting service
sudo -E terraform apply -auto-approve -var docker_host=unix:///var/run/docker.sock

sudo chown -R $USER.$USER $TF_VAR_base_dir

echo Operation completed successfully    