#! /bin/sh

PATH=/sbin:/bin:/usr/sbin:/usr/bin
export PATH

umask 022

ACTION=$1

for DIR in /etc/ldm/rc.d /usr/share/ldm/rc.d ${prefix}/lib/i386-linux-gnu/ldm/rc.d ; do
    if [ -d "$DIR" ]; then
        RCDIRS="$RCDIRS $DIR"
    fi
done

case "${ACTION}" in
    init)
        # From ldm.c: LDM has just started, X is running, the greeter isn't.
        SCRIPTS="I*"
        ;;
    pressh)
        # From ssh.c: a username/password was provided, no connection yet.
        SCRIPTS="P*"
        ;;
    start)
        # From ssh.c: an ssh connection was established.
        SCRIPTS="S*"
        ;;
    xsession)
        # From plugin.c: an xsession was started (maybe even rdp).
        SCRIPTS="X*"
        ;;
    stop)
        # From screen.d/ldm.c: the xsession has ended.
        SCRIPTS="K*"
        ;;
esac

export TEXTDOMAIN=ldmrc

. /usr/bin/gettext.sh

# source ltsp client functions
if [ -f "/usr/share/ltsp/ltsp-client-functions" ]; then
    . /usr/share/ltsp/ltsp-client-functions
# source older common functions for backwards compatibility
elif [ -f "/usr/share/ltsp/ltsp-common-functions" ]; then
    . /usr/share/ltsp/ltsp-common-functions
fi

for SCRIPT in $(find -L $RCDIRS -type f -name "$SCRIPTS" -printf '%f\n' |
    tr ' ' '\n' | egrep ^[0-9a-zA-Z_\-]*$ | sort -u)
do
    # only source the first instance of the script found
    for DIR in $RCDIRS ; do
        if [ -f "$DIR/$SCRIPT" ]; then
            . "$DIR/$SCRIPT"
            break
        fi
    done
done
