#!/bin/bash
#
# Matrix Slack Service

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

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

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

cat > /federated/apps/matrixslack/docker-compose.yml <<EOF
services:
  matrixslack:
    image: dock.mau.dev/mautrix/slack:\${IMAGE_VERSION}
    container_name: matrixslack
    hostname: matrixslack.$DOMAIN
    restart: always
    networks:
      core:
        ipv4_address: 192.168.0.80
    env_file:
      - ./.env
    volumes:
      - ./data:/data
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.matrixslack.rule=Host(\`matrixslack.$DOMAIN\`)"
      - "traefik.http.routers.matrixslack.entrypoints=websecure"
      - "traefik.http.routers.matrixslack.tls.certresolver=letsencrypt"

networks:
  core:
    external: true
EOF

MATRIXSLACK_SECRET=$(create_password);

# Run to create the config file
docker run --rm -v /federated/apps/matrixslack/data:/data:z dock.mau.dev/mautrix/slack:latest

cat > /federated/apps/matrixslack/.env <<EOF
IMAGE_VERSION="latest"
EOF
chmod 600 /federated/apps/matrixslack/.env

# Change config values in config.yaml
sed -i '0,/address:.*/s//address: http:\/\/192.168.0.19:8008/' /federated/apps/matrixslack/data/config.yaml
sed -i "s/localhost/192.168.0.80/g" /federated/apps/matrixslack/data/config.yaml
sed -i "s#domain:.*#domain: matrix.$DOMAIN#g" /federated/apps/matrixslack/data/config.yaml
sed -i "s#uri:.*#uri: postgres://matrixslack:$MATRIXSLACK_SECRET@postgresql.$DOMAIN/matrixslack?sslmode=disable#g" /federated/apps/matrixslack/data/config.yaml
sed -i "s/example.com/matrix.$DOMAIN/g" /federated/apps/matrixslack/data/config.yaml

# Create database and user in postgresql
docker exec postgresql psql -U postgres -c "CREATE USER matrixslack WITH PASSWORD '$MATRIXSLACK_SECRET'" &> /dev/null
docker exec postgresql psql -U postgres -c "CREATE DATABASE matrixslack" &> /dev/null
docker exec postgresql psql -U postgres -c "GRANT ALL PRIVILEGES ON DATABASE matrixslack TO matrixslack" &> /dev/null

# Run again to create the registration file
docker run --rm -v /federated/apps/matrixslack/data:/data:z dock.mau.dev/mautrix/slack:latest

# Insert our Matrix Slack config into Matrix homeserverl.yaml
cp /federated/apps/matrixslack/data/registration.yaml /federated/apps/matrix/data/matrix/matrixslack-registration.yaml
cat >> /federated/apps/matrix/data/matrix/homeserver.yaml <<EOF
app_service_config_files:
- /data/matrixslack-registration.yaml
EOF

# Set permissions
chown -R 991:991 /federated/apps/matrix/data/matrix
 
echo -ne "done."
}
start_matrixslack() {
  # Start service with command to make sure it's up before proceeding
  start_service "matrixslack" "nc -z 192.168.0.80 29335 &> /dev/null" "7"

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

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

  # Delete database and user in postgresql
  docker exec postgresql psql -U postgres -c "DROP DATABASE matrixslack" &> /dev/null
  docker exec postgresql psql -U postgres -c "DROP USER matrixslack" &> /dev/null

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

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