#!/bin/bash
#
# Castopod Service

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

config_castopod() {
  echo -ne "\n* Configuring /federated/apps/castopod container.."
  spin &
  SPINPID=$!

  if [ ! -d "/federated/apps/castopod" ]; then
    mkdir -p /federated/apps/castopod/data/var/www/castopod/public/media
    chown -R 33 /federated/apps/castopod/data/var
  fi

cat > /federated/apps/castopod/docker-compose.yml <<EOF
version: "3.7"

services:
  castopod:
    image: castopod/castopod:\${IMAGE_VERSION}
    container_name: castopod
    hostname: castopod.$DOMAIN
    domainname: $DOMAIN
    restart: always
    networks:
      federated:
        ipv4_address: 172.99.0.33
    volumes:
      - ./data/var/www/castopod/public/media:/var/www/castopod/public/media
    env_file:
      - ./.env
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.castopod.rule=Host(\`castopod.$DOMAIN\`,\`podcasts.$DOMAIN\`)"
      - "traefik.http.routers.castopod.entrypoints=websecure"
      - "traefik.http.routers.castopod.tls.certresolver=letsencrypt"
      - "traefik.http.services.castopod.loadbalancer.server.port=8000"

networks:
  federated:
    external: true
EOF

CP_ANALYTICS_SALT=$(create_password);
CASTOPOD_SECRET=$(create_password);

cat > /federated/apps/castopod/.env <<EOF
IMAGE_VERSION="1.6.5"
MYSQL_DATABASE=castopod
MYSQL_USER=castopod
MYSQL_PASSWORD=$CASTOPOD_SECRET
CP_DATABASE_HOSTNAME=pdnsmysql.$DOMAIN
CP_BASEURL="https://castopod.$DOMAIN"
CP_ANALYTICS_SALT="$CP_ANALYTICS_SALT"
CP_EMAIL_SMTP_HOST="mail.$DOMAIN"
CP_EMAIL_FROM="castopod@$DOMAIN"
CP_EMAIL_SMTP_USERNAME="fcore"
CP_EMAIL_SMTP_PASSWORD=$ADMINPASS
CP_EMAIL_SMTP_PORT="587"
CP_EMAIL_SMTP_CRYPTO="tls"
EOF
chmod 600 /federated/apps/castopod/.env

# Create database and user in mysql
docker exec pdnsmysql bash -c "mysql -uroot -p$MYSQL_ROOTPASSWORD -e 'create database castopod;'"
docker exec pdnsmysql bash -c "mysql -uroot -p$MYSQL_ROOTPASSWORD -e \"CREATE USER 'castopod'@'%' IDENTIFIED BY '$CASTOPOD_SECRET';\""
docker exec pdnsmysql bash -c "mysql -uroot -p$MYSQL_ROOTPASSWORD -e \"grant all privileges on castopod.* to 'castopod'@'%';\""
docker exec pdnsmysql bash -c "mysql -uroot -p$MYSQL_ROOTPASSWORD -e 'flush privileges;'"
 
kill -9 $SPINPID &> /dev/null
echo -ne "done."
}
start_castopod() {
  # Start service with command to make sure it's up before proceeding
  start_service "castopod" "nc -z 172.99.0.33 8000 &> /dev/null" "7"

  CASTOPOD_BASE_URL="https://castopod.$DOMAIN"
  CASTOPOD_ADMIN_USERNAME="admin@$DOMAIN"
  CASTOPOD_ADMIN_EMAIL="admin@$DOMAIN"
  CASTOPOD_ADMIN_PASSWORD="$ADMINPASS"

  CSRF_TEST_NAME=$(curl -v -k -c /tmp/cp-cookies $CASTOPOD_BASE_URL/cp-install | sed -n 's/.*input.*name="csrf_test_name".*value="\([^"]\+\)".*/\1/p') &> /dev/null
  curl -v -b /tmp/cp-cookies --data-urlencode "csrf_test_name=$CSRF_TEST_NAME" --data-urlencode "username=$CASTOPOD_ADMIN_USERNAME" --data-urlencode "email=$CASTOPOD_ADMIN_EMAIL" --data-urlencode "password=$CASTOPOD_ADMIN_PASSWORD" $CASTOPOD_BASE_URL/cp-install/create-superadmin &> /dev/null
  rm /tmp/cp-cookies

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