#
#  Copyright (c) 2005 Canonical LTD
#
#  Author: Matt Zimmerman <mdz@canonical.com>
#
#  2006, Oliver Grawert <ogra@canonical.com>
#        Vagrant Cascadian <vagrant@freegeek.org>
#  2007, Scott Balneaves <sbalneav@ltsp.org>
#        Oliver Grawert <ogra@canonical.com>
#  2008, Vagrant Cascadian <vagrant@freegeek.org>
#        Oliver Grawert <ogra@canonical.com>
#        Warren Togami <wtogami@redhat.com>
#        Gideon Romm <ltsp@symbio-technologies.com>
#
#  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 of the
#  License, 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
#  along with this program.  If not, you can find it on the World Wide
#  Web at http://www.gnu.org/copyleft/gpl.html, or write to the Free
#  Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
#  MA 02110-1301, USA.
#

#
# ltsp_config: This should be sourced by most scripts within an ltsp
# environment.  Ensures that LTSP5 defaults are set up
#
#

# Source ltsp-common-functions if we have not already (needed in some functions)
# We set PATH to empty string here (in the subshell), because shell will look
# for boolean_is_true in PATH if it doesn't find it as a built-in function
# That search is a waste of time and so, we avoid it by not giving a PATH.
#
# Also, let's call this at the very beginning to make absolutely sure that 
# anything that calls ltsp_config will have ltsp-common-functions sourced
(PATH="" boolean_is_true True 2>/dev/null) || . /usr/share/ltsp/ltsp-common-functions || true

# Don't source this file if it has been sourced already
if [ "$LTSP_CONFIG" = "True" ]; then 
    return 0
fi

# Read in lts.conf entries.
set -f
if [ -f /etc/lts.conf ]; then
    eval $(getltscfg -a) || true
fi
if [ -f /var/cache/getltscfg-cluster/lts.conf ]; then
    eval $(getltscfg -a -c /var/cache/getltscfg-cluster/lts.conf) || true
fi
if [ -f /var/cache/ltsp/ltsp_config ]; then
    . /var/cache/ltsp/ltsp_config
fi

if [ -d /usr/share/ltsp/ltsp_config.d ]; then
    for script in $(run_parts_list /usr/share/ltsp/ltsp_config.d) ; do
        . $script        
    done
fi
set +f

# set compatbility for ltsp 4.x variables
export X_VIDEO_RAM=${X_VIDEO_RAM:-"$X_VIDEORAM"}
export LOCALDEV=${LOCALDEV:-"$LOCAL_STORAGE"}
export X_CONF=${X_CONF:-"$XF86CONFIG_FILE"}

# work around common typo with horizontal sync variables
export X_HORZSYNC=${X_HORZSYNC:-"$X_HORIZSYNC"}

# Ensure a default for SERVER
if [ -z "$SERVER" ] || [ "$SERVER" = "auto" ]; then
    if [ -n "$NBD_SERVER" ]; then
        export SERVER=$NBD_SERVER
    elif [ -n "$NFS_SERVER" ]; then
        export SERVER=$NFS_SERVER
    fi
fi
if [ -z "$SERVER" ] || [ "$SERVER" = "auto" ]; then
    test -f /proc/mounts && while read DEVICE MOUNTPOINT FSTYPE OPTIONS PASS1 PASS2; do
        case $DEVICE in
            /dev/nbd[0-9]*)
                if [ "$MOUNTPOINT" = "/rofs" ] || [ "$MOUNTPOINT" = "/" ]; then
                    # check for NBD devices
                    pid=$(nbd-client -c $DEVICE)
                    server=$(cut -d '' -f 2 /proc/$pid/cmdline)
                    port=$(cut -d '' -f 3 /proc/$pid/cmdline)

                    # Check for nbd-proxy
                    if [ "$server" = "127.0.0.1" ]; then
                        server=$(ps --no-headers -o cmd x | grep "^nbd-proxy [[:graph:]]* [[:graph:]]* ${port}" | cut -d ' ' -f2)
                    fi
                fi
                # Save the result so we don't go through this again
                if [ ! -f /var/cache/ltsp/ltsp_config ]; then
                    if [ ! -d /var/cache/ltsp ]; then
                        mkdir -p /var/cache/ltsp || true
                    fi
                    if [ -d /var/cache/ltsp/ ]; then
                        echo "NBD_SERVER=${server}" > /var/cache/ltsp/ltsp_config || true
                        echo "NBD_ROOT_PORT=${port}" >> /var/cache/ltsp/ltsp_config || true
                    fi
                fi
                ;;
            *)
                if [ "$FSTYPE" = "nfs" ] && [ "$MOUNTPOINT" = "/" ]; then
                    # NFS root
                    server=${DEVICE%%:*}
                    NFS_SERVER=${server} > /var/cache/ltsp/ltsp_config || true
                fi
                ;;
        esac
        [ -n "$server" ] && break
    done < /proc/mounts

    SERVER="${server:-192.168.0.254}"
    export SERVER
fi

# Ensure a default screen if X is present
unset IS_SCREEN
for i in 01 02 03 04 05 06 07 08 09 10 11 12; do
    eval IS_SCREEN=\${SCREEN_${i}}
    [ -n "${IS_SCREEN}" ] && break
done
if [ -z "${IS_SCREEN}" ]; then
    if [ -f /etc/X11/default-display-manager ]; then
        default_display_manager="$(cat /etc/X11/default-display-manager)"
        if [ "$default_display_manager" = "/usr/sbin/ldm" ]; then
            true
        elif [ -x "$default_display_manager" ]; then
            display_manager_configured=true
        elif [ "$default_display_manager" = "/usr/bin/gdm" ] && \
             [ -x /usr/sbin/gdm ]; then
            # some versions of gdm set default-display-manager to /usr/bin/gdm,
            # even if there is no /usr/bin/gdm, but start /usr/sbin/gdm.
            display_manager_configured=true
        fi
    fi
    if [ "$display_manager_configured" = "true" ]; then
        # Do nothing, these daemons (xdm, gdm, kdm, wdm, etc.) should configure themselves.
        true
    elif [ -x /usr/sbin/ldm ]; then
        export SCREEN_07=ldm
    elif [ -x /usr/bin/X ]; then
        export SCREEN_07=xdmcp
    fi
fi

if [ -z "$LTSP_FATCLIENT" ] && [ -f /etc/ltsp_fat_chroot ] && [ -f /proc/meminfo ]; then
    # Don't set LTSP_FATCLIENT for low-RAM clients booting from a fat chroot
    memtotal=$(sed -n 's/MemTotal: *\([0-9]*\) .*/\1/p' /proc/meminfo)
    FAT_RAM_THRESHOLD=${FAT_RAM_THRESHOLD:-300}
    if [ "$memtotal" -ge $((1024*$FAT_RAM_THRESHOLD)) ]; then
        export LTSP_FATCLIENT=True
    fi
fi

# set defaults for sound support
if boolean_is_true "$LTSP_FATCLIENT"; then
    export SOUND=False
elif [ -z "$SOUND_DAEMON" ]; then
    if [ -x /usr/bin/pulseaudio ]; then
        export SOUND_DAEMON=pulse
    elif [ -x /usr/bin/esd ]; then
        export SOUND_DAEMON=esd
    elif [ -x /usr/bin/nasd ]; then
        export SOUND_DAEMON=nasd
    fi
    if [ -z "$SOUND" ] && [ -n "$SOUND_DAEMON" ]; then
        export SOUND=True
    fi
fi

# set defaults for local device support
if [ -z "$LOCALDEV" ] && [ -x /usr/bin/ltspfsd ]; then
    export LOCALDEV=True
fi

# Fat clients don't use ltspfsd. If an admin needs to prevent users from
# accessing local devices, he should do it with pklocalauthority.
if boolean_is_true "$LTSP_FATCLIENT"; then
    unset LOCALDEV
fi

# enable encrypted swap when cryptsetup is installed
if [ -z "$ENCRYPT_SWAP" ] && [ -x /sbin/cryptsetup ]; then
    export ENCRYPT_SWAP=True
fi

# set defaults for local apps support
if [ -z "$LOCAL_APPS" ] && ([ -x /usr/bin/sshfs ] || [ -n "$NFS_HOME" ]); then
    export LOCAL_APPS=True
fi

# set to determine if ltsp_config has already been sourced
export LTSP_CONFIG=${LTSP_CONFIG:-"True"}
