#!/bin/bash
#
# PowerDNS Nginx Service

PATH=$HOME/.docker/cli-plugins:/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

config_pdnsstatic() {
  echo -ne "\n* Configuring /federated/apps/pdns-static container.."
  spin &
  SPINPID=$!

  DOMAIN_ARRAY=(${DOMAIN//./ })
  DOMAIN_FIRST=${DOMAIN_ARRAY[0]}
  DOMAIN_MIDDLE=${DOMAIN_ARRAY[1]}
  DOMAIN_LAST=${DOMAIN_ARRAY[2]}

  if [ ! -d "/federated/apps/pdns-static" ]; then
    mkdir -p /federated/apps/pdns-static
  fi

#  DOMAIN_ARRAY=(${DOMAIN//./ })
#  DOMAIN_FIRST=${DOMAIN_ARRAY[0]}
#  DOMAIN_LAST=${DOMAIN_ARRAY[1]}

cat > /federated/apps/pdns-static/docker-compose.yml <<EOF
services:
  pdns-static:
    image: pschiffe/pdns-admin-static\${IMAGE_VERSION}
    container_name: pdns-static
    hostname: pdns-static.$DOMAIN
    restart: always
    networks:
      core:
        ipv4_address: 192.168.0.7
    ports:
      - "8989:80"
    volumes:
      - /etc/localtime:/etc/localtime:ro
    env_file:
      - ./.env

networks:
  core:
    external: true
EOF

cat > /federated/apps/pdns-static/.env <<EOF
IMAGE_VERSION="@sha256:c75fd98215db2ac2d4abe6e56710f93fecf3394e984c017fa9fffa5228d7b35a"
EOF
chmod 600 /federated/apps/pdns-static/.env
 
kill -9 $SPINPID &> /dev/null
echo -ne "done."
}
start_pdnsstatic() {
  # Start service with command to make sure it's up before proceeding
  start_service "pdns-static" "nc -z 192.168.0.7 80 &> /dev/null"

  kill -9 $SPINPID &> /dev/null
  echo -ne "done."
}
