#!/bin/sh
#
# LightDM wrapper to run around X sessions.

echo "Running X session wrapper"

# Load profile
for file in "/etc/profile" "$HOME/.profile" "/usr/share/defaults/etc/xprofile" "/etc/xprofile" "$HOME/.xprofile"; do
    if [ -f "$file" ]; then
        echo "Loading profile from $file"
        . "$file"
    fi
done
unset file

# Load resources
if command -v xrdb >/dev/null 2>&1; then
    for file in "/etc/X11/Xresources" "$HOME/.Xresources"; do
        if [ -f "$file" ]; then
            echo "Loading resource: $file"
            xrdb -merge "$file"
        fi
    done
    unset file
fi

# Run all system xinitrc shell scripts in the vendordir
xinitvendordir="/usr/share/defaults/etc/X11/xinit/xinitrc.d"
if [ -d "$xinitvendordir" ]; then
    for script in $xinitvendordir/*; do
        if [ -x "$script" -a ! -d "$script" ]; then
            echo "Loading xinit script $script"
            . "$script"
        fi
    done
    unset script
fi

# Run all system xinitrc shell scripts
xinitdir="/etc/X11/xinit/xinitrc.d"
if [ -d "$xinitdir" ]; then
    for script in $xinitdir/*; do
        if [ -x "$script" -a ! -d "$script" ]; then
            echo "Loading xinit script $script"
            . "$script"
        fi
    done
    unset script
fi

echo "X session wrapper complete, running session $@"

exec $@
