#!/bin/bash

# Abort if any command returns an error value 
set -e

# This script is called as the last step of the installation of the 
# package.  All the package's files are in place, dpkg has already
# done its automatic conffile handling, and all the packages we depend
# of are already fully installed and configured.

# The following idempotent stuff doesn't generally need protecting 
# against being run in the abort-* cases.

# Use debconf. (installs templates)
. /usr/share/debconf/confmodule
# all done with debconf here.
db_stop

case "$1" in
  triggered)
    # We don't print a status message here, as dpkg already said
    # "Processing triggers for ...".

    # test if /etc/pve is mounted; else simple exit to avoid
    # error during updates
    test -f /etc/pve/local/pve-ssl.pem || exit 0;

    test -e /proxmox_install_mode || invoke-rc.d pvedaemon restart
    test -e /proxmox_install_mode || invoke-rc.d pvestatd restart    
    test -e /proxmox_install_mode || invoke-rc.d pveproxy restart
    test -e /proxmox_install_mode || invoke-rc.d spiceproxy restart

    exit 0;;

  configure)
    # Configure this package.  If the package must prompt the user for
    # information, do it here.

    mkdir /etc/pve 2>/dev/null || true

    # remove old APL dir
    rm -rf /var/lib/pve-manager/apl-available

    if test ! -e /var/lib/pve-manager/apl-info/download.proxmox.com; then
	mkdir -p /var/lib/pve-manager/apl-info
	cp /usr/share/doc/pve-manager/aplinfo.dat /var/lib/pve-manager/apl-info/download.proxmox.com
	pveam update || true
    fi

    if test -f /root/.forward; then
	if ! grep -q '|/usr/bin/pvemailforward' /root/.forward; then 
	    echo -e "|/usr/bin/pvemailforward\n$(cat /root/.forward)" >/root/.forward.tmp
	    mv /root/.forward.tmp /root/.forward
	fi
    else
	echo '|/usr/bin/pvemailforward' >/root/.forward
    fi

    # disable fancy init messages (bad with bootlogd)
    test -f /etc/lsb-base-logging.sh || echo "FANCYTTY=0" >/etc/lsb-base-logging.sh 

    update-rc.d pvedaemon defaults 21 79 >/dev/null
    update-rc.d pveproxy defaults 21 79 >/dev/null
    update-rc.d spiceproxy defaults 21 79 >/dev/null
    update-rc.d pvestatd defaults 21 79 >/dev/null
    update-rc.d pvebanner start 99 2 3 4 5 . >/dev/null
    update-rc.d pvenetcommit start 15 S . >/dev/null
    update-rc.d pve-manager defaults 25 75 >/dev/null

    test -e /proxmox_install_mode || invoke-rc.d pvedaemon restart
    test -e /proxmox_install_mode || invoke-rc.d pveproxy restart
    test -e /proxmox_install_mode || invoke-rc.d spiceproxy restart
    test -e /proxmox_install_mode || invoke-rc.d pvestatd restart
    
    # rewrite banner
    test -e /proxmox_install_mode || pvebanner || /bin/true

    #a2ensite pve.conf >/dev/null 2>&1

    # There are three sub-cases:
    if test "${2+set}" != set; then
      # We're being installed by an ancient dpkg which doesn't remember
      # which version was most recently configured, or even whether
      # there is a most recently configured version.
      :

    elif test -z "$2" -o "$2" = "<unknown>"; then
      # The package has not ever been configured on this system, or was
      # purged since it was last configured.
      :

    else
      # Version $2 is the most recently configured version of this
      # package.
      :

    fi ;;
  abort-upgrade)
    # Back out of an attempt to upgrade this package FROM THIS VERSION
    # to version $2.  Undo the effects of "prerm upgrade $2".
    :

    ;;
  abort-remove)
    if test "$2" != in-favour; then
      echo "$0: undocumented call to \`postinst $*'" 1>&2
      exit 0
    fi
    # Back out of an attempt to remove this package, which was due to
    # a conflict with package $3 (version $4).  Undo the effects of
    # "prerm remove in-favour $3 $4".
    :

    ;;
  abort-deconfigure)
    if test "$2" != in-favour -o "$5" != removing; then
      echo "$0: undocumented call to \`postinst $*'" 1>&2
      exit 0
    fi
    # Back out of an attempt to deconfigure this package, which was
    # due to package $6 (version $7) which we depend on being removed
    # to make way for package $3 (version $4).  Undo the effects of
    # "prerm deconfigure in-favour $3 $4 removing $6 $7".
    :

    ;;
  *) echo "$0: didn't understand being called with \`$1'" 1>&2
     exit 0;;
esac

exit 0
