# !/bin/bash
#
# app-env - sets up the application environment variables base on mode
#           where mode is one of 'development', 'test', 'production'
#
# Variables with values that are independent of mode
export APP_DB_NAME="{{ smoacks_app_name }}"
export APP_DB_CPORT={{ smoacks_db_container_port }}
export OPENAPI_SPEC='/app/src/spec/{{ dest_spec }}'
export REACT_APP_API_PATH="{{ smoacks_api_path }}"
export APP_LOGGING_CONFIG='/app/src/server_logging.yaml'
export APPSERVER_CPORT={{ smoacks_server_container_port }}
export APP_SHUTDOWN_KEY='{{ smoacks_app_shutdown_key }}'

if [ "${{ smoacks_app_name_c }}_MODE" == "development" ]; then
  echo "Setting up development environment variables"
  export APP_DB_CNAME='{{ smoacks_dev_db_service }}'
  export APP_DB_ACCOUNT={{ smoacks_dev_db_account }}
  export APP_DB_PASSWORD={{ smoacks_dev_db_password }}
  export APP_DB_SPORT={{ smoacks_dev_db_service_port }}
  export APPSERVER_SPORT={{ smoacks_dev_server_service_port }}
  export SECRET_KEY={{ smoacks_dev_secret_key }}
  export REACT_APP_BASE_URL="http://{{ smoacks_local_kube_server }}:${APPSERVER_SPORT}"
elif [ "${{ smoacks_app_name_c }}_MODE" == "test" ]; then
  export APP_DB_CNAME='{{ smoacks_test_db_service }}'
  export APP_DB_ACCOUNT={{ smoacks_test_db_account }}
  export APP_DB_PASSWORD={{ smoacks_test_db_password }}
  export APP_DB_SPORT={{ smoacks_test_db_service_port }}
  export APPSERVER_SPORT={{ smoacks_test_server_service_port }}
  export TEST_HOST={{ smoacks_test_host }}
  export SECRET_KEY={{ smoacks_test_secret_key }}
elif [ "${{ smoacks_app_name_c }}_MODE" == "production" ]; then
  export APP_DB_CNAME='{{ smoacks_db_service }}'
  export APP_DB_ACCOUNT={{ smoacks_db_account }}
  export APP_DB_PASSWORD={{ smoacks_db_password }}
  export APP_DB_SPORT={{ smoacks_db_service_port }}
  export APPSERVER_SPORT={{ smoacks_server_service_port }}
  export SECRET_KEY={{ smoacks_secret_key }}
else
  echo "Invalid value for {{ smoacks_app_name_c }}_MODE: ${{ smoacks_app_name_c }}_MODE"
fi
alias dsrestart="curl -H 'Content-type: application/json' -X POST -d '{\"key\":\"{{ smoacks_app_shutdown_key }}\"}' ${REACT_APP_BASE_URL}${REACT_APP_API_PATH}/shutdown"
alias dlog="kubectl logs \$(kubectl get pod -l \"app={{ smoacks_app_name_k }}-dev-server\" -o jsonpath='{.items[0].metadata.name}')"
alias tlog="kubectl logs \$(kubectl get pod -l \"app={{ smoacks_app_name_k }}-test-server\" -o jsonpath='{.items[0].metadata.name}')"