#!/bin/bash
#
# Baserow Service

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

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

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

cat > /federated/apps/baserow/docker-compose.yml <<EOF
version: '3.7'

services:
  baserow:
    image: baserow/baserow:\${IMAGE_VERSION}
    container_name: baserow
    hostname: baserow.$DOMAIN
    domainname: $DOMAIN
    restart: always
    networks:
      federated:
        ipv4_address: 172.99.0.29
    env_file:
      - ./.env
    volumes:
      - ./data/baserow/data:/baserow/data
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.baserow.rule=Host(\`baserow.$DOMAIN\`)"
      - "traefik.http.routers.baserow.entrypoints=websecure"
      - "traefik.http.routers.baserow.tls.certresolver=letsencrypt"

networks:
  federated:
    external: true
EOF

cat > /federated/apps/baserow/.env <<EOF
IMAGE_VERSION="1.18.0"
BASEROW_PUBLIC_URL=https://baserow.$DOMAIN
DATABASE_HOST=postgresql.$DOMAIN
DATABASE_NAME=baserow
DATABASE_USER=baserow
DATABASE_PASSWORD_FILE=/baserow/data/.federated.postgresql.secret
DATABASE_PORT=5432
BASEROW_AMOUNT_OF_WORKERS=1
BASEROW_AMOUNT_OF_GUNICORN_WORKERS=1
BASEROW_RUN_MINIMAL=1
BASEROW_TRIGGER_SYNC_TEMPLATES_AFTER_MIGRATION="false"
EMAIL_SMTP=True
FROM_EMAIL=baserow@$DOMAIN
EMAIL_SMTP_HOST=mail.$DOMAIN
EMAIL_SMTP_PORT=587
EMAIL_SMTP_USER=fcore
EMAIL_SMTP_PASSWORD=$ADMINPASS
EMAIL_SMTP_USE_TLS=True
EOF
chmod 600 /federated/apps/baserow/.env

echo "$BASEROW_SECRET" > /federated/apps/baserow/data/baserow/data/.federated.postgresql.secret

cat > /federated/apps/baserow/data/createuser.sh <<EOF
#!/bin/bash

curl -X POST -H 'Content-Type: application/json' -i http://localhost/api/user/ --data '{
"name": "admin",
"email": "admin@$DOMAIN",
"password": "$ADMINPASS"
}' &> /dev/null
EOF
chmod +x /federated/apps/baserow/data/createuser.sh
 
kill -9 $SPINPID &> /dev/null
echo -ne "done."
}

start_baserow() {
  # Start service with command to make sure it's up before proceeding
  start_service "baserow" "docker exec baserow curl http://localhost:8000 &> /dev/null" "20"

  # Move createuser.sh and set it executable
  mv /federated/apps/baserow/data/createuser.sh /federated/apps/baserow/data/baserow/data/
  docker exec baserow chmod 755 /baserow/data/createuser.sh
  [ $? -ne 0 ] && fail "Couldn't chown createuser.sh in /federated/apps/baserow container"

  # Run createuser.sh inside baserow container
  docker exec baserow /baserow/data/createuser.sh
  [ $? -ne 0 ] && fail "Couldn't run createuser.sh in /federated/apps/baserow container"

  # Disable new sign ups
  docker exec postgresql psql -U baserow -c "update core_settings set allow_new_signups='f' where allow_new_signups='t';"

  # Remove baserow create user
  rm /federated/apps/baserow/data/baserow/data/createuser.sh

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