#!/bin/sh -e

APP_NAME="openproject"
APP_SAFE_NAME="_APP_SAFE_NAME_"

# Source debconf library.
. /usr/share/debconf/confmodule

db_input high ${APP_NAME}/smtp/method || true
db_go

db_get ${APP_NAME}/smtp/method
email_delivery_method="$RET"

db_input high ${APP_NAME}/smtp/admin_email || true
db_go

if [ "$email_delivery_method" = "sendmail" ]; then
  echo "You chose to deliver openproject email with sendmail."
  exit 0
else
  db_input high ${APP_NAME}/smtp/host || true
  db_input high ${APP_NAME}/smtp/port || true
  db_input high ${APP_NAME}/smtp/username || true
  db_input high ${APP_NAME}/smtp/password || true
  db_go

  default_hostname=$(hostname -f)

  db_get ${APP_NAME}/server/hostname || true
  if [ ! -z "$RET" ]; then
    default_hostname="$RET"
  fi
  # replace default value
  db_set ${APP_NAME}/smtp/domain "${default_hostname}"
  db_input high ${APP_NAME}/smtp/domain || true
  db_go
fi
