#!/bin/bash
#
# Listmonk Service

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

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

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

cat > /federated/apps/listmonk/docker-compose.yml <<EOF
version: "3.7"

services:
  listmonk:
    image: listmonk/listmonk:\${IMAGE_VERSION}
    container_name: listmonk
    hostname: listmonk.$DOMAIN
    domainname: $DOMAIN
    restart: always
    networks:
      federated:
        ipv4_address: 172.99.0.21
    env_file:
      - ./.env
    volumes:
      - ./data/listmonk/config.toml:/listmonk/config.toml
      - ./data/listmonk/static:/listmonk/static
      - ./data/listmonk/uploads:/listmonk/uploads
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.listmonk.rule=Host(\`listmonk.$DOMAIN\`)"
      - "traefik.http.routers.listmonk.entrypoints=websecure"
      - "traefik.http.routers.listmonk.tls.certresolver=letsencrypt"

networks:
  federated:
    external: true
EOF

cat > /federated/apps/listmonk/.env <<EOF
IMAGE_VERSION="v2.4.0"
TZ=Etc/UTC
EOF

cat > /federated/apps/listmonk/data/listmonk/config.toml <<EOF
[app]
address = "0.0.0.0:9000"
admin_username = "listmonk"
admin_password = "$LISTMONKPASS"

# Database.
[db]
host = "postgresql.$DOMAIN"
port = 5432
user = "listmonk"
password = "$LISTMONK_SECRET"
database = "listmonk"
ssl_mode = "verify-full"
max_open = 25
max_idle = 25
max_lifetime = "300s"
EOF
chmod 600 /federated/apps/listmonk/data/listmonk/config.toml /federated/apps/listmonk/.env
 
kill -9 $SPINPID &> /dev/null
echo -ne "done."
}
start_listmonk() {
  # Install the database scheme first
  docker-compose -f /federated/apps/listmonk/docker-compose.yml run --rm listmonk ./listmonk --install --yes &> /dev/null

  # Change app.root_url and other settings to our domain
  docker exec postgresql psql -U listmonk -c "update settings set value='\"http://listmonk.$DOMAIN\"' where key='app.root_url'" &> /dev/null
  docker exec postgresql psql -U listmonk -c "update settings set value='\"listmonk <listmonk@listmonk.$DOMAIN>\"' where key='app.from_email'" &> /dev/null
  docker exec postgresql psql -U listmonk -c "update settings set value='[{\"host\": \"mail.$DOMAIN\", \"port\": 587, \"enabled\": true, \"password\": \"$ADMINPASS\", \"tls_type\": \"STARTTLS\", \"username\": \"fcore\", \"max_conns\": 10, \"idle_timeout\": \"15s\", \"wait_timeout\": \"5s\", \"auth_protocol\": \"login\", \"email_headers\": [], \"hello_hostname\": \"\", \"max_msg_retries\": 2, \"tls_skip_verify\": false}, {\"host\": \"smtp.gmail.com\", \"port\": 465, \"enabled\": false, \"password\": \"password\", \"tls_type\": \"TLS\", \"username\": \"username@gmail.com\", \"max_conns\": 10, \"idle_timeout\": \"15s\", \"wait_timeout\": \"5s\", \"auth_protocol\": \"login\", \"email_headers\": [], \"hello_hostname\": \"\", \"max_msg_retries\": 2, \"tls_skip_verify\": false}]' where key='smtp';" &> /dev/null

  # Start service with command to make sure it's up before proceeding
  start_service "listmonk" "nc -z 172.99.0.21 9000 &> /dev/null" "8"

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