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

# Read in lts.conf entries.
if [ -f /etc/lts.conf ]; then
    eval $(getltscfg -a) || true
fi

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

# Ensure a default for SERVER
if [ -z "$SERVER" -o "$SERVER" = "auto" ]; then
    # check for NBD devices
    NBD_DEVICES=$(awk '/^\/dev\/nbd.* \/ /||/^\/dev\/nbd.* \/rofs /{print $1}' /proc/mounts)
    if [ -n "$NBD_DEVICES" ]; then
        for DEVICE in $NBD_DEVICES ; do
                server=$(pgrep -f -l $DEVICE | awk '{print $3}')
                test -n "$server" && break
        done
    else
        # check for NFS root
        server=$(sed -ne '/ltsp.*nfs/ { s/^\([^:]*\):.*$/\1/; p; q }' /proc/mounts)
    fi

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

# Ensure a default screen if X is present
if ! env | grep -q '^SCREEN_'; then
    if [ -f /etc/X11/default-display-manager ]; then
        default_display_manager="$(cat /etc/X11/default-display-manager)"
        if [ -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

# set defaults for sound support
if [ -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

# 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 ]; then
    export LOCAL_APPS=True
fi

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