#!/bin/bash
#
# Autodiscover service for mail

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

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

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

  TRAEFIK_HTTPAUTH_STRING_AUTODISCOVER=$(echo `htpasswd -nb admin@$DOMAIN $ADMINPASS` | sed -e s/\\$/\\$\\$/g)

  if [ "$(uname -m)" = "aarch64" ]; then
    # The federatedcomputer/autodiscover-email-settings container is built
    # from the same sources as monogramm/autodiscover-email-settings
    # without modifications.
    # The only "change" is an aarch64 port.
    CONTAINER=federatedcomputer/autodiscover-email-settings
  else
    CONTAINER=monogramm/autodiscover-email-settings
  fi

cat > /federated/apps/autodiscover/docker-compose.yml <<EOF
services:
  autodiscover:
    image: ${CONTAINER}:\${IMAGE_VERSION}
    container_name: autodiscover
    hostname: autodiscover.$DOMAIN
    restart: always
    networks:
      core:
        ipv4_address: 192.168.0.32
    env_file:
      - ./.env
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.autodiscover.rule=Host(\`autoconfig.$DOMAIN\`) || Host(\`autodiscover.$DOMAIN\`)"
      - "traefik.http.routers.autodiscover.service=autodiscover"
      - "traefik.http.routers.autodiscover.entrypoints=websecure"
      - "traefik.http.routers.autodiscover.tls.certresolver=letsencrypt"
      - "traefik.http.services.autodiscover.loadbalancer.server.port=8000"
      - "traefik.http.routers.autodiscover.middlewares=autodiscover-auth"
      - "traefik.http.middlewares.autodiscover-auth.basicauth.users=$TRAEFIK_HTTPAUTH_STRING_AUTODISCOVER"
      - "traefik.http.routers.autodiscovernew.rule=(Host(\`autoconfig.$DOMAIN\`) || Host(\`autodiscover.$DOMAIN\`)) && PathPrefix(\`/mail\`)"
      - "traefik.http.routers.autodiscovernew.entrypoints=websecure"
      - "traefik.http.routers.autodiscovernew.tls.certresolver=letsencrypt"
      - "traefik.http.routers.autodiscovernew.service=autodiscovernew"
      - "traefik.http.services.autodiscovernew.loadbalancer.server.port=8000"

networks:
  core:
    external: true
EOF

cat > /federated/apps/autodiscover/.env <<EOF
IMAGE_VERSION="$(current_version autodiscover)"
COMPANY_NAME=$COMPANY
SUPPORT_URL=https://autodiscover.$DOMAIN
DOMAIN=$DOMAIN
# IMAP configuration (host mandatory to enable)
IMAP_HOST=mail.$DOMAIN
IMAP_PORT=993
IMAP_SOCKET=SSL
# POP configuration (host mandatory to enable)
POP_HOST=mail.$DOMAIN
POP_PORT=995
POP_SOCKET=SSL
# SMTP configuration (host mandatory to enable)
SMTP_HOST=mail.$DOMAIN
SMTP_PORT=587
SMTP_SOCKET=STARTTLS
# MobileSync/ActiveSync configuration (url mandatory to enable)
#      - MOBILESYNC_URL=https://sync.example.com
#- MOBILESYNC_NAME=sync.example.com
# LDAP configuration (host mandatory to enable)
#LDAP_HOST=ldap.$DOMAIN
#LDAP_PORT=636
#LDAP_SOCKET=SSL
#LDAP_BASE=dc=federatedcomputer,dc=cloud
#LDAP_USER_FIELD=mail
#LDAP_USER_BASE=ou=people,dc=federatedcomputer,dc=cloud
#LDAP_SEARCH=(|(objectClass=PostfixBookMailAccount))
# Apple mobile config identifiers (identifier mandatory to enable)
PROFILE_IDENTIFIER=cloud.federatedcomputer.autodiscover
PROFILE_UUID=92943D26-CAB3-4086-897D-DC6C0D8B1E86
MAIL_UUID=7A981A9E-D5D0-4EF8-87FE-39FD6A506FAC
LDAP_UUID=6ECB6BA9-2208-4ABF-9E60-4E9F4CD7309E
EOF
chmod 600 /federated/apps/autodiscover/.env

# Add DNS records for auto discovery
docker exec pdns pdnsutil add-record $DOMAIN autoconfig A 86400 $EXTERNALIP
[ $? -ne 0 ] && fail "Couldn't add dns record for auto discovery"
docker exec pdns pdnsutil add-record $DOMAIN autodiscover A 86400 $EXTERNALIP
[ $? -ne 0 ] && fail "Couldn't add dns record for auto discovery"
docker exec pdns pdnsutil add-record $DOMAIN @ TXT 86400 "\"mailconf=https://autoconfig.$DOMAIN/mail/config-v1.1.xml\""
[ $? -ne 0 ] && fail "Couldn't add dns record for auto discovery"
docker exec pdns pdnsutil add-record $DOMAIN _imaps._tcp SRV 86400 "0 0 993 mail.$DOMAIN"
[ $? -ne 0 ] && fail "Couldn't add dns record for auto discovery"
docker exec pdns pdnsutil add-record $DOMAIN _pop3s._tcp SRV 86400 "0 0 995 mail.$DOMAIN"
[ $? -ne 0 ] && fail "Couldn't add dns record for auto discovery"
docker exec pdns pdnsutil add-record $DOMAIN _imaps._tcp SRV 86400 "0 0 993 mail.$DOMAIN"
[ $? -ne 0 ] && fail "Couldn't add dns record for auto discovery"
docker exec pdns pdnsutil add-record $DOMAIN _submission._tcp SRV 86400 "0 0 587 $DOMAIN"
[ $? -ne 0 ] && fail "Couldn't add dns record for auto discovery"
docker exec pdns pdnsutil add-record $DOMAIN _autodiscover._tcp SRV 86400 "0 0 443 autodiscover.$DOMAIN"
[ $? -ne 0 ] && fail "Couldn't add dns record for auto discovery"
docker exec pdns pdnsutil add-record $DOMAIN _ldap._tcp SRV 86400 "0 0 636 ldap.$DOMAIN"
[ $? -ne 0 ] && fail "Couldn't add dns record for auto discovery"

echo -ne "done."
}

start_autodiscover() {
  # Start service with command to make sure it's up before proceeding
  start_service "autodiscover" "nc -z 192.168.0.32 8000 &> /dev/null" "8"

  echo -ne "done."
}
