#!/bin/bash
set -eo pipefail
[[ $TRACE ]] && set -x

if [[ -e /usr/share/debconf/confmodule ]]; then
  . /usr/share/debconf/confmodule
fi

readonly DOKKU_ROOT="${DOKKU_ROOT:-/home/dokku}"
readonly DOKKU_LIB_ROOT="${DOKKU_LIB_PATH:-/var/lib/dokku}"
readonly DOKKU_LOGS_DIR="${DOKKU_LOGS_DIR:="/var/log/dokku"}"

main() {
  if [[ -f /etc/systemd/system/dokku-installer.service ]] || [[ -f /etc/init/dokku-installer.conf ]]; then
    service dokku-installer stop || true
  fi

  rm -f /etc/init/dokku-installer.conf
  rm -f /etc/init/dokku-redeploy.conf
  rm -f /etc/systemd/system/dokku-installer.service
  rm -f /etc/systemd/system/dokku-redeploy.service
  rm -f /etc/update-motd.d/99-dokku

  db_get "dokku/nginx_enable"
  if [ "$RET" = "true" ]; then
    (nginx -t && service nginx reload) || true
  fi

  if [[ "$1" == "purge" ]]; then
    rm -f /usr/local/openresty/nginx/conf/conf.d/dokku.conf
    rm -f /usr/local/openresty/nginx/conf/conf.d/dokku-installer.conf
    rm -f /etc/nginx/conf.d/dokku.conf
    rm -f /etc/nginx/conf.d/dokku-installer.conf
    rm -rf ${DOKKU_ROOT}/.dokkurc ${DOKKU_ROOT}/dokkurc ${DOKKU_ROOT}/tls
    rm -f ${DOKKU_ROOT}/.ssh/authorized_keys ${DOKKU_ROOT}/.sshcommand
    rm -f ${DOKKU_ROOT}/ENV ${DOKKU_ROOT}/HOSTNAME ${DOKKU_ROOT}/VERSION
    rm -rf ${DOKKU_ROOT}/.cache
    rm -rf ${DOKKU_LIB_ROOT}/core-plugins

    rm -f ${DOKKU_LIB_ROOT}/plugins/config.toml
    if [[ -d ${DOKKU_LIB_ROOT} ]]; then
      find -L ${DOKKU_LIB_ROOT} -type l -delete
      find ${DOKKU_LIB_ROOT} -type d -empty -delete
    fi

    if [[ -d ${DOKKU_ROOT} ]]; then
      find -L ${DOKKU_ROOT} -type l -delete
      find ${DOKKU_ROOT} -type d -empty -delete
    fi

    rm -rf "${DOKKU_LOGS_DIR}"

    deluser dokku || true
    delgroup dokku || true

    db_purge
  fi
}

main "$@"
