#!/bin/ksh

#
# This file is a part of the NsCDE - Not so Common Desktop Environment
# Author: Hegel3DReloaded
# Licence: GPLv3
#

# Main starting wrapper

# Basic sanity check.
if [ -z $HOME ]; then
   echo "Error: HOME not set." >&2
   exit 2
else
   export NSCDE_VERSION="1.0rc18"
fi

# Either print version and exit, or construct main options for and output
if [ "$1" == "-V" ]; then
   echo "NsCDE Version $NSCDE_VERSION"
   exit 0
else
   export FVWM_OPTS="$@"
   exec 2>&1
   # exec 1>> "$HOME"/.xsession-errors
fi

# Find out which DPI we are using.
DpiInfo=$(LC_ALL=C xdpyinfo | egrep 'resolution:.*dots per inch' | awk '{ print $2 }')
echo "${DpiInfo}" | egrep -q "^[[:digit:]]+x[[:digit:]]+$"
if (($? == 0)); then
   export DPI="${DpiInfo##*x}"
else
   echo "Warning: DPI \"$DPI\" is not recognized as X DPI specification. Setting DPI to 96"
   export DPI=96
fi

# Fuzzy decisions for font handling
if (($DPI <= 85)); then
   NSCDE_DPI=75
elif (($DPI > 85 && $DPI < 108)); then
   NSCDE_DPI=96
elif (($DPI >= 108 && $DPI < 132)); then
   NSCDE_DPI=120
elif (($DPI >= 132)); then
   NSCDE_DPI=144
fi

# Set main NSCDE and FVWM variables. Most of the things later
# depends on this core variables.
export FVWM_USERDIR="${HOME}/.NsCDE"
export NSCDE_ROOT=/opt/NsCDE
export FVWM_DATADIR="${NSCDE_ROOT}/config"

echo "$PATH" | egrep -q "/opt\/NsCDE\/bin"
if (($? != 0)); then
   export PATH="${PATH}:${NSCDE_ROOT}"
fi

# Find fvwm(1) binary. This is a critical point.
# We cannot continue without main workforce.
FVWM_BIN=$(whence -p fvwm)
if (($? != 0)); then
   echo "Error: cannot detect fvwm in $PATH"
   exit 3
fi

# Handle our X defaults if it exists.
if [ -r "${FVWM_USERDIR}/Xdefaults" ]; then
   cd $HOME
   whence -qp xrdb cpp
   if (($? == 0)); then
      xrdb -remove && xrdb -cpp /usr/bin/cpp < ${FVWM_USERDIR}/Xdefaults
   else
      echo "Warning: cannot find xrdb or cpp in ${PATH}, ${FVWM_USERDIR}/Xdefaults will not be processed"
   fi
else
   echo "Warning: cannot read ${FVWM_USERDIR}/Xdefaults file"
fi

# Handle our app-defaults dir if it exists.
if [ -d "${FVWM_USERDIR}/app-defaults" ]; then
   export XAPPLRESDIR="${FVWM_USERDIR}/app-defaults"
else
   echo "Warning: cannot find ${FVWM_USERDIR}/app-defaults directory"
fi

# Set xset(1), setxkbmap(1), xgamma(1), xmodmap(1) and such things from
# Xset.conf if it exists.
if [ -r "${FVWM_USERDIR}/Xset.conf" ]; then
   ksh "${FVWM_USERDIR}/Xset.conf"
else
   echo "Notice: ${FVWM_USERDIR}/Xset.conf does not exist"
fi

# Early user customizations if needed can be set here.
if [ -x "${FVWM_USERDIR}/libexec/pre_start.sh" ]; then
   . "${FVWM_USERDIR}/libexec/pre_start.sh"
fi

# Check if there is a XOverrideFontCursor.so for LD_PRELOAD and if LD_PRELOAD is already in
# use so we can append to the variable, and be careful to use colon as library object separator
if [ -x "$NSCDE_ROOT/lib/XOverrideFontCursor.so" ]; then
   if [ "x$LD_PRELOAD" != "x" ]; then
      echo "$LD_PRELOAD" | egrep -q "XOverrideFontCursor\.so"
      if (($? != 0)); then
         ld_preload_wc=$(echo "$LD_PRELOAD" | wc -c)
         ld_preload_lastchar=$(($ld_preload_wc - 1))
         ld_preload_last_letter=$(echo "$LD_PRELOAD" | cut -b $ld_preload_lastchar)
         if [ "$ld_preload_lastletter" == ":" ]; then
            export LD_PRELOAD="$LD_PRELOAD:$NSCDE_ROOT/lib/XOverrideFontCursor.so"
         else
            echo "$LD_PRELOAD" | egrep -q ' '
            if (($? == 0)); then
               new_ld_preload=$(echo "$LD_PRELOAD" | tr ' ' ':')
               export LD_PRELOAD="$new_ld_preload:$NSCDE_ROOT/lib/XOverrideFontCursor.so"
            else
               export LD_PRELOAD="$LD_PRELOAD:$NSCDE_ROOT/lib/XOverrideFontCursor.so"
            fi
         fi
      else
         echo "Notice: $NSCDE_ROOT/lib/XOverrideFontCursor.so is already ld-preloaded, skipping"
      fi
   else
      export LD_PRELOAD="$NSCDE_ROOT/lib/XOverrideFontCursor.so"
   fi
fi

# Now exec fvwm. Forking NsCDE-Main.conf should be a rare case,
# but we never know. From Main everything else is read.
if [ -r ${FVWM_USERDIR}/NsCDE-Main.conf ]; then
   exec $FVWM_BIN -f ${FVWM_USERDIR}/NsCDE-Main.conf $FVWM_OPTS
else
   exec $FVWM_BIN -f ${NSCDE_ROOT}/config/NsCDE-Main.conf $FVWM_OPTS
fi

# Do not be picky if there is some failure left.
# Do the best effort and exit always with 0.
exit 0

