#!/bin/bash

. ${TOOLKIT}-functions.sh

msgfile="$RUNDIR/msg.tmp"

KUWATCHER_REMOVEFILES=

# /etc/fstab modified?
#
$SOMETHING_CHANGED && {

    cat $msgfile

    in_chroot || {

	# swap
	#
	swap=`jtconf alix.swap_device 2>/dev/null` && {
		# disable if already running
		for dev in `swapon -s | sed -e '1d' -e 's/ .*//'`
		do
			[ $dev = $swap ] && {
				swapoff $dev || exit $?
				break
			}
		done

		# (re)create and (re)activate if device present
		# if not maybe that we are running on chrooted filesystem
		[ -e $swap ] && {
			label=`jtconf alix.swap_label`
			echo "    creating swap on $swap (LABEL=$label) ... "
			mkswap -L $label $swap >$RUNDIR/tmp.err 2>&1 || {
				status=$?
				cat $RUNDIR/tmp.err
				exit $status
			}
			echo "    activating swap ... "
			swapon $swap || exit $?
		}
	}


	# must mount datadir?
	#
	datalabel=`jtconf alix.data_label 2>/dev/null` && {
		[ -e /dev/disk/by-label/CFDATA ] && {
			datadir=`jtconf path.datadir`	|| exit_missing_define path.datadir
			mountpoint $datadir || {
				mount LABEL=CFDATA || exit $?
			}
		}
	}

    } # in_chroot

} # $SOMETHING_CHANGED



# grub configuration
#
[ -d /boot/grub ] || {
	mkdir /boot/grub || exit $?
	case $DISTRIB_FULL_ID in
   	  Ubuntu-9.*)
		cp /usr/lib/grub/i386-pc/* /boot/grub	|| exit $?
		;;
	esac
}

if [ -e /boot/grub/device.map ]
then
	# running system
	echo "(hd0) /dev/hda"	>/boot/grub/device.map
else
	if [ $DISTRIB_FULL_ID == "Ubuntu-9.04" ]
	then
		# fresh installed (on chrooted jail)
		echo "(hd0) /dev/YOUR_CF_DEVICE"	>/boot/grub/device.map

		# attenzione: il file device.map deve essere configurato a mano
		# perche` dipende da dove e` stata montata la compact flash
		#
		putwarning "GRUB install" "
		  manually modify the file /boot/grub/device.map where you must
		  put the actual Compact Flash device you are using, and then
		  you can launch the 'update-grub' command. I repeat: you must
		  modify the file and the launch the 'update-grub' command, or
		  your CF will not be bootable. Remember to change the file
		  again when the CF will be mounted on the target appliance, if
		  you want to update the kernel and/or the bootloader.
		"
	else
		echo "(hd0) /dev/hda"			>/boot/grub/device.map
	fi

fi

# grub options (only server for alix, at the moment)
#
export temp_grub_opts=`jtconf grub.options_srv 2>/dev/null`
echo "  using server grub options: $temp_grub_opts"

case $DISTRIB_FULL_ID in
   Ubuntu-9.04)
	if getconfirm alix.vga_console
	then
		installfile menu.lst-vga	/boot/grub/menu.lst	root:root 644 || exit $?
		uninstallfiles /etc/event.d/ttyS0 /etc/init/ttyS0
	else
		installfile menu.lst-serial	/boot/grub/menu.lst	root:root 644 || exit $?
	   	installfile init/ttyS0		/etc/event.d/		root:root 644 || exit $?
	fi
	;;
    Ubuntu-10.04|Ubuntu-12.04)
	if getconfirm alix.vga_console
	then
		echo "ERROR: at the moment the alix.vga_console is not supported" >&2
		echo "	for release $DISTRIB_FULL_ID" >&2
		exit 1
	else
		installfile grub2.default	/etc/default/grub	root:root 644 || exit $?
		installfile upstart/ttyS0.conf	/etc/init/		root:root 644 || exit $?
	fi
	;;
    Devuan*)
	if getconfirm alix.vga_console
	then
		echo "ERROR: at the moment the alix.vga_console is not supported" >&2
		echo "	for release $DISTRIB_FULL_ID" >&2
		exit 1
	else
		installfile grub2.default	/etc/default/grub	root:root 644 || exit $?
		grep -q '^T0:' /etc/inittab || {
			echo "  adding getty on ttyS0 in /etc/inittab"
			echo -e "\n#[ku]" >>/etc/inittab
			echo "T0:23:respawn:/sbin/getty -L ttyS0 38400" >>/etc/inittab
			telinit q
		}
	fi
	;;
    *)
    	exit_err 1 "FIXME: console settings for distro $DISTRIB_FULL_ID"
	;;
esac


# FIX: restores hwclock programs (disabled by previuos releases of kusa)
#
progname="/sbin/hwclock"
[ -L $progname -a -f $progname.orig ] && {
	echo "  re-enable $progname"
	rm -f $progname
	mv $progname.orig $progname	|| exit $?
	if [ -f /etc/init.d/hwclock.sh ]
	then
		update-rc.d hwclock.sh defaults
	fi
	echo "0.0 0 0.0" >/etc/adjtime
	script=`ls /etc/cron.*/timesync 2>/dev/null | head -1`
	[ "$script" != "" ] && {
		sh $script
		modprobe rtc		# just for safety
		/sbin/hwclock --systohc
		restart_service ntp ntpd
	}
}
# not ubuntu standard, kernel does not set clock at boot, we need
# to enable it manually via normal /etc/init.d script
# we can't use add_init_script function because works on rc0.d
# instead of rc6.d on systems stop and doesn't follows 100-S rule
#
if [ -f /etc/init.d/hwclock.sh ]
then
	case $DISTRIB_FULL_ID in
   	  Ubuntu-9.04)
		rm -f /etc/rc*.d/[SK]*hwclock.sh
		(cd /etc/rc2.d ; ln -s ../init.d/hwclock.sh S00hwclock.sh)
		(cd /etc/rc0.d ; ln -s ../init.d/hwclock.sh K25hwclock.sh)
		;;
	  *)
	  	update-rc.d hwclock.sh defaults
		;;
	esac
else
	# clean mess make by kusa on systems > 9.04
	purgefiles /etc/init.d/[KS]??hwclock.sh
fi


# kernel updated?
#
# 2017.05.06 kanna
# - still needed? cln-kernels module should take care of this
#
##in_chroot || {
	##kernelversion=`jtconf machine.kernel` && {
		##[ -f /boot/initrd.img-$kernelversion ] || {
			##echo "  rebuilding initrd for kernel $kernelversion"
			##mkinitramfs -o /boot/initrd.img-$kernelversion $kernelversion || exit $?
		##}
	##}
##}


# logs persistence and saving
#
getconfirm alix_logs.tmpfs && {
	mirror_dir=$(jtconf alix_logs.mirror_dir 2>/dev/null)
	save_dir=$(jtconf alix_logs.save_dir 2>/dev/null)

	file="ku-logs-mirror"
	target="/etc/cron.d/$file"

	if [ "$mirror_dir" != "" ]
	then
		jtmkpath $mirror_dir
		installfile $file.cron $target root:root 400
	else
		[ -f $target ] && {
			echo "  disabling logs mirror"
			uninstallfiles $target
		}
	fi

	file="ku-logs-save"
	target="/etc/cron.d/$file"

	if [ "$save_dir" != "" ]
	then
		jtmkpath $save_dir
		installfile $file.cron $target root:root 400
	else
		[ -f $target ] && {
			echo "  disabling logs archive"
			uninstallfiles $target
		}
	fi

} # alix_logs.tmpfs=true


file="auto-alix-check-wan"
target="/etc/kuwatcher/conf.d/$file"

wan_ip=$(jtconf alix-leds.wan_ip 2>/dev/null) || :

if [ "X$wan_ip" != "X" ]
then
	installfile $file $target		root:root 400 || exit $?
else
	KUWATCHER_REMOVEFILES="$KUWATCHER_REMOVEFILES $target"
fi


# old stuffs cleanup
#
purgefiles /etc/kuwatcher/conf.d/auto-check-free-ram \
	/etc/kuwatcher/conf.d/auto-net-rdate \
	/etc/kuwatcher/kuwatcher-sync-time \
	/etc/kuwatcher/conf.d/check-internet /etc/kuwatcher/check-internet \
	/etc/kuwatcher/alix-check-wan \
	$KUWATCHER_REMOVEFILES


$SOMETHING_CHANGED && {
	restart_service kuwatcher
}

exit 0
