#!/bin/sh
# postinst script for eole-lxc-controller
#
# see: dh_installdeb(1)

set -e

# summary of how this script can be called:
#        * <postinst> `configure' <most-recently-configured-version>
#        * <old-postinst> `abort-upgrade' <new version>
#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
#          <new-version>
#        * <postinst> `abort-remove'
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package

LXC_DEFAULT=/etc/default/lxc-net
DNSMASQ_CONF=/etc/dnsmasq.d/lxc
LXC_CONF=/etc/lxc/default.conf

no_lxc_running() {
    [ -z "$(lxc-ls --active)" ]
}

case "$1" in
    configure)
        # Modify network IP
	no_lxc_running && service lxc-net stop 2>/dev/null || true
	cat > ${LXC_DEFAULT} <<EOF
# This file is auto-generated by lxc.postinst if it does not
# exist.  Customizations will not be overridden.
# Leave USE_LXC_BRIDGE as "true" if you want to use br0 for your
# containers.  Set to "false" if you'll use virbr0 or another existing
# bridge, or mavlan to your host's NIC.
USE_LXC_BRIDGE="true"

# If you change the LXC_BRIDGE to something other than br0, then
# you will also need to update your /etc/lxc/default.conf as well as the
# configuration (/var/lib/lxc/<container>/config) for any containers
# already created using the default config to reflect the new bridge
# name.
# If you have the dnsmasq daemon installed, you'll also have to update
# /etc/dnsmasq.d/lxc and restart the system wide dnsmasq daemon.
LXC_BRIDGE="br0"
LXC_ADDR="192.0.2.1"
LXC_NETMASK="255.255.255.0"
LXC_NETWORK="192.0.2.0/24"
LXC_DHCP_RANGE="192.0.2.250,192.0.2.251"
# LXC_DHCP_MAX="253"
# Uncomment the next line if you'd like to use a conf-file for the br0
# dnsmasq.  For instance, you can use 'dhcp-host=mail1,10.0.3.100' to have
# container 'mail1' always get ip address 10.0.3.100.
#LXC_DHCP_CONFILE=/etc/lxc/dnsmasq.conf

# Uncomment the next line if you want br0's dnsmasq to resolve the .lxc
# domain.  You can then add "server=/lxc/10.0.3.1' (or your actual $LXC_ADDR)
# to your system dnsmasq configuration file (normally /etc/dnsmasq.conf,
# or /etc/NetworkManager/dnsmasq.d/lxc.conf on systems that use NetworkManager).
# Once these changes are made, restart the lxc-net and network-manager services.
# 'container1.lxc' will then resolve on your host.
#LXC_DOMAIN="lxc"
EOF
	for conf in "${DNSMASQ_CONF}" "${LXC_CONF}" "${LXC_DEFAULT}"
	do
            if [ -f "${conf}" ]; then
            sed -i -e 's|lxcbr0|br0|g' "${conf}"
            fi
	done
	no_lxc_running && service lxc-net start 2>/dev/null || true
        if [ -d /sys/kernel/security/apparmor ]
        then
            apparmor_parser -r -W -T /etc/apparmor.d/lxc-containers
        fi
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 1
    ;;
esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0
