#!/bin/bash
#
# Matrix Signal Service

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

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

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

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

networks:
  core:
    external: true
EOF

MATRIXSIGNAL_SECRET=$(create_password);

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

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

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

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

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

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

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

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

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

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

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

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