#!/bin/bash
set -e

function initVolumes() {
  podman volume inspect dev_contiamo-metadb-datavolume 2>&1 >/dev/null || podman volume create dev_contiamo-metadb-datavolume
  podman volume inspect dev_contiamo-minio-data-volume 2>&1 >/dev/null || podman volume create dev_contiamo-minio-data-volume
}

function initPod() {
  podman pod create --name=dev --share net \
    -p 127.0.0.1:5429:5431 \
    -p 127.0.0.1:9001:9000 \
    -p 127.0.0.1:5433:5433 \
    -p 127.0.0.1:8443:8443 \
    -p 127.0.0.1:5001:5001 \
    -p 127.0.0.1:9898:8888 \
    -p 127.0.0.1:6379:6379 \
    -p 127.0.0.1:3001:3001 \
    -p 5778:5778 \
    -p 14268:14268 \
    -p 16686:16686 \
    -p 9411:9411 \
    -p 127.0.0.1:9191:9191 \
    -p 6831:6831/udp \
    -p 6832:6832/udp \
    -p 5775:5775/udp
}

function startRedis() {
  podman run --name=redis --hostname=redis -d --pod=dev redis:alpine
}

function startTracing() {
  podman run --name=tracing --hostname=tracing -d --pod=dev \
    -e COLLECTOR_ZIPKIN_HTTP_PORT=9411 \
    jaegertracing/all-in-one:1.14
}

function startMetadb() {
  podman run --name=metadb --hostname=metadb -d --pod=dev \
    -e POSTGRES_USER=user \
    -e POSTGRES_PASSWORD_FILE=/db/password \
    -e PGPORT=5433 \
    -v ./config/db-password:/db/password \
    -v ./config/dumps:/docker-entrypoint-initdb.d/dumps \
    -v ./config/datastore/db-bootstrap.sh:/docker-entrypoint-initdb.d/datastore-db-bootstrap.sh \
    -v ./config/hub/db-bootstrap.sh:/docker-entrypoint-initdb.d/hub-db-bootstrap.sh \
    -v ./config/auth/db-bootstrap.sh:/docker-entrypoint-initdb.d/auth-db-bootstrap.sh \
    -v ./config/pantheon/db-bootstrap.sh:/docker-entrypoint-initdb.d/pantheon-db-bootstrap.sh \
    -v dev_contiamo-metadb-datavolume:/var/lib/postgresql/data \
    postgres:latest -p 5433 -c log_statement=all
}

function startBlobstorage() {
  podman run --name=blobstorage --hostname=blobstorage -d --pod=dev \
    --env-file config/blobstore/config.env \
    -v dev_contiamo-minio-data-volume:/data \
    -v ./config/blobstore:/config/blobstore \
    minio/minio:latest server /data

}

function startIdp() {
  podman run --name=idp --hostname=idp -d --pod=dev \
    -e JAEGER_DISABLED=false \
    -e JAEGER_AGENT_HOST=localhost \
    -v ./config/auth:/config/auth \
    -v ./config/db-password:/db/password \
		--entrypoint=/bin/idp \
    eu.gcr.io/dev-and-test-env/idp:dev \
      --log-level=DEBUG \
      --secure-cookie=false \
      --bootstrap-file=/config/auth/bootstrap.json \
      --domain= \
      --external-link-base-url=http://localhost:9898/auth \
      --postgres-host=localhost \
      --postgres-port=5433 \
      --postgres-db=simpleidp \
      --postgres-user=user \
      --postgres-password-path=/db/password \
      --grpc-addr=0.0.0.0:3001 \
      --http-addr=0.0.0.0:8443 \
      --ca-cert= --key=/config/auth/idp.key \
      --cert=/config/auth/idp.crt \
      --template-dir=/templates \
      --encryption-secret-path=/config/auth/encryption_pass.txt \
      --smtp-server= \
      --smtp-password-path=/db/password \
      --tracing=tracing:6831 \
      --auth-providers-config-file=/config/auth/providers.json \
      --dev
}

function startPantheon() {
  podman run --name=pantheon --hostname=pantheon -d --pod=dev \
    -e JDBC_DATABASE_URL='jdbc:postgresql://localhost:5433/pantheon?user=pantheon&password=test' \
    -e PLAY_HTTP_SECRET_KEY=secret \
    -e DATASTORE_API_URL=http://localhost:9191/internal/api/v1/ \
    -e IDP_BASE_URL=https://localhost:8443/api/v2/ \
    -e SIGN_AND_VERIFY_ALGO=RS512 \
    -e SERVICE_TOKEN_KEY=/config/auth/idp.key \
    -e AUTHENTICATE_CERT=/config/auth/idp.crt \
    -e TRACING_ENABLED=true \
    -e TRACING_HOST=localhost \
    -e TRACING_SYNC=true \
    -v ./config/auth:/config/auth \
    -v ./config/pantheon:/config/pantheon \
    --add-host=pantheon:127.0.0.1 \
    eu.gcr.io/dev-and-test-env/pantheon:latest \
      -Dconfig.resource=environments/production.conf \
      -J-javaagent:conf/jmx_prometheus_javaagent.jar=8081:/config/pantheon/metrics.yaml \
      -Djava.rmi.server.hostname=localhost \
      -Dcom.sun.management.jmxremote.port=5001 \
      -Dcom.sun.management.jmxremote.rmi.port=5001 \
      -Dcom.sun.management.jmxremote.ssl=false \
      -Dcom.sun.management.jmxremote.local.only=true \
      -Dcom.sun.management.jmxremote.access.file=/tmp/jmx-security/jmxremote.access \
      -Dcom.sun.management.jmxremote.password.file=/tmp/jmx-security/jmxremote.password
}

function startProfiler() {
  podman run --name=profiler --hostname=profiler -d --pod=dev \
    -e PROFILER_PANTHEON_PG_HOST=pantheon \
    -e PROFILER_PANTHEON_PG_PORT=5431 \
    -e PROFILER_DEBUG=true \
    -e PROFILER_LOG_LEVEL=DEBUG \
    -e JAEGER_DISABLED=false \
    -e JAEGER_AGENT_HOST=localhost \
    eu.gcr.io/dev-and-test-env/profiler:dev
}

function startDatastore() {
  podman run --name=datastore --hostname=datastore -d --pod=dev \
    -e JAEGER_DISABLED=false \
    -e JAEGER_AGENT_HOST=localhost \
    -v ./config/enc-passphrase:/enc/passphrase \
    -v ./config/db-password:/db/password \
    -v ./config/blobstore/secret_key:/blobstorage/secret \
    -v ./config/auth:/config/auth \
    eu.gcr.io/dev-and-test-env/datastore-manager:dev /bin/ds-manager \
      --log-level=DEBUG \
      --http-address=:9191 \
      --metrics-health-address=:9092 \
      --encryption-passphrase-path=/enc/passphrase \
      --pantheon-url=http://localhost:4300 \
      --metadb-host=localhost \
      --metadb-port=5433 \
      --metadb-name=datastore \
      --metadb-user=user \
      --metadb-password-path=/db/password \
      --metadb-conn-pool-size=10 \
      --storagedb-host=localhost \
      --storagedb-port=5433 \
      --storagedb-user=user \
      --storagedb-password-path=/db/password \
      --storagedb-conn-pool-size=10 \
      --blobstorage-host=localhost:9000 \
      --blobstorage-access-key=minioaccesskey \
      --blobstorage-secret-key-path=/blobstorage/secret \
      --blobstorage-ssl=false \
      --blobstorage-bucket=ds-mgr-uploads \
      --blobstorage-path-prefix=local/lemon \
      --profiling=:9292 \
      --jwt-public-key-path=/config/auth/idp.crt
}

function startDatastoreWorker() {
  podman run --name=datastore-worker --hostname=datastore-worker -d --pod=dev \
    -e JAEGER_AGENT_HOST=localhost \
    -v ./config/db-password:/db/password \
    -v ./config/blobstore/secret_key:/blobstorage/secret \
    -v ./config/auth:/config/auth \
    eu.gcr.io/dev-and-test-env/datastore-manager:dev /bin/ds-ingest \
      --log-level=DEBUG \
      --metrics-health-address=:9093 \
      --metadb-host=localhost \
      --metadb-port=5433 \
      --metadb-name=datastore \
      --metadb-user=user \
      --metadb-password-path=/db/password \
      --metadb-conn-pool-size=10 \
      --storagedb-host=metadb \
      --storagedb-port=5433 \
      --storagedb-user=user \
      --storagedb-password-path=/db/password \
      --storagedb-conn-pool-size=10 \
      --blobstorage-host=localhost:9000 \
      --blobstorage-access-key=minioaccesskey \
      --blobstorage-secret-key-path=/blobstorage/secret \
      --blobstorage-ssl=false \
      --blobstorage-bucket=ds-mgr-uploads \
      --blobstorage-path-prefix=local/lemon \
      --pantheon-postgres='postgres://localhost:5431?user=datastore&sslmode=disable' \
      --pantheon-jwt-signing-key=/config/auth/idp.key
}

function startHub() {
  podman run --name=hub --hostname=hub -d --pod=dev \
    -e JAEGER_DISABLED=false \
    -e JAEGER_AGENT_HOST=localhost \
    -v ./config/db-password:/db/password \
    -v ./config/auth:/config/auth \
    eu.gcr.io/dev-and-test-env/hub:dev /bin/hub \
      --log-level=DEBUG \
      --http-address=:9192 \
      --metrics-health-address=:9091 \
      --metadb-host=localhost \
      --metadb-port=5433 \
      --metadb-name=hub \
      --metadb-user=user \
      --metadb-password-path=/db/password \
      --metadb-conn-pool-size=10 \
      --profiling=:9292 \
      --jwt-public-key-path=/config/auth/idp.crt \
      --jwt-private-key-path=/config/auth/idp.key \
      --pantheon-url=http://localhost:4300 \
      --datastore-url=http://localhost:9191 \
      --profiler-url=http://localhost:8080 \
      --encryption-passphrase-path=/db/password
}

function startGraphql() {
  podman run --name=graphql --hostname=graphql -d --pod=dev \
    -e JAEGER_DISABLED=false \
    -e JAEGER_AGENT_HOST=localhost \
    -e PGQL_ENABLE_PLAYGROUND=false \
    -e PGQL_LOG_LEVEL=DEBUG \
    -e PGQL_PANTHEON_HOST=localhost \
    -e PGQL_PANTHEON_PORT=5431 \
    -e PGQL_PANTHEON_DISABLE_SSL=true \
    -e PGQL_HUB_URL=http://localhost:9192 \
    -e PGQL_JWT_PUBLIC_KEY_PATH=/config/auth/idp.crt \
    -e PGQL_JWT_PRIVATE_KEY_PATH=/config/auth/key.pem \
    -e PGQL_REDIS_ADDR=localhost:6379 \
    -v ./config/auth:/config/auth \
    eu.gcr.io/dev-and-test-env/pgql-server:dev \
    --http-address=:8082
}

function startUI() {
  podman run --name=ui --hostname=ui -d --pod=dev \
    -e DEPLOYMENTURL=http://localhost:9898 \
    -v ./config/ui-config.js:/usr/share/nginx/html/config.js \
    eu.gcr.io/dev-and-test-env/contiamo-ui:latest
}

function startStunnel() {
  podman run --name=stunnel --hostname=stunnel -d --pod=dev \
    -v ./config/pg-stunnel/cert:/cert \
    -v ./podman/stunnel.conf:/etc/stunnel/config/stunnel.conf \
    eu.gcr.io/dev-and-test-env/stunnel:v0.0.19
}

function startNginx() {
  podman run --name=nginx --hostname=nginx -d --pod=dev \
    -v ./podman/ingress/nginx.conf:/etc/nginx/conf.d/default.conf \
    -v ./podman/ingress:/ingress \
    nginx:alpine
}

function start() {
  initVolumes
  initPod
  startRedis
  startTracing
  startMetadb
  startBlobstorage
  startIdp
  startPantheon
  startProfiler
  startDatastore
  startDatastoreWorker
  startHub
  startGraphql
  startUI
  startNginx
  # startStunnel
}

case $1 in
  pod) initVolumes; initPod;;
  redis) startRedis;;
  tracing) startTracing;;
  metadb) startMetadb;;
  blobstorage) startBlobstorage;;
  idp) startIdp;;
  pantheon) startPantheon;;
  profiler) startProfiler;;
  datastore) startDatastore;;
  datastore-worker) startDatastoreWorker;;
  hub) startHub;;
  graphql) startGraphql;;
  ui) startUI;;
  nginx) startNginx;;
  stunnel) startStunnel;;
  all) start;;
  *) start;;
esac
