#!/bin/sh

set -e

get_passwd()
{
  db_get ejabberd/user
  if [ -n "$RET" ]; then
    db_input medium ejabberd/password || true
    db_input medium ejabberd/verify || true
    db_go || true
    db_get ejabberd/password
    PASSWORD="$RET"
    db_get ejabberd/verify
    VERIFY="$RET"
    if [ -z "$PASSWORD" ] || [ "$PASSWORD" != "$VERIFY" ]; then
      db_input medium ejabberd/nomatch || true
      db_go || true
      get_passwd
    fi
  fi
}

validate_hostname()
{
  HOST="$1"
  if [ -z "$HOST" ] || echo "$HOST" | grep -Evq '(^[a-z0-9]+$)|^([a-z0-9]*\.)+([a-z0-9]|[a-z0-9][a-z0-9_-]*[a-z0-9]\.)*([a-z0-9]+)$'; then
    echo 1
  else
    echo 0
  fi
}

get_hostname()
{
  # If there is no hostname registered in the debconf database (new install)
  # or if invoked by dpkg-reconfigure
  db_get ejabberd/hostname
  if [ -z "$RET" ] || ( [ -n "$RET" ] && [ ! -f /var/lib/ejabberd/.upgrade_flag ] ); then
    db_input medium ejabberd/hostname || true
    db_go || true

    db_get ejabberd/hostname
    # Convert hostname to lowercase if user entered in uppercase alphabets,
    # save it back to the debconf database, and read it back
    db_set ejabberd/hostname "$(echo "$RET" | sed -e 's/\(.*\)/\L\1/')" && \
      db_get ejabberd/hostname && \
      HOST="$RET"

    # Check hostname for invalid characters
    # If there were invalid characters, show error and ask again
    if [ "$(validate_hostname "$HOST")" != 0 ]; then
	db_reset ejabberd/hostname
        db_input medium ejabberd/invalidhostname || true
        db_go || true
	get_hostname
    fi
  # If this is an upgrade but the previous values of hostname registerd in
  # debconf database are invalid according to the validation standards at the
  # time of writing, we show a warning to the user and continue the upgrade
  elif [ -n "$RET" ] && [ "$(validate_hostname "$RET")" != 0 ] && [ -f /var/lib/ejabberd/.upgrade_flag ]; then
    db_subst ejabberd/invalidpreseed preseed "hostname"
    db_input medium ejabberd/invalidpreseed || true
    db_go || true
  fi
}

validate_username()
{
  USER="$1"
  if [ -z "$USER" ] || echo "$USER" | grep -Evq '^[[:alnum:]_\.-]+$'; then
    echo 1
  else
    echo 0
  fi
}

get_username()
{
  db_get ejabberd/hostname
  HOST="$RET"
  db_get ejabberd/user
  USER="$RET"

  # If there is no username registered in the debconf database (new install)
  # or if invoked by dpkg-reconfigure
  if [ -z "$USER" ] || ( [ -n "$USER" ] && [ ! -f /var/lib/ejabberd/.upgrade_flag ] ) ; then
    db_input medium ejabberd/user || true
    db_go || true

    db_get ejabberd/user
  
    # If username is not empty, check it for invalid characters
    if [ -n "$RET" ]; then
      # Strip the hostname if user entered it as a JID
      USER=$(echo "$RET" | sed -e "s/\(.*\)@$HOST/\1/")
      db_set ejabberd/user "$USER"
  
      # Check for invalid characters
      if [ "$(validate_username "$USER")" != 0 ]; then
        db_reset ejabberd/user
        db_reset ejabberd/password
        db_reset ejabberd/verify
        db_input medium ejabberd/invaliduser || true
        db_go || true
        get_username
      fi
    fi
  # If this is an upgrade but the previous values of username registerd in
  # debconf database are invalid according to the validation standards at the
  # time of writing, we show a warning to the user and continue the upgrade
  elif [ -n "$RET" ] && [ "$(validate_username "$USER")" != 0 ] && [ -f /var/lib/ejabberd/.upgrade_flag ]; then
    db_subst ejabberd/invalidpreseed preseed "username"
    db_input medium ejabberd/invalidpreseed || true
    db_go || true
  fi
}

get_credentials()
{
  db_get ejabberd/user
  USER=${RET:-admin}
  db_subst ejabberd/user user "$USER"
  db_get ejabberd/hostname
  HOST=${RET:-hostname}
  db_subst ejabberd/user hostname "$HOST"

  get_username
  get_passwd
}

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

get_hostname

FLAG="/var/lib/ejabberd/.admin_registered"
if [ ! -f $FLAG ]; then
  get_credentials
fi

# Remove upgrade flag as it is not needed anymore.
# Note that if unremoved, it will cause dpkg-reconfigure to skip its queries
rm -f /var/lib/ejabberd/.upgrade_flag

# If the user was not informed about the change in nodename, we show the user a
# high priority note on how to deal with changes. If not, show it as a medium
# priority (noted in the flag file)
if [ -f /var/lib/ejabberd/.ctlcfg.update ]; then
  msg_priority=$(cat /var/lib/ejabberd/.ctlcfg.update)
  if [ -n "$msg_priority" ] && ( [ "$msg_priority" = "high" ] || [ "$msg_priority" = "medium" ] ); then
    db_input "$msg_priority" ejabberd/nodenamechanges || true
    db_go || true
  fi
  # we don't need it anymore since debconf-show ejabberd should note whether
  # or not ejabberd/nodenamechanges has been viewed
  rm -f /var/lib/ejabberd/.ctlcfg.update
fi

exit 0

