# -*- Shell-Script -*-

# reimplementation of ldm's x_session function in shell, so that it's
# environment can be modified by other rc.d scripts (for .dmrc
# processing and such)

# Set up the LTSP_CLIENT and LTSP_CLIENT_HOSTNAME environment variables.
# We may not want to force set this:
# FIXME:  Revisit this at some point.

CLIENT_ENV="$CLIENT_ENV LTSP_CLIENT=$LDMINFO_IPADDR LTSP_CLIENT_HOSTNAME=$(hostname)"

# Set our language, if a different one is picked.

if [ -n "$LDM_LANGUAGE" ]; then
    MY_LANG="LC_ALL=$LDM_LANGUAGE LANGUAGE=$LDM_LANGUAGE LANG=$LDM_LANGUAGE"
fi

# Set the DISPLAY env, if not running over encrypted ssh

if boolean_is_true "$LDM_DIRECTX"; then
    MY_DISP="DISPLAY=${LDMINFO_IPADDR}${DISPLAY}"
fi

# Handle scanner
if boolean_is_true "$SCANNER"; then
    MY_SCANNER="SANE_NET_HOSTS=$LDMINFO_IPADDR"
    # Allow the application server to connect to saned
    if [ -f /etc/sane.d/saned.conf ]; then
        echo $LDM_SERVER > /etc/sane.d/saned.conf
    fi
fi

# Handle sound

if boolean_is_true "$SOUND"; then
    if [ -z "$SOUND_DAEMON" ]; then
        SOUND_DAEMON=pulse
    fi
    case $SOUND_DAEMON in
        pulse)
            MY_SOUND="PULSE_SERVER=tcp:$LDMINFO_IPADDR:4713 ESPEAKER=$LDMINFO_IPADDR:16001 LTSP_ALSA_DEFAULT=pulse" 
            ;;
        esd)
            esd -nobeeps -public -tcp &
            ESD_PID=$!
            MY_SOUND="ESPEAKER=$LDMINFO_IPADDR:16001"
            ;;
        nasd)
            MY_SOUND="AUDIOSERVER=$LDMINFO_IPADDR:0"
            ;;
    esac
fi

# Handle localdev

if boolean_is_true "$LOCALDEV"; then
    MY_LOCALDEV="; /usr/sbin/ltspfsmounter all cleanup"
fi

if ! boolean_is_true "$LDM_NOMAD"; then
    if ! boolean_is_true "$LTSP_FATCLIENT"; then
        ssh -Y -t -S $LDM_SOCKET -l $LDM_USERNAME $LDM_SERVER "$CLIENT_ENV $MY_LANG $MY_DISP $MY_SCANNER $MY_SOUND $LDM_XSESSION $LDM_SESSION < /dev/null > /dev/null $MY_LOCALDEV"
    else
        ICEAUTHORITY="$(dirname $XAUTHORITY)/ICEauthority"
        # Using pam_ck_connector to make fat client sessions ConsoleKit-aware
        # FIXME: su doesn't read the current process environment, so we're using
        # /etc/security/pam_env.conf temporarily, which also affects non-X sessions
        if [ -x /usr/lib/ConsoleKit/ck-get-x11-display-device ] \
            && [ -f /etc/security/pam_env.conf ] \
            && CKCON_X11_DISPLAY_DEVICE=$(/usr/lib/ConsoleKit/ck-get-x11-display-device 2>/dev/null)
        then
            CKCON_X11_DISPLAY="$DISPLAY"
            sed -i '/^CKCON_X11_DISPLAY/d' /etc/security/pam_env.conf
            echo "CKCON_X11_DISPLAY DEFAULT=$CKCON_X11_DISPLAY" >> /etc/security/pam_env.conf
            echo "CKCON_X11_DISPLAY_DEVICE DEFAULT=$CKCON_X11_DISPLAY_DEVICE" >> /etc/security/pam_env.conf
        fi

        su - ${LDM_USERNAME} -c "$CLIENT_ENV $MY_LANG DISPLAY=$DISPLAY ICEAUTHORITY=$ICEAUTHORITY XAUTHORITY=$XAUTHORITY $LDM_XSESSION $LDM_SESSION"

        # Unmount any removable devices mounted by the user
        for d in $(mount | awk '/uhelper=udisks/ {print $1}; /uhelper=devkit/ {print $1}'); do
            umount "$d" &
        done
    fi
fi

if [ "$SOUND_DAEMON" = "esd" ]; then
    kill $ESD_PID
    wait $ESD_PID
fi
