#!/bin/bash

. ${TOOLKIT}-functions.sh

echo "  OMV custom module"

# ------------------------------------------------------------
# motd (from /etc/init* scrips)
# ------------------------------------------------------------
#
# saves file changed flag, motd does not count
#
save_changed=$SOMETHING_CHANGED

# clean old /etc/motd.tail if installed by kusa
[ -f /etc/motd.tail ] && {
	grep -q 'system updated .* kusa-reconf' /etc/motd.tail && rm -f /etc/motd.tail
}

# build temp definition used in motd template
# (ie: fancy formatted subclasses list)
#
(
	echo "[temp]"
	echo "  machine_desc " $(jtconf machine.desc 2>/dev/null || :)
	echo "  subclasses	^^BLOCK^^"
	jtconf sub_classes | fold -w 64 -s | \
		sed -e 's/^/	\\s         /'
	echo "^^BLOCK^^"

) >$MODLOCALDB

installfile motd /etc/motd root:root 444

# custom text can be added to /etc/motd.local or /etc/motd.tail (the latter
# on old Ubuntu systems)
#
[ -f /etc/motd.local ]	&& cat /etc/motd.local >> /etc/motd || :
[ -f /etc/motd.tail ]	&& cat /etc/motd.tail >> /etc/motd || :
 
SOMETHING_CHANGED=$save_changed



# ------------------------------------------------------------
# apt and dpkg cleanup
# ------------------------------------------------------------
#
echo -n "  purging apt cache ... "
apt-get purge >tmp.log 2>&1 || {
	echo -e "\nERROR running apt-get purge:\n"
	cat tmp.log
	exit 1
}
echo "ok"

pkgs=`dpkg -l | grep "^rc " | sed -e 's/^....//' -e 's/ .*//'`
[ "$pkgs" ] && {
	set `echo "$pkgs" | wc -l`
	echo -n "  purging $1 packages ... "
	dpkg --purge $pkgs >tmp.log 2>&1 || {
		echo -e "\nERROR running dpkg --purge:\n"
		cat tmp.log
		exit 1
	}
	echo "ok"
}

exit 0
