#!/bin/sh

set -e

project_base="$(dirname $0)"

export WMAKER_USER_ROOT="$HOME/tmp/GNUstep"
gs_base="$WMAKER_USER_ROOT"
gs_defaults="$gs_base/Defaults"
gs_system_defaults="$project_base"/WindowMaker/Defaults
wm_base="$gs_base/Library/WindowMaker"
wm_backgrounds="$wm_base/Backgrounds"
wm_iconsets="$wm_base/IconSets"
wm_pixmaps="$wm_base/Pixmaps"
gs_icons="$gs_base/Library/Icons"
wm_style="$wm_base/Style"
wm_styles="$wm_base/Styles"
wm_themes="$wm_base/Themes"
WindowMaker="$project_base/src/.libs/wmaker"
convertfonts="$project_base/util/convertfonts"

make_dir_if_needed ()
{
    if [ ! -d "$1" ] ; then
        install -m 0755 -d "$1"
    fi
}

rename_dir_if_possible ()
{
    if [ ! -d "$2" ] ; then
       if [ -d "$1" ] ; then
          mv "$1" "$2"
       fi
    fi
}

copy_defaults_if_needed ()
{
    file="$gs_defaults/$1"
    system_file="$gs_system_defaults/$1"
    if [ ! -f "$file" ] ; then
        install -m 0644 "$system_file" "$file"
    fi
}

make_dir_if_needed     "$gs_defaults"
make_dir_if_needed     "$wm_base"
make_dir_if_needed     "$wm_backgrounds"
make_dir_if_needed     "$wm_iconsets"
make_dir_if_needed     "$wm_pixmaps"
make_dir_if_needed     "$gs_icons"
rename_dir_if_possible "$wm_style"        "$wm_styles"
make_dir_if_needed     "$wm_styles"
make_dir_if_needed     "$wm_themes"

export LD_LIBRARY_PATH="$project_base/WINGs/wings-rs/target/debug:$project_base/wutil-rs/target/debug:$project_base/wmaker-rs/target/debug:$project_base/wrlib/.libs:$project_base/WINGs/.libs:$LD_LIBRARY_PATH" 

copy_defaults_if_needed WindowMaker
copy_defaults_if_needed WMRootMenu
copy_defaults_if_needed WMState
#copy_defaults_if_needed WMWindowAttributes

if [ -x $convertfonts -a ! -e "$wm_base/.fonts_converted" ] ; then
# --keep-xlfd is used in order to preserve the original information
    $convertfonts --keep-xlfd "$gs_defaults/WindowMaker"
    if [ -f "$gs_defaults/WMGLOBAL" ] ; then
        $convertfonts --keep-xlfd "$gs_defaults/WMGLOBAL"
    fi
    find "$wm_styles" -mindepth 1 -maxdepth 1 -type f -print0 |
    xargs -0 -r -n 1 $convertfonts --keep-xlfd
    touch "$wm_base/.fonts_converted"
fi

if [ -n "$1" -a -x "$WindowMaker$1" ] ; then
    WindowMaker="$WindowMaker$1"
    shift
fi

for i in $(seq 5 10) ; do
    if [ "x$DISPLAY" != "x:$i" ] ; then
        xephyr_display=":$i"
        break
    fi
done
echo "Running Xephyr on display $xephyr_display"

Xephyr -screen 640x480 "$xephyr_display" &
xephyr_pid=$!
DISPLAY="$xephyr_display" gdb \
           --directory "$project_base" \
           --directory "$HOME/src/libX11-1.5.0/build/src" \
           --quiet \
           --fullname \
           --args "$WindowMaker" -display "$xephyr_display" --for-real "$@"
kill $xephyr_pid
