#!/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 "* Configuring pdnsmysql container.."

	. /federated/services/pdnsmysql/service

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

	cat > /federated/apps/pdnsmysql/docker-compose.yml <<EOF
services:
  mysql:
    image: ${CONTAINER}:\${IMAGE_VERSION}
    container_name: pdnsmysql
    hostname: pdnsmysql.$DOMAIN
    restart: always
    networks:
      core:
        ipv4_address: ${INTERNAL_IP}
    env_file:
      - ./.env
    volumes:
      - ./data/var/lib/mysql:/var/lib/mysql

networks:
  core:
    external: true
EOF

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

	cat > /federated/apps/pdnsmysql/.env <<EOF
IMAGE_VERSION="${VERSION}"
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.\n"
}
start_pdnsmysql() {
	# Start service with command to make sure it's up before proceeding
	start_service "pdnsmysql" "nc -z 192.168.0.10 3306 &> /dev/null" "8"

	echo -ne "done.\n"
}
