#!/bin/sh
set -e

. /usr/share/debconf/confmodule

#DEBHELPER#

USRBASE="/usr/share/debian-devel-changes-bot"
BASE="/var/lib/debian-devel-changes-bot"
CONFIGFILE="/etc/debian-devel-changes-bot.config"

if [ "$1" = "configure" ]
then
  db_get BTS/nickserv-password
  NICKSERV_PASSWORD="$RET"
  db_get BTS/admin-user
  ADMIN_USER="$RET"
  db_get BTS/admin-password
  ADMIN_PASSWORD="$RET"

  echo "# Settings for debian-devel-changes-bot" > $CONFIGFILE
  chmod o-rwx "$CONFIGFILE"
  echo "NICKSERV_PASSWORD=\"$NICKSERV_PASSWORD\"" >> $CONFIGFILE
  echo "ADMIN_USER=\"$ADMIN_USER\"" >> $CONFIGFILE
  echo "ADMIN_PASSWORD=\"$ADMIN_PASSWORD\"" >> $CONFIGFILE

  # Create admin user.
  if [ -n "$ADMIN_USER" ] && [ -n "$ADMIN_PASSWORD" ]
  then
    if ! grep -q "name $ADMIN_USER\$" "${BASE}/bot-config/conf/users.conf"
    then
      supybot-adduser -u "$ADMIN_USER" -p "$ADMIN_PASSWORD" \
        -c owner "${BASE}/bot-config/conf/users.conf"
    fi
  fi

  # Set NickServ password.
  sed "s,@NICKSERV_PASSWORD@,$NICKSERV_PASSWORD," \
    "${USRBASE}/bot-config/BTS.conf.in" > "${BASE}/bot-config/BTS.conf"
fi

exit 0
