#!/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

Setup_debconf ()
{
	if [ ! -e /var/lib/live/debconfig ]
	then
		mkdir -p /var/lib/live/debconfig
		chmod 0700 /var/lib/live/debconfig
	fi

	if [ ! -e /var/lib/live/debconfig/systemrc ]
	then

cat > /var/lib/live/debconfig/systemrc << EOF
Config: configdb
Templates: templatedb

Name: config
Driver: File
Mode: 644
Reject-Type: password
Filename: /var/lib/live/debconfig/config.dat

Name: passwords
Driver: File
Mode: 600
Backup: false
Required: false
Accept-Type: password
Filename: /var/lib/live/debconfig/passwords.dat

Name: configdb
Driver: Stack
Stack: config, passwords

Name: templatedb
Driver: File
Mode: 644
Filename: /var/lib/live/debconfig/templates.dat
EOF

		chmod 0600 /var/lib/live/debconfig/systemrc
	fi
}

Main ()
{
	if [ ! -x "$(which debconf-set-selections 2>/dev/null)" ]
	then
		echo "E: debconf-set-selections - command not found"
		echo "I: debconf can be obtained from:"
		echo "I:   http://ftp.debian.org/debian/pool/main/d/debconf/"
		echo "I: On Debian based systems, po4a can be installed with:"
		echo "I:   apt-get install debconf"
		exit 1
	fi

	Setup_debconf

	if [ -n "${1}" ]
	then
		# Preseed from file
		debconf-set-selections "${1}"
	else
		# Preseed from stdin
		while read _LINE
		do
			echo ${_LINE} | debconf-set-selections
		done < /dev/stdin
	fi
}

Main ${@}
