# -*- 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 environment variable.
# We may not want to force set this:
# FIXME:  Revisit this at some point.

CLIENT_ENV="$CLIENT_ENV LTSP_CLIENT=$LDMINFO_IPADDR"

# 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 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"
            ;;
        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

ssh -Y -t -S $LDM_SOCKET -l $LDM_USERNAME $LDM_SERVER "$CLIENT_ENV $MY_LANG $MY_DISP $MY_SOUND $LDM_XSESSION $LDM_SESSION < /dev/null > /dev/null $MY_LOCALDEV"

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