#!/bin/bash
#
# read required env variables
BASEDIR="$PWD"
source $BASEDIR/.env/ipdp-shared.properties
source $BASEDIR/.env/ipdp-app.properties
#
# docker login
echo "Attempting to login to $artifactUrl"
docker login $artifactUrl -u $artifactUser -p $artifactPassword
#
# docker pull app image
echo "Attempting to pull $artifactUrl/ipdp-app:$appVersion"
docker pull $artifactUrl/ipdp-app:$appVersion
#
# docker stop ipdp app container
echo "Attempting to stop $entityId-ipdp-app container"
docker stop $entityId-ipdp-app
#
# docker rm ipdp app container
echo "Attempting to remove $entityId-ipdp-app container"
docker rm $entityId-ipdp-app
#
# Builder docker run command based on settings in env files
echo "Building docker run command"
dockerRunCommand="docker run --name $entityId-ipdp-app
  --restart=unlipdp-stopped
  --network $entityId-ipdp-net
  -e SPRING_DATASOURCE_URL=jdbc:postgresql://$entityId-ipdp-db:5432/ipdpdb
  -e SPRING_DATASOURCE_DRIVER_CLASS_NAME=org.postgresql.Driver
  -e SPRING_DATASOURCE_USERNAME=postgres
  -e SPRING_DATASOURCE_PASSWORD=$dbPassword
  -e SPRING_PROFILES_ACTIVE=prod
  -e APPLICATION_ADMIN_PASSWORD=$adminPassword
  -e APPLICATION_ADMIN_RESET=$resetAdminPassword
  -e USPS_CONFIGURATION_HOST=$uspsHost
  -e USPS_CONFIGURATION_PORT=$uspsPort
  -e USPS_CONFIGURATION_APIKEY=$uspsApiKey
  -e USPS_CONFIGURATION_REMOTEAPIKEY=$uspsRemoteApiKey
  -e JAVA_OPTS=$java_opts
  -e DOCUMENTSTORE_ELASTIC_HOST=$documentStoreHost
  -e DOCUMENTSTORE_ELASTIC_PORT=$documentStorePort
  -e DOCUMENTSTORE_ELASTIC_APIKEY=$documentStoreApiKey
  -e DOCUMENTSTORE_ELASTIC_FINGERPRINT=$documentStoreFingerprint
  -e ELASTIC_PASSWORD=$documentStorePassword"
#
if [ ! -z "$virtualHost" ]; then
    dockerRunCommand="$dockerRunCommand -e VIRTUAL_HOST=$virtualHost"
fi
#
if [ ! -z "$virtualPort" ]; then
    dockerRunCommand="$dockerRunCommand -e VIRTUAL_PORT=$virtualPort"
fi
#
if [ ! -z "$letsencryptHost" ]; then
    dockerRunCommand="$dockerRunCommand -e LETSENCRYPT_HOST=$letsencryptHost"
fi
#
if [ ! -z "$letsencryptEmail" ]; then
    dockerRunCommand="$dockerRunCommand -e LETSENCRYPT_EMAIL=$letsencryptEmail"
fi
#
if [ ! -z "$hostPort" ]; then
    dockerRunCommand="$dockerRunCommand -p $hostPort"
fi
#
#
if [ ! -z "$duoEnabled" ]; then
    echo "Adding Duo Configuration"
    dockerRunCommand="$dockerRunCommand
        -e DUO_ENABLED=$duoEnabled
        -e DUO_CLIENT_ID=$duoClientId
        -e DUO_CLIENT_SECRET=$duoClientSecret
        -e DUO_API_HOSTNAME=$duoApiHostname
        -e DUO_FAIL_OPEN=$duoFailOpen
        -e DUO_ESS_HOST=$duoIpdpHost
        -e DUO_ESS_PORT=$duoIpdpPort
        -e DUO_ESS_CONTEXT=$duoIpdpContext
        -e DUO_MINUTES_UNTIL_EXPIRATION=$duoMinutesUntilExpiration"
fi
#
#
if [ ! -z "$miniOrangeEnabled" ]; then
    echo "Adding MiniOrange Configuration"
    dockerRunCommand="$dockerRunCommand
        -e MINI_ORANGE_ENABLED=$miniOrangeEnabled
        -e MINI_ORANGE_CLIENT_ID=$miniOrangeClientId
        -e MINI_ORANGE_CLIENT_SECRET=$miniOrangeClientSecret
        -e MINI_ORANGE_API_HOSTNAME=$miniOrangeApiHostname
        -e MINI_ORANGE_FAIL_OPEN=$miniOrangeFailOpen
        -e MINI_ORANGE_APP_HOST=$miniOrangeAppHost
        -e MINI_ORANGE_MINUTES_UNTIL_EXPIRATION=$miniOrangeMinutesUntilExpiration"
fi
#
#
if [ ! -z "$ads_config1_enabled" ]; then
    echo "Add Window Active Director Configuration 1"
    dockerRunCommand="$dockerRunCommand
    -e SSDT_ADS_CONFIG1_ENABLED=$ads_config1_enabled
    -e SSDT_ADS_CONFIG1_DOMAIN=$ads_config1_domain
    -e SSDT_ADS_CONFIG1_LDAP_URL=$ads_config1_ldap_url
    -e SSDT_ADS_CONFIG1_ROOT_DN=$ads_config1_root_dn
    -e SSDT_ADS_CONFIG1_SEARCH_FILTER=$ads_config1_search_filter"
fi
#
#
if [ ! -z "$ads_config2_enabled" ]; then
    echo "Add Window Active Director Configuration 2"
    dockerRunCommand="$dockerRunCommand
    -e SSDT_ADS_CONFIG2_ENABLED=$ads_config2_enabled
    -e SSDT_ADS_CONFIG2_DOMAIN=$ads_config2_domain
    -e SSDT_ADS_CONFIG2_LDAP_URL=$ads_config2_ldap_url
    -e SSDT_ADS_CONFIG2_ROOT_DN=$ads_config2_root_dn
    -e SSDT_ADS_CONFIG2_SEARCH_FILTER=$ads_config2_search_filter"
fi
#
#
if [ ! -z "$ads_config3_enabled" ]; then
    echo "Add Window Active Director Configuration 3"
    dockerRunCommand="$dockerRunCommand
    -e SSDT_ADS_CONFIG3_ENABLED=$ads_config3_enabled
    -e SSDT_ADS_CONFIG3_DOMAIN=$ads_config3_domain
    -e SSDT_ADS_CONFIG3_LDAP_URL=$ads_config3_ldap_url
    -e SSDT_ADS_CONFIG3_ROOT_DN=$ads_config3_root_dn
    -e SSDT_ADS_CONFIG3_SEARCH_FILTER=$ads_config3_search_filter"
fi
#
#
support_instance=${support_instance:-false}
if [ "$support_instance" = "true" ]; then
    echo "Setting application instance type to Support"
    dockerRunCommand="$dockerRunCommand -e APPLICATION_INSTANCE_TYPE=Support"
fi
#
#
dockerRunCommand="$dockerRunCommand -d $artifactUrl/ipdp-app:$appVersion"
#
# Execute constructed docker run command to create $entityId-ipdp-app container connected to db and docker network
echo "Executing docker run command"
$dockerRunCommand
