#!/bin/sh
# remove_fstab_entry
# place in /lib/udev

DEVNAME=$1
FSTAB=/var/run/ltspfs_fstab

remove_dev()
{
    DIR=$1

    umount -l ${DIR}
    rmdir ${DIR}
    for LDM_SOCKET in /var/run/ldm_socket_*; do
        if [ -S ${LDM_SOCKET} ]; then
            SERVER=${LDM_SOCKET##*_}
            SSH_OPTS="-X"
            # see if we have a command with DISPLAY matching our socket
            IS_DIRECTX=$(pgrep -f -l ${LDM_SOCKET}.*DISPLAY)

            unset DISPLAY_INFO
            # get the DISPLAY info for ltspfsmounter
            if [ -n "${IS_DIRECTX}" ];then
                unset SSH_OPTS
                for line in $IS_DIRECTX ; do
                    case $line in
                        DISPLAY*)
                            DISPLAY_INFO="${line}"
                        ;;
                    esac
                done
            fi

            /usr/bin/ssh $SSH_OPTS -S ${LDM_SOCKET} ${SERVER} \
                "$DISPLAY_INFO /usr/sbin/ltspfsmounter ${MOUNTPOINT} remove"
        fi
    done
    sed -i -e "\@ ${DIR} @d" ${FSTAB}
    exit
}

while read DEV MOUNTPOINT TYPE OPTIONS DUMP PASS; do
    if [ "${DEV}" = "/dev/${DEVNAME}" ]; then
        remove_dev ${MOUNTPOINT}
    fi
done < ${FSTAB}
