#!/bin/sh

msg() {
	echo "nfsroot-hooks/gosa: $1"
}

# Make sysconfig a real directory
if [ ! -d "${NFSROOT}"/etc/sysconfig ]; then
	msg "creating sysconfig directory"
	mkdir -p "${NFSROOT}"/etc/sysconfig
fi

# Copy some templates
for file in /etc/initramfs-tools/initramfs.conf /etc/initramfs-tools/update-initramfs.conf /etc/usplash.conf; do
	name=${file##*/}
	msg "copying $name"
	[ -f "/etc/fai/nfsroot-templates/$name" ] && cp /etc/fai/nfsroot-templates/$name "${NFSROOT}"$file
done

# Copy make-fai-nfsroot.conf to allow debootstrapping later on
cp /etc/fai/make-fai-nfsroot.conf "${NFSROOT}"/etc/fai

#creating pid and socket directory for gosa-si
msg "Creating /var/run/gosa-si directory"
mkdir -p "${NFSROOT}"/var/run/gosa-si

msg "checking if gosa-si server needs to be specified"
SERVER=""
PARENT=""
if [ -f /etc/gosa-si/server.conf ]; then
	PARENT=$(sed -n '/^\[server\]/,/^$/{s/^ip = \(.*\)$/\1/p}' /etc/gosa-si/server.conf)
	if [ "$PARENT" = "" ]; then
		SERVER="ip = $(hostname -f)"
	fi

	msg "extracting parents gosa-si credentials"
	CRED=$(sed -n '/^\[ClientPackages\]/,/^$/{s/^key = \(.*\)$/\1/p}' /etc/gosa-si/server.conf)

	if [ -f /etc/gosa-si/client.conf.in ]; then
		msg "adapting gosa-si configuration"
		sed "s/@@SERVER@@/$SERVER/g;s/^key.*$/key = $CRED/g" /etc/gosa-si/client.conf.in > "${NFSROOT}"/etc/gosa-si/client.conf
	fi

	# Fix logging path of NFSrooted client to be /tmp/fai/fai.log...
	msg "fixing fai-logpath to point to nfsroot location"
	sed -i 's%^fai-logpath = .*$%fai-logpath = /tmp/fai/fai.log%g' "${NFSROOT}"/etc/gosa-si/client.conf
	chmod go-rwx "${NFSROOT}"/etc/gosa-si/client.conf

	# Remove ldap.conf
	msg "removing ldap configuration"
	rm "${NFSROOT}"/etc/ldap/ldap.conf
else
	if [ -f /etc/gosa-si/client.conf ]; then
		msg "Fall back to existing gosa-si client configuration"
		cp /etc/gosa-si/client.conf "${NFSROOT}"/etc/gosa-si

		# Fix logging path of NFSrooted client to be /tmp/fai/fai.log...
		msg "fixing fai-logpath to point to nfsroot location"
		sed -i 's%^fai-logpath = .*$%fai-logpath = /tmp/fai/fai.log%g' "${NFSROOT}"/etc/gosa-si/client.conf
		chmod go-rwx "${NFSROOT}"/etc/gosa-si/client.conf
	else
		msg "WARNING: there is no gosa-si support in the NFSROOT - installations will fail!"

		# Copy over original ldap configuration to make things work
		[ -f /etc/ldap/ldap.conf ] && cp /etc/ldap/ldap.conf "$NFSROOT"/etc/ldap
	fi
fi


# Set locale if needed
if [ -n "$NFSROOT_LOCALE" ]; then
  echo "set locales/default_environment_locale $NFSROOT_LOCALE" | chroot $NFSROOT debconf-communicate
  echo "set locales/locales_to_be_generated $NFSROOT_LOCALE UTF-8" | chroot $NFSROOT debconf-communicate
  chroot $NFSROOT dpkg-reconfigure -fnoninteractive locales
fi

# Get newest kernel installed inside the nfsroot
version=$(echo $NFSROOT/boot/vmlinuz-* | sort -n | head -n1 | cut -d- -f2-)

# Copy as default kernel
if [ -f "$NFSROOT/boot/vmlinuz-$version" ]; then
	msg "installing vmlinuz-install"
        cp "$NFSROOT/boot/vmlinuz-$version" "$TFTPROOT/vmlinuz-install"
else
        msg "ERROR: cannot find kernel to use as vmlinuz-install!!!"
fi

# Copy initrd if present
if [ -f "$NFSROOT/boot/initrd.img-$version" ]; then
	msg "installing initrd.img-install"
	cp "$NFSROOT/boot/initrd.img-$version" "$TFTPROOT/initrd.img-install"
fi

