#!/bin/sh

## live-debconfig(7) - System Configuration Components
## Copyright (C) 2006-2013 Daniel Baumann <mail@daniel-baumann.ch>
##
## This program comes with ABSOLUTELY NO WARRANTY; for details see COPYING.
## This is free software, and you are welcome to redistribute it
## under certain conditions; see COPYING for details.


set -e

DEBCONF_SYSTEMRC="/var/lib/live/debconfig/systemrc"
export DEBCONF_SYSTEMRC

. /usr/share/debconf/confmodule

Defaults ()
{
	if [ -z "${_NET_IPV4_IP_FORWARD}" ]
	then
		if [ -e /etc/sysctl.d/net.ipv4.ip_forward.conf ]
		then
			_NET_IPV4_IP_FORWARD="$(grep '^ *net.ipv4.ip_forward' /etc/sysctl.d/net.ipv4.ip_forward.conf | sed -e 's|.*=* ||')"
		fi

		_NET_IPV4_IP_FORWARD="${_NET_IPV4_IP_FORWARD:-0}"
	fi

	if [ -z "${_VM_SWAPPINESS}" ]
	then
		if [ -e /etc/sysctl.d/vm.swappiness.conf ]
		then
			_VM_SWAPPINESS="$(grep '^ *vm.swappiness' /etc/sysctl.d/vm.swappiness.conf | sed -e 's|.*=* ||')"
		fi

		_VM_SWAPPINESS="${_VM_SWAPPINESS:-60}"
	fi
}

db_get live-debconfig/procps/net.ipv4.ip_forward
_NET_IPV4_IP_FORWARD="${RET}" # string w/

db_get live-debconfig/procps/vm.swappiness
_VM_SWAPPINESS="${RET}" # string w/

Defaults

db_set live-debconfig/procps/net.ipv4.ip_forward "${_NET_IPV4_IP_FORWARD}"
db_fset live-debconfig/procps/net.ipv4.ip_forward seen false

db_settitle live-debconfig/title
db_input high live-debconfig/procps/net.ipv4.ip_forward || true
db_go

db_get live-debconfig/procps/net.ipv4.ip_forward
_NET_IPV4_IP_FORWARD="${RET}" # string w/

db_set live-debconfig/procps/vm.swappiness "${_VM_SWAPPINESS}"
db_fset live-debconfig/procps/vm.swappiness seen false

db_settitle live-debconfig/title
db_input high live-debconfig/procps/vm.swappiness || true
db_go

db_get live-debconfig/procps/vm.swappiness
_VM_SWAPPINESS="${RET}" # string w/

Defaults

db_stop

# translate debconf boolean into 'yes' and 'no'
# (this is better than using a select in debconf with Choices-C,
# otherwise all translators would need to translate 'yes' and 'no').
case "${_NET_IPV4_IP_FORWARD}" in
	true)
		_NET_IPV4_IP_FORWARD="1"
		;;

	false)
		_NET_IPV4_IP_FORWARD="0"
		;;
esac

# Set the net.ipv4.ip_forward parameters
if [ -e /etc/sysctl.d/net.ipv4.ip_forward.conf ]
then
	cp /etc/sysctl.d/net.ipv4.ip_forward.conf /etc/sysctl.d/net.ipv4.ip_forward.conf.tmp
else
	mkdir -p /etc/sysctl.d
	touch /etc/sysctl.d/net.ipv4.ip_forward.conf.tmp
fi

grep -Eq '^ *net.ipv4.ip_forward' /etc/sysctl.d/net.ipv4.ip_forward.conf.tmp || \
	echo "net.ipv4.ip_forward" >> /etc/sysctl.d/net.ipv4.ip_forward.conf.tmp

sed -i -e "s|^ *net.ipv4.ip_forward.*|net.ipv4.ip_forward = ${_NET_IPV4_IP_FORWARD}|" \
	/etc/sysctl.d/net.ipv4.ip_forward.conf.tmp

mv /etc/sysctl.d/net.ipv4.ip_forward.conf.tmp /etc/sysctl.d/net.ipv4.ip_forward.conf

# Set the vm.swappiness
if [ -e /etc/sysctl.d/vm.swappiness.conf ]
then
	cp /etc/sysctl.d/vm.swappiness.conf /etc/sysctl.d/vm.swappiness.conf.tmp
else
	mkdir -p /etc/sysctl.d
	touch /etc/sysctl.d/vm.swappiness.conf.tmp
fi

grep -Eq '^ *vm.swappiness' /etc/sysctl.d/vm.swappiness.conf.tmp || \
	echo "vm.swappiness" >> /etc/sysctl.d/vm.swappiness.conf.tmp

sed -i -e "s|^ *vm.swappiness.*|vm.swappiness = ${_VM_SWAPPINESS}|" \
	/etc/sysctl.d/vm.swappiness.conf.tmp

mv /etc/sysctl.d/vm.swappiness.conf.tmp /etc/sysctl.d/vm.swappiness.conf
