#!/bin/bash
#
# PowerDNS MySQL Service

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

config_pdnsmysql() {
  echo -ne "\n* Configuring /federated/apps/pdnsmysql container.."

  if [ ! -d "/federated/apps/pdnsmysql" ]; then
    mkdir -p /federated/apps/pdnsmysql/data/var/lib/mysql
  fi

cat > /federated/apps/pdnsmysql/docker-compose.yml <<EOF
version: '3.7'

services:
  mysql:
    image: mariadb:\${IMAGE_VERSION}
    container_name: pdnsmysql
    hostname: pdnsmysql.$DOMAIN
    domainname: $DOMAIN
    restart: always
    networks:
      federated:
        ipv4_address: 172.99.0.10
    env_file:
      - ./.env
    volumes:
      - ./data/var/lib/mysql:/var/lib/mysql

networks:
  federated:
    external: true
EOF

MYSQL_ROOTPASSWORD=$(create_password);
MYSQL_PASSWORD=$(create_password);

cat > /federated/apps/pdnsmysql/.env <<EOF
IMAGE_VERSION="10.7.8"
MYSQL_ROOT_PASSWORD=$MYSQL_ROOTPASSWORD
MYSQL_PASSWORD=$MYSQL_PASSWORD
MYSQL_DATABASE=pdns
MYSQL_USER=pdns
EOF
chmod 600 /federated/apps/pdnsmysql/.env
 
echo -ne "done."
}
start_pdnsmysql() {
  # Start service with command to make sure it's up before proceeding
  start_service "pdnsmysql" "nc -z 172.99.0.10 3306 &> /dev/null" "8"

  echo -ne "done."
}
