#!/bin/sh
# Depend on the ldm package
#
# Copyright 2005, Canonical Ltd.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE.  See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License with your
# Debian GNU system, in /usr/share/common-licenses/GPL.  If not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.


#
# Load ltsp config defaults
#

. /usr/lib/ltsp/ltsp_config

#
# Compute tty and displaynum
#

TTY=$(tty)
TTYNUM=${TTY#/dev/tty}
DISPLAYNUM=$((${TTYNUM} - 1))

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

#
# limit ram percentage.  By default, behaviour is no limit.
# Use this to stop firefox crashing sites with pathalogically large images,
# etc.
#

X_RAMPERC=${X_RAMPERC:-100}

if [ ${X_RAMPERC} -lt 100 ]; then
    XMEM=0
    while read TYPE VALUE UNITS; do
        case ${TYPE} in
            MemFree:|SwapFree:)
                XMEM=$((${XMEM} + ${VALUE}))
                ;;
        esac
    done < /proc/meminfo
    XMEM=$((${XMEM} * ${X_RAMPERC} / 100))

    ulimit -m ${XMEM}
fi

while :; do
    #
    # Server scalability.  If there exists a /usr/lib/ltsp/get_hosts file, then
    # use it to populate the LDM_SERVER environment variable.
    #

    if [ -z "${LDM_SERVER}" ]; then
        if [ -x /usr/lib/ltsp/get_hosts ]; then
            LDM_SERVER=$(/usr/lib/ltsp/get_hosts)
        else
            LDM_SERVER=${SERVER}
        fi
    fi
    export LDM_SERVER

    #
    # Loop though each of the hosts, and get their ldminfo
    #

    if [ -n "${LDM_SERVER}" ]; then
        test ! -d /var/run/ldm && mkdir -p /var/run/ldm
        for SRV in $LDM_SERVER ; do
            nc -w 5 $SRV 9571 > /var/run/ldm/$SRV
        done
    fi

    if boolean_is_true "$LDM_DIRECTX" ; then
        mcookie=`dd if=/dev/urandom bs=16 count=1 2>/dev/null | hexdump -e \\"%08x\\"`
        if [ -z "$mcookie" ]; then
            echo "Couldn't create cookie"
            exit 1
        fi
        # FIXME: This needs to non-destructively operate  in parallel with
        # other X servers running on the same machine.
        # See /usr/bin/startx for example.
        export XAUTHORITY=/root/.Xauthority
        xauth -q -f $XAUTHORITY << EOF
add :${DISPLAYNUM} . $mcookie
EOF
        # Might not be necessary to do these...
        xauth -q -f $XAUTHORITY << EOF
add `hostname`:${DISPLAYNUM} . $mcookie
EOF
        xauth -q -f $XAUTHORITY << EOF
add 127.0.0.1/unix:${DISPLAYNUM} . $mcookie
EOF
    fi

    ldm vt${TTYNUM} :${DISPLAYNUM}

    # Clean up from some buggy video drivers that need two kills
    XPROC=$(pgrep -f ":${DISPLAYNUM}")
    while [ -n "${XPROC}" ]; do
	kill $XPROC
        sleep 1
	XPROC=$(pgrep -f ":${DISPLAYNUM}")
    done

    rm -f /root/.Xauthority
    rm -f /tmp/.X11-unix/X${DISPLAYNUM}
    rm -f /tmp/.X${DISPLAYNUM}-lock
done
