#!/bin/bash

DB_HOST=${DB_HOST:-db}
DB_PORT=${DB_PORT:-5432}
DB_USER=${DB_USER:-root}
DB_PASS=${DB_PASS:-homerSeven}

if [ -f /usr/local/homer/etc/webapp_config.json ]; then

###Create user
#/homer-app -create-homer-user -database-root-user=$DB_USER -database-host=$DB_HOST -database-root-password=$DB_PASS
###Show user
#/homer-app -show-db-user -database-root-user=$DB_USER -database-host=$DB_HOST -database-root-password=$DB_PASS
###Create Role
#/homer-app -create-homer-role -database-root-user=$DB_USER -database-host=localhost -database-root-password=postgres -database-homer-data=homer_data -database-homer-config=homer_config

###Create DB
/homer-app -create-config-db -database-root-user=$DB_USER -database-host=$DB_HOST -database-port=$DB_PORT -database-root-password=$DB_PASS -database-homer-user=$DB_USER
/homer-app -create-data-db -database-root-user=$DB_USER -database-host=$DB_HOST -database-port=$DB_PORT -database-root-password=$DB_PASS -database-homer-user=$DB_USER

#Save it or edit the webapp_config.json manualy
#/homer-app -save-homer-db-config-settings -database-host=localhost -database-homer-config=homer_config -database-homer-user=homer_user -database-homer-password=homer_password
#/homer-app -save-homer-db-data-settings -database-host=localhost -database-homer-data=homer_data -database-homer-user=homer_user -database-homer-password=homer_password

#Create Table / Migration - connection data will be read from the webapp_config.json
/homer-app -create-table-db-config -webapp-config-path=/usr/local/homer/etc

#Populate DB
/homer-app -populate-table-db-config -webapp-config-path=/usr/local/homer/etc

   echo "Database provisioning completed!"

else

   echo "DB fail! Configuration file not found!"

fi

exec "$@"
