#!/bin/sh

. /usr/share/debconf/confmodule

exec 2>> /tmp/`basename ${0}`.log
set -x
PRESEED_FILE="`mktemp`"
TEMPLATE="`mktemp`"

# FIXME Add logic for sudo and possibly ssh-key passphrase
cat > "${TEMPLATE}" <<'END_OF_TEMPLATE'
Template: one-passwd/passwd
Type: password
Description: Enter password to use on this computer.

Template: one-passwd/passwd_title
Type: text
Description: Password entry

Template: one-passwd/passwd_uses
Type: multiselect
Choices: account, luks
Default: account, luks
Description: Uses for provided password.

Template: one-passwd/passwd_uses_title
Type: text
Description: Password uses
END_OF_TEMPLATE

debconf-loadtemplate one-passwd "${TEMPLATE}"
db_settitle one-passwd/passwd_title
db_input critical one-passwd/passwd
db_go
db_settitle one-passwd/passwd_uses_title
db_input critical one-passwd/passwd_uses
db_go

db_get one-passwd/passwd
PASSWD="${RET}"
db_get one-passwd/passwd_uses
USES="${RET}"

rm "${PRESEED_FILE}" || :

if [ "${USES}" != "${USES%account*}" ]; then
  db_unregister passwd/user-password-crypted
  for S in \
      "d-i passwd/user-password password ${PASSWD}" \
      "d-i passwd/user-password-again password ${PASSWD}" \
      "d-i passwd/user-password-crypted password "
  do
    echo "${S}" >> "${PRESEED_FILE}"
  done
fi

if [ "${USES}" != "${USES%luks*}" ]; then
  for S in \
      "d-i partman-crypto/passphrase string ${PASSWD}" \
      "d-i partman-crypto/passphrase-again string ${PASSWD}"
  do
    echo "${S}" >> "${PRESEED_FILE}"
  done
fi

/bin/debconf-set-selections "${PRESEED_FILE}"
