PKG=#DZ-PKG#
INSTANCE=#DZ-INSTANCE#
ZVER=#DZ-ZVER#
PORT=#DZ-PORT#

# Random password stuff
RANDOMDEVICE=/dev/urandom
allowed=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./
declare -a RVAL

second () {
    echo $2
}

create_random_password() {
    if ! read -n 0 < $RANDOMDEVICE ; then
        echo 1>&2 "Warning, no random device found, password might be insecure"
        for i in 0 1 2 3 4 5 6 7; do
            RVAL[i]=$RANDOM
        done
    else
        for i in 0 1 2 3 4 5 6 7; do
            RVAL[i]=$(second $(od -N 1 -t d $RANDOMDEVICE))
        done
    fi

    PW=""
    for i in 0 1 2 3 4 5 6 7; do
        idx=$((${RVAL[i]} & 0x3F))
        PW="${PW}${allowed:$idx:1}"
    done

    printf "%s" $PW
}
# End

# if we do not have debconf, we just skip this
. /usr/share/debconf/confmodule || exit 0

db_capb backup

LAST_STATE=0
STATE=1
TRIES=0
while true; do

    if [ -d /var/lib/zope$ZVER/instance/$INSTANCE ] && \
         [ ! -f /var/lib/zope$ZVER/instance/$INSTANCE/var/Data.fs ]; then
        FIRST_STATE=1
    else
        FIRST_STATE=2
    fi

    [ $STATE -ge $FIRST_STATE ] || STATE=$FIRST_STATE
    case "$STATE" in
        1)
            # directory exists, but no data file
            db_register zope-common/remove-instance-without-data $PKG/remove-instance-without-data || true
            db_subst $PKG/remove-instance-without-data instance "$INSTANCE" || true
            db_subst $PKG/remove-instance-without-data zver "$ZVER" || true
            if db_go; then
                db_get $PKG/remove-instance-without-data
                if [ "$RET" = abort ]; then
                    exit 10
                fi
                STATE=2
            else
                STATE=1
            fi
            ;;
        2)
            db_register zope-common/admin-user $PKG/admin-user
            db_subst $PKG/admin-user instance "$INSTANCE" || true
            db_subst $PKG/admin-user zver "$ZVER" || true
            if ! db_input high $PKG/admin-user; then
                db_set $PKG/admin-user "admin"
            fi
            if db_go; then
                db_get $PKG/admin-user
                if echo "$RET" | grep -q '^[a-zA-Z][a-zA-Z0-9]*$'; then
                    STATE=3
                else
                    STATE=2
                fi
            else
                STATE=1
            fi
           ;;
        3)
            RET=""
            db_get $PKG/admin-password || true
            PWD="$RET"

            db_register zope-common/admin-password $PKG/admin-password
            db_subst $PKG/admin-password instance "$INSTANCE" || true
            db_subst $PKG/admin-password zver "$ZVER" || true
            if ! db_input high $PKG/admin-password && [ "$PWD" = "" ]; then
                rpassword=$(create_random_password)
                db_register zope-common/admin-password-confirmation $PKG/admin-password-confirmation || true
                db_set $PKG/admin-password "$rpassword"
                db_set $PKG/admin-password-confirmation "$rpassword"
                db_register zope-common/admin-automatic-password $PKG/admin-automatic-password || true
                db_subst $PKG/admin-automatic-password instance "$INSTANCE" || true
                db_subst $PKG/admin-automatic-password zver "$ZVER" || true
                db_subst $PKG/admin-automatic-password user "admin" || true
                db_subst $PKG/admin-automatic-password password "$rpassword" || true
                db_input high $PKG/admin-automatic-password || true
                db_go || true
                STATE=4
                continue
            fi
            if db_go; then
                db_get $PKG/admin-password
                if [ -n "$RET" ]; then
                    PASS="$RET"
                    db_register zope-common/admin-password-confirmation $PKG/admin-password-confirmation || true
                    db_subst $PKG/admin-password-confirmation instance "$INSTANCE" || true
                    db_subst $PKG/admin-password-confirmation zver "$ZVER" || true
                    db_input high $PKG/admin-password-confirmation || true
                    db_go
                    db_get $PKG/admin-password-confirmation
                    if [ "$PASS" = "$RET" ]; then
                       STATE=4
                    else
                       STATE=3
                    fi
                else
                    STATE=3
                fi
            else
                STATE=2
            fi
            ;;
        4)
            db_register zope-common/instance-http-port $PKG/instance-http-port
            db_subst $PKG/instance-http-port instance "$INSTANCE" || true
            db_subst $PKG/instance-http-port zver "$ZVER" || true
            db_fget $PKG/instance-http-port seen
            if [ "$RET" = false ]; then
                db_set $PKG/instance-http-port "$PORT" || true
            fi
            db_input high $PKG/instance-http-port || true
            if db_go; then
                STATE=5
            else
                STATE=3
            fi
            ;;
        5)
            db_register zope-common/keep-data-on-purge $PKG/keep-data-on-purge
            db_subst $PKG/keep-data-on-purge instance "$INSTANCE" || true
            db_subst $PKG/keep-data-on-purge zver "$ZVER" || true
            db_input high $PKG/keep-data-on-purge || true
            if db_go; then
                STATE=6
            else
                STATE=4
            fi
            ;;

        *)
            break
    esac
    if [ $LAST_STATE -eq $STATE ]; then
        : $((TRIES++))
        if [ $TRIES -gt 3 ]; then
            exit $((20 + $STATE))
        fi
    fi
    LAST_STATE=$STATE
done

if [ $STATE -le $FIRST_STATE ]; then
    exit 30
fi
