#!/bin/bash
#
# Dashboard Service

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

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

  if [ ! -d "/federated/apps/dashboard" ]; then
    mkdir -p /federated/apps/dashboard/data
  fi

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

services:
  dashboard:
    image: federatedcomputer/dashboard:\${IMAGE_VERSION}
    container_name: dashboard
    hostname: dashboard.$DOMAIN
    restart: always
    networks:
      core:
        ipv4_address: 192.168.0.41
    env_file:
      - ./.env
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.dashboard.rule=Host(\`dashboard.$DOMAIN\`)"
      - "traefik.http.routers.dashboard.entrypoints=websecure"
      - "traefik.http.routers.dashboard.tls.certresolver=letsencrypt"

networks:
  core:
    external: true
EOF

cat > /federated/apps/dashboard/.env <<EOF
IMAGE_VERSION="latest"
DOMAIN="$DOMAIN"
TIER="$TIER"
EOF
chmod 600 /federated/apps/dashboard/.env

if [[ ! -f "/root/.docker/config.json" ]]; then
  [[ ! -d "/root/.docker" ]] && mkdir /root/.docker
cat > /root/.docker/config.json <<EOF
{
        "auths": {
                "https://index.docker.io/v1/": {
                        "auth": "ZGF2aWRwYXVseW91bmc6VkdBN2F6ajR5ZXQ1bW5oQGV0eA=="
                }
        }
}
EOF
chmod 600 /root/.docker/config.json
fi
 
echo -ne "done."
}
start_dashboard() {
  # Start service with command to make sure it's up before proceeding
  start_service "dashboard" "nc -z 192.168.0.41 8080 &> /dev/null" "7"

  docker exec pdns pdnsutil add-record $DOMAIN dashboard A 86400 $EXTERNALIP &> /dev/null
  [ $? -ne 0 ] && fail "Couldn't add dns record for dashboard"

  echo -ne "done."
}
uninstall_dashboard() {
  echo -ne "* Uninstalling dashboard container.."
  spin &
  SPINPID=$!

  # First stop the service
  cd /federated/apps/dashboard && docker compose -f docker-compose.yml -p dashboard down &> /dev/null

  # Delete the app directory
  rm -rf /federated/apps/dashboard

  # Delete the image
  docker image rm federatedcomputer/dashboard:$IMAGE_VERSION &> /dev/null

  # Delete the DNS record
  docker exec pdns pdnsutil delete-rrset $DOMAIN dashboard A

  kill -9 $SPINPID &> /dev/null
  echo -ne "done.\n"
}
email_dashboard() {
  echo -ne "* Sending email to customer.."
  spin &
  SPINPID=$!

cat > /federated/apps/mail/data/root/certs/mailfile <<EOF
<html>
<img src="https://www.federated.computer/wp-content/uploads/2023/11/logo.png" alt="" /><br>
<p>
<h4>Dashboard is now installed on $DOMAIN</h4>
<p>
Dear Customer,<br>
<br>
We'd like to introduce you to "Dashboard", a simple application listing all the applications installed on your Federated Core. You can find "Dashboard" at <a href="https://dashboard.$DOMAIN">https://dashboard.$DOMAIN</a>
<br>
<br>
This is the first step in a number of big changes you'll be seeing from Federated Computer in the coming months to improve and enhance your Federated Core. Thank you for being a customer!
<br>
<br>
Best,
<br>
Federated Computer
<p>
</html>
EOF

  # Send out e-mail from mail container with details
  docker exec mail bash -c "mail -r admin@$DOMAIN -a \"Content-type: text/html\" -s \"Application installed on $DOMAIN\" $EMAIL < /root/certs/mailfile"
  rm /federated/apps/mail/data/root/certs/mailfile

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