#!/bin/bash

# Start some stuff: Sidekiq Web UI, collectd web UI, MariaDB container, Redis container.
#
# Looks like shell scripts are also going into here. You may additionally backup_database.
#
# Hey it's starting to turn into a real deploy script!

set -euox

DOCKER=podman
MARIADB_IMAGE=localhost/alpine-mariadb
REDIS_IMAGE=localhost/redis-7.2-rc-alpine
SCHEMACRAWLER_DIR=$HOME/schemacrawler-16.20.4-bin

function message(){
    plus="+"
    [[ $1 -ne 0 ]] && plus=-
    shift
    echo "[${plus}] $@"
}

# This will probably either go away or be moved to mariadb database as we migrate (eventually)
function backup_database() {
    shift
    running_container=
    if [ $# -eq 0 ]
    then
        running_container=$(${DOCKER} ps --format '{{.Image}} {{.Names}}' | grep cl-deck-builder2 | awk '{print $2}')
    else
        running_container="$1"
    fi
    if [ "x$running_container" = "x" ]
    then
        message 1 "deck builder container not running?"
    else
        message 0 "found $running_container"
        message 0 "copying database $running_container:/home/quicklisp/quicklisp/local-projects/cl-deck-builder2/deck_builder.sqlite3"
        $DOCKER cp "$running_container:/home/quicklisp/quicklisp/local-projects/cl-deck-builder2/deck_builder.sqlite3" \
                ~/sqlite3/$(date '+%Y-%m-%d')-deck_builder.sqlite3
        ls -l ~/sqlite3/$(date '+%Y-%m-%d')-deck_builder.sqlite3
    fi
}

function psychiq_up(){
    ps -C rackup >/dev/null
    if [ $? -ne 0 ]
    then
        message 0 "Starting Psychiq Web UI"
        rackup /home/user/code/cl-deck-builder2/ruby/config.ru &
    else
        message 1 "Psychiq Web UI already running."
    fi
}

function collectd_up() {
    ps -C python3 >/dev/null
    if [ $? -ne 0 ]
    then
        message 0 "Starting collectd Web UI"
        pushd /home/user/code/alpine-collectd-web/
        python3 runserver.py &
        popd
    else
        message 1 "collectd Web UI already running."
    fi
}

function mariadb_up (){
    mkdir -p ~/mariadb
    $DOCKER ps 2>&1| grep 3306 2>&1>/dev/null
    if [ $? -ne 0 ]
    then
        $DOCKER run --restart always -d -P -p 3306:3306 -v $HOME/mariadb:/var/lib/mysql $MARIADB_IMAGE
    else
        message 1 "mariadb already running."
    fi
}

function redis_up(){
    mkdir -p ~/redis
    $DOCKER ps 2>&1| grep 6379 2>&1>/dev/null
    if [ $? -ne 0 ]
    then
        $DOCKER run --restart always -d -P -p 6379:6379 -v $HOME/redis:/data $REDIS_IMAGE
    else
        message 1 "redis already running."
    fi
}

function schemacrawler(){
    ORIG_DIR="$(pwd)"
    cd $SCHEMACRAWLER_DIR
    # Gentoo Strikes Again!
    # https://github.com/schemacrawler/SchemaCrawler/issues/394
    _JAVA_OPTIONS=-Djava.io.tmpdir=. \
        bash bin/schemacrawler.sh \
        --user= \
        --password= \
        --database "$ORIG_DIR/deck_builder.sqlite3" \
        --server sqlite \
        --weak-associations=true \
        -i standard \
        -c schema \
        -o ~/www/db.png

    chown :www-data ~/www/db.png
}

function main(){
    # backup_database
    psychiq_up
    collectd_up
    mariadb_up
    redis_up
}

if [ $# -lt 1 ]
then
    message 1 "Usage: $0 [main | backup_database | psychiq_up | collectd_up | mariadb_up | redis_up | schemacrawler]"
else
    "$1" "$@"
fi
