# Sourced with .
# Script to add LDM Xserver address to xauth

boolean_is_true(){
    case "$(echo $1 | tr 'A-Z' 'a-z')" in
       true|y|yes) return 0 ;;
       *) return 1 ;;
    esac
}

ldm_log=/var/log/ldm.log

LDMINFO_IPADDR=$(ip -o -f inet addr show | sed -e 's,.* \(.*\)/.*,\1,' | grep -v '^127')

if boolean_is_true "$LDM_DIRECTX"; then
    PROTOCOL=$(xauth -f $XAUTHORITY list | head -1 | awk '{print $2}')
    KEY=$(xauth -f $XAUTHORITY list | head -1| awk '{print $3}')

    echo $DISPLAY $LDMINFO_IPADDR $PROTOCOL $KEY >> $ldm_log
    ssh -S ${LDM_SOCKET} ${LDM_SERVER} \
        "xauth remove ${LDMINFO_IPADDR}${DISPLAY}" >> $ldm_log
    ssh -S ${LDM_SOCKET} ${LDM_SERVER} \
        "xauth add ${LDMINFO_IPADDR}${DISPLAY} ${PROTOCOL} ${KEY}" >> $ldm_log
fi
