#!/bin/sh
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/etc/lt/dla/mnt/lib:/etc/lt/dla/mnt/lib64:/etc/lt/dla/mnt/usr/lib:/etc/lt/dla/mnt/usr/lib64:/etc/lt/dla/mnt/usr/lib/x86_64-linux-gnu

PREREQ=""
SERVICE="antenna"

prereqs()
{
    echo "$PREREQ"
}

case $1 in
    prereqs)
        prereqs
        exit 0
        ;;
esac
vgchange -ay
modprobe ltbd
rootfstype=""
rbd="${ROOT#block:}"
if [ -n "$rbd" ]; then
    case "$rbd" in
        LABEL=*)
            rbd="$(echo $rbd | sed 's,/,\\x2f,g')"
            rbd="/dev/disk/by-label/${rbd#LABEL=}"
            ;;
        UUID=*)
            rbd="/dev/disk/by-uuid/${ROOT#UUID=}"
            ;;
        PARTLABEL=*)
            rbd="/dev/disk/by-partlabel/${rbd#PARTLABEL=}"
            ;;
        PARTUUID=*)
            rbd="/dev/disk/by-partuuid/${rbd#PARTUUID=}"
            ;;
    esac

    echo "ltbd: root block device = $rbd" > /dev/kmsg

    # Device might not be ready
    if [ ! -b "$rbd" ]; then
        udevadm settle
    fi

    # Kernel cmdline might not specify rootfstype
    [ -z "$rootfstype" ] && rootfstype=$(blkid -s TYPE -o value $rbd)

    echo "ltbd: mounting $rbd as $rootfstype" > /dev/kmsg
    blockdev --setro $rbd
	if [ $rootfstype = ext4 ]; then
		mount -t $rootfstype -o ro,noload "$rbd" \/etc\/lt\/dla\/mnt
    elif [ "$rootfstype" = "btrfs" ]; then
        :
	else
		mount -t $rootfstype -o ro $rbd /etc/lt/dla/mnt > /dev/kmsg
	fi
    udevadm settle

    if [ -x /etc/lt/dla/mnt/usr/local/${SERVICE}/${SERVICE} ]; then
        /etc/lt/dla/mnt/usr/local/${SERVICE}/${SERVICE} --reload
    else
        echo "ltbd: error: cannot reload tracking data: missing /etc/lt/dla/mnt/usr/local/${SERVICE}/${SERVICE}" > /dev/kmsg
    fi

    umount -l /etc/lt/dla/mnt > /dev/kmsg
    umount -f /etc/lt/dla/mnt > /dev/kmsg
    blockdev --setrw $rbd
fi

exit 0
