#!/bin/sh
set -e

log() {
    logger -t prebaseconfig "$@"
}

. /usr/share/debconf/confmodule

TEMPLATE_ROOT=debian-installer/network-console
PACKAGE=network-console-config

if [ "$TERM_TYPE" != network ]; then
    # Apparently the user has decided not to install over SSH after all,
    # so base-configuration should be started on the console from inittab.
    # Purge the network-console-config package (if it was installed)
    log "Installation is not over SSH; purging $PACKAGE from /target"
    chroot /target dpkg --purge $PACKAGE >/dev/null 2>&1 || true
    exit 0
fi

if chroot /target dpkg -l $PACKAGE 2>/dev/null | grep "^ii" ; then
    log "Setting up the system to allow base-configuration to be run over SSH"
    db_input critical $TEMPLATE_ROOT/prebaseconfig-reminder
    db_go
else
    ## FIXME (see #279090) ##
    # There really should be a dialog shown here
    # Not implemented yet because of string freeze for RC2 release
    log "Package $PACKAGE was not installed into /target;"
    log "the system will not be set up to run base-configuration over SSH"
    exit 0
fi

DIR=/etc/ssh/

mkdir /target/$DIR
cp $DIR/ssh_host_rsa_key* /target/$DIR
cp $DIR/sshd_config_target /target/$DIR/sshd_config

echo 'installer:x:0:0:installer:/:/usr/sbin/base-config-network-console' >> /target/etc/passwd
grep "^installer:" /etc/shadow >> /target/etc/shadow
