#!/bin/bash

# This should deploy a masto instance neatly.
# Assumptions:
# - Using ubuntu!
# - Docker and docker-compose installed, not from ubuntu repo.
# - Reverse proxy:
#   - You are using jwilder/nginx-proxy to reverse proxy https
#   - You are using nginxproxy/acme-companion to generate letsencrypt certs
#   - These two are inside the external network 'httpsproxy'
# - You have an external network called 'mail' that serves the mail.
# - The domain is solarpunk.moe (might make it generic in future perhaps)
# - Your docker installation is configured root only (no custom docker users)

# Go to this script's dir
cd "$(dirname "${BASH_SOURCE[0]}")"

echo "Welcome to the solarpunk.moe installer."

read -p "Press enter to create public/system dir owned by UID 991, CTRL-C to cancel."
mkdir -p public/system/
sudo chown 991 public/system # Magic mastodon UID inside the container
sudo chown 991 favicon.ico   # Magic mastodon UID inside the container
sudo cp favicon.ico public/

# Run the mastodon setup wizard. Most of the options should be the defaults
read -p "Press enter to run the setup wizard, CTRL-C to cancel."
sudo docker-compose run --rm web bundle exec rake mastodon:setup

echo ES_ENABLED=true >> .env.production
echo ES_HOST=es >> .env.production
echo ES_PORT=9200 >> .env.production

# Start 'er up!
read -p "Press enter to bring up the instance, CTRL-C to cancel."
sudo docker-compose up -d

# Create elasticsearch index.
read -p "Press enter to make ElasticSearch indices, CTRL-C to cancel."
sudo docker-compose run --env RAILS_ENV=production web bin/tootctl search deploy

