#!/bin/bash
#
# Element Service

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

config_element() {
  echo -ne "* Configuring element container.."

  if [ ! -d "/federated/apps/element" ]; then
    mkdir -p /federated/apps/element/data/element &> /dev/null
  fi

cat > /federated/apps/element/docker-compose.yml <<EOF
services:
  element:
    image: vectorim/element-web:\${IMAGE_VERSION}
    container_name: element
    hostname: element.$DOMAIN
    restart: always
    networks:
      core:
        ipv4_address: 192.168.0.20
    volumes:
      - ./data/element/element-config.json:/app/config.json
    env_file:
      - ./.env
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.element.rule=Host(\`element.$DOMAIN\`)"
      - "traefik.http.routers.element.entrypoints=websecure"
      - "traefik.http.routers.element.tls.certresolver=letsencrypt"
        
networks:
  core:
    external: true
EOF

[[ "${PLUS}" = "true" ]] && sed -i "s/letsencrypt/httpresolver/g" /federated/apps/element/docker-compose.yml

cat > /federated/apps/element/.env <<EOF
IMAGE_VERSION="$(current_version element)"
EOF
chmod 600 /federated/apps/element/.env

cat > /federated/apps/element/data/element/element-config.json <<EOF
{
    "default_server_config": {
        "m.homeserver": {
            "base_url": "https://matrix.$DOMAIN",
            "server_name": "matrix.$DOMAIN"
        },
        "m.identity_server": {
            "base_url": "https://vector.im"
        }
    },
    "jitsi": {
        "preferredDomain": "jitsi.$DOMAIN"
    },
    "brand": "Element",
    "integrations_ui_url": "https://scalar.vector.im/",
    "integrations_rest_url": "https://scalar.vector.im/api",
    "integrations_widgets_urls": [
        "https://scalar.vector.im/_matrix/integrations/v1",
        "https://scalar.vector.im/api",
        "https://scalar-staging.vector.im/_matrix/integrations/v1",
        "https://scalar-staging.vector.im/api",
        "https://scalar-staging.riot.im/scalar/api"
    ],
    "hosting_signup_link": "https://element.io/matrix-services?utm_source=element-web&utm_medium=web",
    "bug_report_endpoint_url": "https://element.io/bugreports/submit",
    "uisi_autorageshake_app": "element-auto-uisi",
    "showLabsSettings": true,
    "roomDirectory": {
        "servers": [
            "matrix.org",
            "gitter.im",
            "libera.chat"
        ]
    },
    "enable_presence_by_hs_url": {
        "https://matrix.org": false,
        "https://matrix-client.matrix.org": false
    },
    "terms_and_conditions_links": [
        {
            "url": "https://element.io/privacy",
            "text": "Privacy Policy"
        },
        {
            "url": "https://element.io/cookie-policy",
            "text": "Cookie Policy"
        }
    ],
    "hostSignup": {
      "brand": "Element Home",
      "cookiePolicyUrl": "https://element.io/cookie-policy",
      "domains": [
          "matrix.org"
      ],
      "privacyPolicyUrl": "https://element.io/privacy",
      "termsOfServiceUrl": "https://element.io/terms-of-service",
      "url": "https://ems.element.io/element-home/in-app-loader"
    },
    "sentry": {
        "dsn": "https://029a0eb289f942508ae0fb17935bd8c5@sentry.matrix.org/6",
        "environment": "develop"
    },
    "posthog": {
        "projectApiKey": "phc_Jzsm6DTm6V2705zeU5dcNvQDlonOR68XvX2sh1sEOHO",
        "apiHost": "https://posthog.element.io"
    },
    "privacy_policy_url": "https://element.io/cookie-policy",
    "features": {
        "feature_spotlight": true,
        "feature_video_rooms": true
    },
    "element_call": {
        "url": "https://element-call.netlify.app"
    },
    "map_style_url": "https://api.maptiler.com/maps/streets/style.json?key=fU3vlMsMn4Jb6dnEIFsx"
}
EOF

echo -ne "done.\n"
}
start_element() {
  # Start service with command to make sure it's up before proceeding
  start_service "element" "nc -z 192.168.0.20 80 &> /dev/null" "7"

  [[ "${PLUS}" != "true" ]] && docker exec pdns pdnsutil add-record $DOMAIN element A 86400 $EXTERNALIP &> /dev/null

  echo -ne "done.\n"
}
uninstall_element() {
  echo -ne "* Uninstalling element container.."

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

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

  # Delete the image
  docker image rm vectorim/element-web:$IMAGE_VERSION &> /dev/null

  # Delete the DNS record
  [[ "${PLUS}" != "true" ]] && docker exec pdns pdnsutil delete-rrset $DOMAIN element A

  echo -ne "done.\n"
}
