#!/usr/bin/env bash

# Thanks Mouse Reeve for letting me steal their idea.

# exit on errors
set -e

function runweb {
    docker compose run --rm web "$@"
}

function execdb {
    docker compose exec db $@
}

function migrate {
    runweb python manage.py migrate "$@"
}

CMD=$1
if [ -n "$CMD" ]; then
    shift
fi

# show commands as they're executed
set -x

case "$CMD" in

    announce)
        runweb python manage.py announce
        ;;
    backup)
        ${DOCKER_PATH} exec -u root ausglamr_db_1 pg_dump -v -Fc -U ausglamr -d "ausglamr" -f /tmp/ausglamr_backup.dump
        ${DOCKER_PATH} cp ausglamr_db_1:/tmp/ausglamr_backup.dump ${BACKUPS_DIR}/
        mv ${BACKUPS_DIR}/ausglamr_backup.dump ${BACKUPS_DIR}/ausglamr_backup_$(date +'%a').dump
        ;;
    black)
        docker compose run --rm web black ausglamr blogs
        ;;
    check_feeds)
        runweb python manage.py check_feeds
        ;;
    collectstatic)
        runweb python manage.py collectstatic
        ;;
    copystatic)
		cp -r static /srv/ausglamr/
    	;;
    createsuperuser)
        runweb python manage.py createsuperuser --no-input
        ;;
    dbshell)
        execdb psql -U ${POSTGRES_USER} ${POSTGRES_DB}
        ;;
    manage)
        runweb python manage.py "$@"
        ;;
    makemigrations)
        runweb python manage.py makemigrations "$@"
        ;;
    migrate)
        migrate "$@"
        ;;
    pylint)
        docker compose run --rm web pylint ausglamr blogs
        ;;
    queue_announcements)
        runweb python manage.py queue_announcements
        ;;
    resetdb)
        docker compose rm -svf
        docker volume rm -f ausglamr_pgdata
        migrate
        ;;
    send_weekly_email)
        runweb python manage.py send_weekly_email
        ;;
    test)
        runweb python manage.py test blogs/tests "$@"
        ;;
    *)
        set +x
        echo "That is not a command"
        ;;
esac
