#!/bin/bash
if test "$1" = "configure"
then
	if ! id cephboot &>/dev/null
	then
		echo "Add user cephboot"
		useradd -c "Ceph-Boot commands" -d /home/cephboot \
			-M -s /bin/bash -U cephboot
		mkdir -p -m 0700 /home/cephboot/.ssh
		echo "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDG3ElZesh+xCc7laJsiAT5DcxsQrsns1n8BV0HYVQFGLGOC8jTKl78aHwaQpsukdlcD/onIhzJbZw5fClDrfkhwF85eiLvrdMWe8eKKwD7Rx1K744s2Qy2YQJ2T6uOLd3oBwg4zvhv1B3we2WcJoNxUobdOqePQfkbf2PkMsCzRj77lkD9pLGuP8IiapoWKbKMaLsMS/VASId6nXDG/5okHERlIAbxr+0FVCY5lUh65FEunh711ZMFsSIts5OTpU3GfDGnsKYAbYnP74uMrPwsPq3JFHzhY+jLOfJnfRPaAu2V+bvg2Xb/fO3qo37cuZ0iCdnPEiVFmeNiYQCea+NB AO-Smartcard-2048" > /home/cephboot/.ssh/authorized_keys
		chown -R cephboot: /home/cephboot/
	fi
	chown cephboot /etc/dhcp/dhcpd.conf
	if ! grep -q "cephboot ALL=NOPASSWD:/usr/bin/rbd" /etc/sudoers
	then
		echo "cephboot ALL=NOPASSWD:/usr/bin/rbd" >> /etc/sudoers
		echo "cephboot ALL=NOPASSWD:/sbin/tgpro-image-create" >> /etc/sudoers
		echo "cephboot ALL=NOPASSWD:/sbin/tgpro-image-copy" >> /etc/sudoers
		echo "cephboot ALL=NOPASSWD:/etc/init.d/isc-dhcp-server" >> /etc/sudoers
	fi
	if ! grep -q "check_cephversion" /etc/nagios/nrpe.cfg
	then
		echo 'command[check_cephversion]=/usr/bin/ceph --version' >>/etc/nagios/nrpe.cfg
		/etc/init.d/nagios-nrpe-server reload || true
	fi
	if ! test -e /srv/tftp/grubnetx64.efi
	then
		cp -a /usr/lib/grub/x86_64-efi/monolithic/grubnetx64.efi /srv/tftp/
	fi
	if ! test -e /srv/tftp/grub/x86_64-efi
	then
		mkdir -p /srv/tftp/grub/
		cp -a /usr/lib/grub/x86_64-efi /srv/tftp/grub/
	fi
	if test -e /boot/pcmemtestx64.efi -a ! -e /srv/tftp/pcmemtestx64.efi
	then
		cp -a /boot/pcmemtestx64.efi /srv/tftp/
	fi
	# set reboot marker
	if test -f /etc/ceph/ceph.conf -a -f /etc/ceph/ceph.client.admin.keyring
	then
		MONIP=$(grep "^mon_host = " /etc/ceph/ceph.conf 2>/dev/null|cut -d " " -f 3)
		KEY=$(grep "key = " "/etc/ceph/ceph.client.admin.keyring" | cut -d " " -f 3)
		if test -n "$MONIP" -a -n "$KEY"
		then
			if mount -t ceph "$MONIP:/" /mnt/ -o name=admin,secret="$KEY"
			then
				if test -d /mnt/homeuser
				then
					touch /mnt/homeuser/.rebootneeded || true
				fi
				umount /mnt
			fi
		fi
	fi
fi
exit 0
