# This file is sourced by Xsession(5), not executed.

# Steal X session for the deejayd user if that is configured to do so

if [ -r /etc/default/deejayd ]; then
    . /etc/default/deejayd
    if [ "$DEEJAYD_XSERVER_METHOD" = "reuse" ]; then
        # If we have an auth cookie matching the Deejayd configured DISPLAY,
        # add it to the Deejayd auth cookie file. Also cleanup old auth cookies
        # matching the same display.
        DEEJAYD_DISPLAYNAME=${DEEJAYD_DISPLAYNAME:=:0.0}
        if [ ! -z "$XAUTHORITY" ] && [ -r "$XAUTHORITY" ]; then
            authcookie=`xauth list "$DEEJAYD_DISPLAYNAME" 2> /dev/null\
            | sed -n "s/.*$DEEJAYD_DISPLAYNAME[[:space:]*].*[[:space:]*]//p"` 2>/dev/null;
            if [ -r "$DEEJAYD_XAUTHORITY" ]; then
                # To change the $DEEJAYD_XAUTHORITY file, we must use a
                # temporary file because xauth uses one of its own in the
                # same directory, and we do not have write permission in this
                # directory.
                tmpauthfile=`mktemp -t deejayd-reuse.XXXXXX`
                cp $DEEJAYD_XAUTHORITY $tmpauthfile
                xauth -f $tmpauthfile remove $DEEJAYD_DISPLAYNAME
                if [ "z${authcookie}" != "z" ]; then
                    xauth -f $tmpauthfile << EOF
add $DEEJAYD_DISPLAYNAME . $authcookie
EOF
                fi
                cp $tmpauthfile $DEEJAYD_XAUTHORITY
                rm $tmpauthfile
            fi
        fi
    fi
fi

# vim:set ai et sts=2 sw=2 tw=80:
