#!/bin/bash
#
# Matrix Whatsapp Service

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

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

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

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

networks:
  core:
    external: true
EOF

MATRIXWHATSAPP_SECRET=$(create_password);

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

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

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

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

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

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

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

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

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

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

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

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