#!/sbin/openrc-run

depend()
{
	after dev
	before docker
}

start()
{
	ebegin "Configuring host block device"

	DEV="$(ls /dev/[sxv]da | head -1 | sed s@/dev/@@)"

	[ -z ${DEV} ] && exit 1

	DRIVE="/dev/${DEV}"

	if fdisk -l ${DRIVE} | grep -q "doesn't contain a valid partition table"
	then
		ERASE_DISKS="${DRIVE}" setup-disk -m data ${DRIVE}
	else
		SWAP=$(fdisk -l ${DRIVE} | grep 'Linux swap' | head -1 | awk '{print $1}')
		DATA=$(fdisk -l ${DRIVE} | grep 'Linux$' | head -1 | awk '{print $1}')
		mount ${DATA} /var && swapon ${SWAP} || ERASE_DISKS="${DRIVE}" setup-disk -m data ${DRIVE}
		# boot2docker compat, has /var and /tmp on partition
		[ -d /var/var/lib/boot2docker/ ] && mount --bind /var/var /var
	fi

	mount | grep -q '/dev/[sxv]da. on /var type'

	eend $? "Failed to mount block device"
}
