#!/bin/sh

set -e

# version information maintained by debian/rules
NEWVERSION=14

. /usr/share/debconf/confmodule

try_upgrade () {
    # look for the newest "main" cluster
    OLDVERSION=$(pg_lsclusters -h | grep -E '^[0-9.]+ +main ' | tail -n1)
    [ "$OLDVERSION" ] || return 0 # no cluster found
    OLDVERSION=${OLDVERSION%% *}

    # check if that's older than the newest version
    dpkg --compare-versions $OLDVERSION lt $NEWVERSION || return 0

    # ask if the user wants to upgrade
    db_fset postgresql/auto_upgrade seen false
    db_subst postgresql/auto_upgrade oldversion $OLDVERSION
    db_subst postgresql/auto_upgrade newversion $NEWVERSION
    db_input high postgresql/auto_upgrade || true
    db_go || true
}

try_upgrade

db_stop
