#!/usr/bin/ksh93

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

export TEXTDOMAINDIR="$NSCDE_ROOT/share/locale"
export TEXTDOMAIN="nscde-bootstrap"
export OS=$(uname -s)

# Handle gettext absence from the system
function echofunc
{
   shift
   echo "$@"
}

# Handle gettext absence from the system
whence -q gettext
if (($? != 0)); then
   alias gettext="echofunc"
fi

if [ -z $HOME ]; then
   gettext -s "Error: HOME is not set cannot continue."
   sleep 3
   exit 3
fi

if [ -z $NSCDE_ROOT ]; then
   gettext -s "Error: NSCDE_ROOT is not set cannot continue."
   sleep 3
   exit 4
fi

if [ ! -e "$NSCDE_ROOT" ]; then
   gettext -s "Error: NSCDE_ROOT does not exist. Cannot continue."
   sleep 3
   exit 5
fi

if [ -z $NSCDE_TOOLSDIR ]; then
   gettext -s "Error: NSCDE_ROOT is not set cannot continue."
   sleep 3
   exit 4
fi

if [ ! -e "$NSCDE_TOOLSDIR" ]; then
   gettext -s "Error: NSCDE_TOOLSDIR does not exist. Cannot continue."
   sleep 3
   exit 5
fi

if [ -z $FVWM_USERDIR ]; then
   gettext -s "Error: FVWM_USERDIR is not set cannot continue."
   sleep 3
   exit 6
fi

function setup_nscde
{
   mode="$1"

   already_exists=$(gettext -s "already exists")
   echo ""
   echo "#####################################################################"
   echo "#"
   if [ "$mode" == "initial" ]; then
      gettext -s "# NsCDE First Run Setup"
   fi
   if [ "$mode" == "reconfigure" ]; then
      gettext -s "# NsCDE Reconfigure Setup"
   fi
   echo "#"
   echo "#####################################################################"
   echo ""

   if [ -z $NSCDE_PALETTE ]; then
      NSCDE_PALETTE="Broica"
   fi

   if [ -z $NSCDE_PALETTE_NCOLORS ]; then
      NSCDE_PALETTE_NCOLORS=8
   fi

   gettext -s "Press RETURN to run setup"
   read RET

   # Xsettingsd daemon configuration
   if [ ! -e "${FVWM_USERDIR}/Xsettingsd.conf" ]; then
      cp "${NSCDE_DATADIR}/config_templates/Xsettingsd.conf" "${FVWM_USERDIR}/Xsettingsd.conf"
   else
      echo "${FVWM_USERDIR}/Xsettingsd.conf ${already_exists}."
   fi

   # Bare background before backer setup
   if [ ! -e "${FVWM_USERDIR}/.BGdefault" ]; then
      locale_Writing_default_background_pixel=$(gettext -s "Writing default background pixel")
      echo "$locale_Writing_default_background_pixel ${FVWM_USERDIR}/.BGdefault ..."
      cp ${NSCDE_DATADIR}/config_templates/BGdefault "${FVWM_USERDIR}/.BGdefault"
   else
      echo "${FVWM_USERDIR}/.BGdefault ${already_exists}."
   fi

   # Initial NsCDE.conf after InitFunction is run
   if [ ! -e "${FVWM_USERDIR}/NsCDE.conf" ]; then
      echo "$locale_Initializing_default ${FVWM_USERDIR}/NsCDE.conf ..."
      cp ${NSCDE_DATADIR}/fvwm/NsCDE.conf ${FVWM_USERDIR}/NsCDE.conf

      if [ "$OS" == "NetBSD" ]; then
         echo 'Test (EnvMatch FVWM_IS_FVWM3 1, EnvMatch NSCDE_OS NetBSD, x gtail) InfoStoreAdd xlogcmd "gtail -n 300 -f ~/.xsession-errors ~/.NsCDE/tmp/fvwm.log"' >> ${FVWM_USERDIR}/NsCDE.conf
      fi
   else
      echo "${FVWM_USERDIR}/NsCDE.conf ${already_exists}."
   fi

   # Initial Init.fvwmconf for user session customizations
   if [ ! -e "${FVWM_USERDIR}/Init.fvwmconf" ]; then
      echo "$locale_Initializing_default ${FVWM_USERDIR}/Init.fvwmconf ..."
      cp ${NSCDE_DATADIR}/fvwm/Init.fvwmconf ${FVWM_USERDIR}/Init.fvwmconf
   else
      echo "${FVWM_USERDIR}/Init.fvwmconf ${already_exists}."
   fi

   # Initial Functions.fvwmlocal, Menus.fvwmlocal, Keybindings.fvwmlocal, and
   # Style.fvwmlocal examples for user function customizations
   for localcf in Functions Menus Keybindings Style
   do
      if [ ! -e "${FVWM_USERDIR}/${localcf}.fvwmlocal" ]; then
         echo "$locale_Initializing_default ${FVWM_USERDIR}/${localcf}.fvwmlocal ..."
         cp ${NSCDE_DATADIR}/config_templates/fvwm/${localcf}.fvwmlocal ${FVWM_USERDIR}/${localcf}.fvwmlocal
      else
         echo "${FVWM_USERDIR}/${localcf}.fvwmlocal ${already_exists}."
      fi
   done

   # GeometryDB ini
   if [ ! -e "${FVWM_USERDIR}/GeoDB.ini" ]; then
      echo "$locale_Initializing_default ${FVWM_USERDIR}/GeoDB.ini ..."
      cp ${NSCDE_DATADIR}/config_templates/GeoDB.ini ${FVWM_USERDIR}/GeoDB.ini
   else
      echo "${FVWM_USERDIR}/GeoDB.ini ${already_exists}."
   fi

   # Misc paths
   locale_making_additional_subdirectories=$(gettext -s "Making additional subdirectories in")
   echo "$locale_making_additional_subdirectories ${FVWM_USERDIR} ..."
   mkdir -p ${FVWM_USERDIR}/{palettes,templates,fontsets,backdrops,photos}

   # Backup user dot files which were maybe existing before this setup
   backup_datestr=$(date +%d-%m-%Y_%H-%M-%S)
   locale_making_backing_up_dot_files=$(gettext -s "Backing up pre-NsCDE GTK and Qt config files as ")
   have_old_dot_file=0
   cd "$HOME"
   for dotfile in .gtkrc-2.0 .config/gtk-3.0/settings.ini .config/qt5ct/qt5ct.conf .icons/default/index.themea .config/Trolltech.conf
   do
      if [ -r "$dotfile" ]; then
         have_old_dot_file=1
         backup_dot_files+=" $dotfile"
      fi
   done
   if (($have_old_dot_file != 0)); then
      echo -ne "$locale_making_backing_up_dot_files"
      echo "gtk+qt-dot-files-before-nscde-changes-${backup_datestr}.tar"
      tar cpf gtk+qt-dot-files-before-nscde-changes-${backup_datestr}.tar $backup_dot_files
   fi
   cd - >/dev/null 2>&1

   locale_integrate_gtk2_question=$(gettext -s "Do you want to enable NsCDE GTK2 theme integration?")
   echo -ne "$locale_integrate_gtk2_question (y|n)[y] \c"
   read ans
   if [ "x$ans" == "x" ]; then
      ans="y"
   fi
   if [ "x$ans" == "xy" ]; then
      echo "$locale_integrating GTK2 theme --> 1"
      themegen_gtk2="gtk2"
      $NSCDE_TOOLSDIR/confset -t ini -c $FVWM_USERDIR/StyleMgr.ini -s ColorMgr -k integrate_gtk2 -v 1
      $NSCDE_TOOLSDIR/confset -t ini -c $FVWM_USERDIR/StyleMgr.ini -s FontMgr -k integrate_gtk2 -v 1
   elif [ "x$ans" == "xn" ]; then
      echo "$locale_not_integrating GTK2 theme --> 0"
      themegen_gtk2="False"
      $NSCDE_TOOLSDIR/confset -t ini -c $FVWM_USERDIR/StyleMgr.ini -s ColorMgr -k integrate_gtk2 -v 0
      $NSCDE_TOOLSDIR/confset -t ini -c $FVWM_USERDIR/StyleMgr.ini -s FontMgr -k integrate_gtk2 -v 0
   else
      gettext -s "Answer not understood."
      echo "$locale_integrating GTK2 theme --> 1"
      themegen_gtk2="gtk2"
      $NSCDE_TOOLSDIR/confset -t ini -c $FVWM_USERDIR/StyleMgr.ini -s ColorMgr -k integrate_gtk2 -v 1
      $NSCDE_TOOLSDIR/confset -t ini -c $FVWM_USERDIR/StyleMgr.ini -s FontMgr -k integrate_gtk2 -v 1
   fi

   locale_integrate_gtk3_question=$(gettext -s "Do you want to enable NsCDE GTK3 theme integration?")
   echo -ne "$locale_integrate_gtk3_question (y|n)[y] \c"
   read ans
   if [ "x$ans" == "x" ]; then
      ans="y"
   fi
   if [ "x$ans" == "xy" ]; then
      echo "$locale_integrating GTK3 theme --> 1"
      themegen_gtk3="gtk3"
      $NSCDE_TOOLSDIR/confset -t ini -c $FVWM_USERDIR/StyleMgr.ini -s ColorMgr -k integrate_gtk3 -v 1
      $NSCDE_TOOLSDIR/confset -t ini -c $FVWM_USERDIR/StyleMgr.ini -s FontMgr -k integrate_gtk3 -v 1
   elif [ "x$ans" == "xn" ]; then
      echo "$locale_not_integrating GTK3 theme --> 0"
      themegen_gtk3="False"
      $NSCDE_TOOLSDIR/confset -t ini -c $FVWM_USERDIR/StyleMgr.ini -s ColorMgr -k integrate_gtk3 -v 0
      $NSCDE_TOOLSDIR/confset -t ini -c $FVWM_USERDIR/StyleMgr.ini -s FontMgr -k integrate_gtk3 -v 0
   else
      gettext -s "Answer not understood."
      echo "$locale_integrating GTK3 theme --> 1"
      themegen_gtk3="gtk3"
      $NSCDE_TOOLSDIR/confset -t ini -c $FVWM_USERDIR/StyleMgr.ini -s ColorMgr -k integrate_gtk3 -v 1
      $NSCDE_TOOLSDIR/confset -t ini -c $FVWM_USERDIR/StyleMgr.ini -s FontMgr -k integrate_gtk3 -v 1
   fi

   locale_integrate_qt4_question=$(gettext -s "Do you want to enable NsCDE Qt4 theme integration?")
   echo -ne "$locale_integrate_qt4_question (y|n)[y] \c"
   read ans
   if [ "x$ans" == "x" ]; then
      ans="y"
   fi
   if [ "x$ans" == "xy" ]; then
      echo "$locale_integrating Qt4 theme --> 1"
      themeadd_qt4="qt4"
      $NSCDE_TOOLSDIR/confset -t ini -c $FVWM_USERDIR/StyleMgr.ini -s ColorMgr -k integrate_qt4 -v 1
      $NSCDE_TOOLSDIR/confset -t ini -c $FVWM_USERDIR/StyleMgr.ini -s FontMgr -k integrate_qt4 -v 1
   elif [ "x$ans" == "xn" ]; then
      echo "$locale_not_integrating Qt4 theme --> 0"
      themeadd_qt4="False"
      $NSCDE_TOOLSDIR/confset -t ini -c $FVWM_USERDIR/StyleMgr.ini -s ColorMgr -k integrate_qt4 -v 0
      $NSCDE_TOOLSDIR/confset -t ini -c $FVWM_USERDIR/StyleMgr.ini -s FontMgr -k integrate_qt4 -v 0
   else
      gettext -s "Answer not understood."
      echo "$locale_integrating Qt4 theme --> 1"
      themeadd_qt4="qt4"
      $NSCDE_TOOLSDIR/confset -t ini -c $FVWM_USERDIR/StyleMgr.ini -s ColorMgr -k integrate_qt4 -v 1
      $NSCDE_TOOLSDIR/confset -t ini -c $FVWM_USERDIR/StyleMgr.ini -s FontMgr -k integrate_qt4 -v 1
   fi

   locale_integrate_qt5_gtk_question=$(gettext -s "Do you want to enable NsCDE Qt5 GTK engine theme integration?")
   echo ""
   gettext -s "(Do not use Qt5 GTK engine if Qt5 Kvantum engine is available. Qt5 GTK engine is a legacy.)"
   echo ""
   echo -ne "$locale_integrate_qt5_gtk_question (y|n)[n] \c"
   read ans
   if [ "x$ans" == "x" ]; then
      ans="n"
   fi
   if [ "x$ans" == "xn" ]; then
      echo "$locale_not_integrating Qt5 GTK theme --> 0"
      themeadd_qt5g="False"
      themeadd_qt5="False"
      $NSCDE_TOOLSDIR/confset -t ini -c $FVWM_USERDIR/StyleMgr.ini -s ColorMgr -k integrate_qt5g -v 0
      $NSCDE_TOOLSDIR/confset -t ini -c $FVWM_USERDIR/StyleMgr.ini -s FontMgr -k integrate_qt5 -v 0
   elif [ "x$ans" == "xy" ]; then
      echo "$locale_not_integrating Qt5 GTK theme --> 1"
      themeadd_qt5g="qt5g"
      themeadd_qt5="$themeadd_qt5g"
      $NSCDE_TOOLSDIR/confset -t ini -c $FVWM_USERDIR/StyleMgr.ini -s ColorMgr -k integrate_qt5g -v 1
      $NSCDE_TOOLSDIR/confset -t ini -c $FVWM_USERDIR/StyleMgr.ini -s FontMgr -k integrate_qt5 -v 1
   else
      gettext -s "Answer not understood."
      echo "$locale_integrating Qt5 GTK theme --> 0"
      themeadd_qt5g="False"
      themeadd_qt5="False"
      $NSCDE_TOOLSDIR/confset -t ini -c $FVWM_USERDIR/StyleMgr.ini -s ColorMgr -k integrate_qt5g -v 0
      $NSCDE_TOOLSDIR/confset -t ini -c $FVWM_USERDIR/StyleMgr.ini -s FontMgr -k integrate_qt5 -v 1
   fi

   locale_integrate_qt5_kvantum_question=$(gettext -s "Do you want to enable NsCDE Qt5 Kvantum engine theme integration?")
   echo -ne "$locale_integrate_qt5_kvantum_question (y|n)[y] \c"
   read ans
   if [ "x$ans" == "x" ]; then
      ans="y"
   fi
   if [ "x$ans" == "xy" ]; then
      echo "$locale_integrating Qt5 Kvantum theme --> 1"
      themeadd_qt5k="qt5k"
      themeadd_qt5="$themeadd_qt5k"
      $NSCDE_TOOLSDIR/confset -t ini -c $FVWM_USERDIR/StyleMgr.ini -s ColorMgr -k integrate_qt5k -v 1
      $NSCDE_TOOLSDIR/confset -t ini -c $FVWM_USERDIR/StyleMgr.ini -s FontMgr -k integrate_qt5 -v 1
   elif [ "x$ans" == "xn" ]; then
      echo "$locale_not_integrating Qt5 Kvantum theme --> 0"
      if [ "$themeadd_qt5" != "qt5g" ]; then
         themeadd_qt5k="False"
         themeadd_qt5="False"
         $NSCDE_TOOLSDIR/confset -t ini -c $FVWM_USERDIR/StyleMgr.ini -s ColorMgr -k integrate_qt5k -v 0
         $NSCDE_TOOLSDIR/confset -t ini -c $FVWM_USERDIR/StyleMgr.ini -s FontMgr -k integrate_qt5 -v 0
      fi
   else
      gettext -s "Answer not understood."
      echo "$locale_integrating Qt5 Kvantum theme --> 1"
      themeadd_qt5k="qt5k"
      themeadd_qt5="$themeadd_qt5k"
      $NSCDE_TOOLSDIR/confset -t ini -c $FVWM_USERDIR/StyleMgr.ini -s ColorMgr -k integrate_qt5k -v 1
      $NSCDE_TOOLSDIR/confset -t ini -c $FVWM_USERDIR/StyleMgr.ini -s FontMgr -k integrate_qt5 -v 1
   fi

   locale_integrate_qt6_question=$(gettext -s "Do you want to enable NsCDE Qt6 theme integration?")
   echo -ne "$locale_integrate_qt6_question (y|n)[y] \c"
   read ans
   if [ "x$ans" == "x" ]; then
      ans="y"
   fi
   if [ "x$ans" == "xy" ]; then
      echo "$locale_integrating Qt6 Kvantum theme --> 1"
      themeadd_qt6="qt6"
      $NSCDE_TOOLSDIR/confset -t ini -c $FVWM_USERDIR/StyleMgr.ini -s ColorMgr -k integrate_qt6 -v 1
      $NSCDE_TOOLSDIR/confset -t ini -c $FVWM_USERDIR/StyleMgr.ini -s FontMgr -k integrate_qt6 -v 1
   elif [ "x$ans" == "xn" ]; then
      echo "$locale_not_integrating Qt6 Kvantum theme --> 0"
      themeadd_qt6="False"
      $NSCDE_TOOLSDIR/confset -t ini -c $FVWM_USERDIR/StyleMgr.ini -s ColorMgr -k integrate_qt6 -v 0
      $NSCDE_TOOLSDIR/confset -t ini -c $FVWM_USERDIR/StyleMgr.ini -s FontMgr -k integrate_qt6 -v 0
   else
      gettext -s "Answer not understood."
      echo "$locale_integrating Qt6 Kvantum theme --> 1"
      themeadd_qt6="qt6"
      $NSCDE_TOOLSDIR/confset -t ini -c $FVWM_USERDIR/StyleMgr.ini -s ColorMgr -k integrate_qt6 -v 1
      $NSCDE_TOOLSDIR/confset -t ini -c $FVWM_USERDIR/StyleMgr.ini -s FontMgr -k integrate_qt6 -v 1
   fi

   # Disable "fancy" xcursors
   if [ ! -e "$HOME/.icons/default/index.theme" ]; then
      locale_Writing=$(gettext -s "Writing")
      echo "$locale_Writing $HOME/.icons/default/index.theme ..."
      mkdir -p "$HOME/.icons/default"
      echo -ne '[Icon Theme]\nInherits=dmz-aa\n' > "$HOME/.icons/default/index.theme"
   else
      locale_setting_default_cursor_theme=$(gettext -s "Setting default cursor theme")
      echo "${HOME}/.icons/default/index.theme ${already_exists}. ${locale_setting_default_cursor_theme}."
      $NSCDE_TOOLSDIR/confset -t ini -c "$HOME/.icons/default/index.theme" -s "Icon Theme" -k Inherits -v dmz-aa
   fi

   # Gtk2
   if [ "$themegen_gtk2" == "gtk2" ]; then
      gettext -s "Setting GTK2 NsCDE theme options."
      $NSCDE_TOOLSDIR/confset -t properties -c "$HOME/.gtkrc-2.0" -k gtk-font-name -v '"DejaVu Serif Book 10"'
      $NSCDE_TOOLSDIR/confset -t properties -c "$HOME/.gtkrc-2.0" -k gtk-button-images -v 0
      $NSCDE_TOOLSDIR/confset -t properties -c "$HOME/.gtkrc-2.0" -k gtk-menu-images -v 0
      $NSCDE_TOOLSDIR/confset -t properties -c "$HOME/.gtkrc-2.0" -k gtk-cursor-blink -v 0
   fi

   # Gtk3
   if [ "$themegen_gtk3" == "gtk3" ]; then
      mkdir -p "$HOME/.config/gtk-3.0"
      gettext -s "Setting GTK3 NsCDE theme options."
      $NSCDE_TOOLSDIR/confset -t ini -c "$HOME/.config/gtk-3.0/settings.ini" -s Settings -k gtk-cursor-theme-name -v dmz-aa
      $NSCDE_TOOLSDIR/confset -t ini -c "$HOME/.config/gtk-3.0/settings.ini" -s Settings -k gtk-button-images -v 0
      $NSCDE_TOOLSDIR/confset -t ini -c "$HOME/.config/gtk-3.0/settings.ini" -s Settings -k gtk-menu-images -v 0
      $NSCDE_TOOLSDIR/confset -t ini -c "$HOME/.config/gtk-3.0/settings.ini" -s Settings -k gtk-font-name -v "DejaVu Serif Book 10"
      $NSCDE_TOOLSDIR/confset -t ini -c "$HOME/.config/gtk-3.0/settings.ini" -s Settings -k gtk-cursor-blink -v 0
   fi

   # Weird gsettings idiocy reported as issue
   whence -q gsettings
   if (($? == 0)); then
      gcolorscheme=$(gsettings get org.gnome.desktop.interface color-scheme >/dev/null 2>&1)
      if (($? == 0)); then
         if [ "$gcolorscheme" != "'default'" ]; then
            gsettings set org.gnome.desktop.interface color-scheme default
         fi
      fi
   fi

   # Qt4
   if [ "$themeadd_qt4" = "qt4" ]; then
      mkdir -p $HOME/.config/qt5ct/{qss,colors}
      gettext -s "Setting NsCDE parameters in Trolltech.conf."
      $NSCDE_TOOLSDIR/confset -t ini -c "$HOME/.config/Trolltech.conf" -s Qt -k font -v '"DejaVu Serif,10"'
   fi

   # Qt5
   if [ "$themeadd_qt5g" = "qt5g" ] || [ "$themeadd_qt5k" = "qt5k" ]; then
      mkdir -p $HOME/.config/qt5ct/{qss,colors}
      gettext -s "Setting NsCDE parameters in qt5ct.conf."
      $NSCDE_TOOLSDIR/confset -t ini -c "$HOME/.config/qt5ct/qt5ct.conf" -s Fonts -k general -v '"DejaVu Serif,10"'
      $NSCDE_TOOLSDIR/confset -t ini -c "$HOME/.config/qt5ct/qt5ct.conf" -s Fonts -k fixed -v '"DejaVu Sans Mono,10"'
      $NSCDE_TOOLSDIR/confset -t ini -c "$HOME/.config/qt5ct/qt5ct.conf" -s Interface -k show_shortcuts_in_context_menus -v true
      $NSCDE_TOOLSDIR/confset -t ini -c "$HOME/.config/qt5ct/qt5ct.conf" -s Interface -k keyboard_scheme -v 5
   fi

   # Qt6
   if [ "$themeadd_qt6" = "qt6" ]; then
      if [ -d "/usr/share/qt6" ] || [ -d "/usr/local/share/qt6" ] || [ -d "/usr/pkg/qt6" ]; then
         mkdir -p $HOME/.config/qt6ct/{qss,colors}
         gettext -s "Setting NsCDE parameters in qt6ct.conf."
         $NSCDE_TOOLSDIR/confset -t ini -c "$HOME/.config/qt6ct/qt6ct.conf" -s Fonts -k general -v '"DejaVu Serif,10"'
         $NSCDE_TOOLSDIR/confset -t ini -c "$HOME/.config/qt6ct/qt6ct.conf" -s Fonts -k fixed -v '"DejaVu Sans Mono,10"'
         $NSCDE_TOOLSDIR/confset -t ini -c "$HOME/.config/qt6ct/qt6ct.conf" -s Interface -k show_shortcuts_in_context_menus -v true
         $NSCDE_TOOLSDIR/confset -t ini -c "$HOME/.config/qt6ct/qt6ct.conf" -s Interface -k keyboard_scheme -v 5
      fi
   fi

   # Color Manager local hooks
   if [ ! -e "${FVWM_USERDIR}/libexec/colormgr.local" ]; then
      setting_default=$(gettext -s "Setting default")
      echo "$setting_default ${FVWM_USERDIR}/libexec/colormgr.local ..."
      mkdir -p "${FVWM_USERDIR}/libexec"
      cp ${NSCDE_DATADIR}/config_templates/colormgr.local "${FVWM_USERDIR}/libexec/colormgr.local"
      chmod +x "${FVWM_USERDIR}/libexec/colormgr.local"
      $NSCDE_TOOLSDIR/confset -t ini -c $FVWM_USERDIR/StyleMgr.ini -s ColorMgr -k integrate_local -v 1
      $NSCDE_TOOLSDIR/confset -t ini -c $FVWM_USERDIR/StyleMgr.ini -s FontMgr -k integrate_local -v 1
   else
      echo "${FVWM_USERDIR}/libexec/colormgr.local ${already_exists}."
   fi

   # Font Manager local hooks
   if [ ! -e "${FVWM_USERDIR}/libexec/fontmgr.local" ]; then
      setting_default=$(gettext -s "Setting default")
      echo "$setting_default ${FVWM_USERDIR}/libexec/fontmgr.local ..."
      mkdir -p "${FVWM_USERDIR}/libexec"
      cp ${NSCDE_DATADIR}/config_templates/fontmgr.local "${FVWM_USERDIR}/libexec/fontmgr.local"
      chmod +x "${FVWM_USERDIR}/libexec/fontmgr.local"
   else
      echo "${FVWM_USERDIR}/libexec/fontmgr.local ${already_exists}."
   fi

   # Generate themes and change the screen
   locale_generating_default_theme_for_gtk2_gtk3_in=$(gettext -s "Generating default theme for Gtk2/Gtk3 in")
   echo "$locale_generating_default_theme_for_gtk2_gtk3_in $HOME/.themes/NsCDE ..."
   mkdir -p "$HOME/.themes"
   if [ -d "/usr/share/qt6" ] || [ -d "/usr/local/share/qt6" ] || [ -d "/usr/pkg/share/qt6" ]; then
      ${NSCDE_TOOLSDIR}/chtheme -p $NSCDE_PALETTE -n $NSCDE_PALETTE_NCOLORS -5 -m -b -x -g $themegen_gtk2,$themegen_gtk3 -q $themeadd_qt4,$themeadd_qt5,$themeadd_qt6 -l
   else
      ${NSCDE_TOOLSDIR}/chtheme -p $NSCDE_PALETTE -n $NSCDE_PALETTE_NCOLORS -5 -m -b -x -g $themegen_gtk2,$themegen_gtk3 -q $themeadd_qt4,$themeadd_qt5,False -l
   fi
   # Reload is separate, so that new configs are in place.
   ${NSCDE_TOOLSDIR}/chtheme -p $NSCDE_PALETTE -n $NSCDE_PALETTE_NCOLORS -R

   locale_enabling=$(gettext -s "Enabling:")
   locale_not_enabling=$(gettext -s "Not Enabling:")
   locale_enabled_by_default=$(gettext -s "Enabled by default:")
   locale_disabling=$(gettext -s "Disabling:")
   locale_enable_visual_pager_question=$(gettext -s "Do you want to enable NsCDE-specific visual pager preview on page/workspace change?")
   echo -ne "$locale_enable_visual_pager_question (y|n)[y] \c"
   read ans
   if [ "x$ans" == "x" ]; then
      ans="y"
   fi
   if [ "x$ans" == "xy" ]; then
      echo "${locale_enabling}: pageshowrootpager --> 1"
      $NSCDE_TOOLSDIR/ised -c 's/InfoStoreAdd pageshowrootpager 0/InfoStoreAdd pageshowrootpager 1/g' -f "$FVWM_USERDIR/NsCDE.conf"
   elif [ "x$ans" == "xn" ]; then
      echo "${locale_not_enabling} pageshowrootpager --> 0"
   else
      gettext -s "Answer not understood."
      echo "$locale_not_enabling pageshowrootpager --> 0"
   fi

   locale_remember_workspace_question=$(gettext -s "Do you want to remember last page on workspace when changing back to it?")
   echo -ne "$locale_remember_workspace_question (y|n)[y] \c"
   read ans
   if [ "x$ans" == "x" ]; then
      ans="y"
   fi
   if [ "x$ans" == "xy" ]; then
      echo "$locale_enabling desklastpage --> 1"
      $NSCDE_TOOLSDIR/ised -c 's/InfoStoreAdd desklastpage 0/InfoStoreAdd desklastpage 1/g' -f "$FVWM_USERDIR/NsCDE.conf"
   elif [ "x$ans" == "xn" ]; then
      echo "$locale_not_enabling desklastpage --> 0"
      $NSCDE_TOOLSDIR/ised -c 's/InfoStoreAdd desklastpage 1/InfoStoreAdd desklastpage 0/g' -f "$FVWM_USERDIR/NsCDE.conf"
   else
      gettext -s "Answer not understood."
      echo "$locale_not_enabling desklastpage --> 0"
      $NSCDE_TOOLSDIR/ised -c 's/InfoStoreAdd desklastpage 1/InfoStoreAdd desklastpage 0/g' -f "$FVWM_USERDIR/NsCDE.conf"
   fi

   if [ "x$FVWM_IS_FVWM3" == "x1" ]; then
      locale_enable_fvwm3_default_logging_question=$(gettext -s "Do you want to enable FVWM3 logging by default?")
      echo -ne "$locale_enable_fvwm3_default_logging_question (y|n)[y] \c"
      read ans
      if [ "x$ans" == "x" ]; then
         ans="y"
      fi
      if [ "x$ans" == "xy" ]; then
         echo "${locale_enabling}: fvwm3_default_logging --> 1"
         $NSCDE_TOOLSDIR/ised -c 's/# InfoStoreAdd fvwm3_default_logging 1/InfoStoreAdd fvwm3_default_logging 1/g' -f "$FVWM_USERDIR/NsCDE.conf"
      elif [ "x$ans" == "xn" ]; then
         echo "${locale_not_enabling} fvwm3_default_logging --> 0"
      else
         gettext -s "Answer not understood."
         echo "$locale_not_enabling fvwm3_default_logging --> 0"
      fi
   fi

   locale_enable_xscreensaver_question=$(gettext -s "Do you want to enable X Screen Saver?")
   whence -q xscreensaver
   xscreensaver_found=$?
   if (($xscreensaver_found != 0)); then
      gettext -s "WARNING: Xscreensaver installation not found."
      echo -ne "$locale_enable_xscreensaver_question (y|n)[n] \c"
   else
      echo -ne "$locale_enable_xscreensaver_question (y|n)[y] \c"
   fi
   read ans
   if [ "x$ans" == "x" ]; then
      if (($xscreensaver_found != 0)); then
         ans="n"
      else
         ans="y"
      fi
   fi
   if [ "x$ans" == "xy" ]; then
      echo "$locale_enabled_by_default nscde_use_xscreensaver --> 1"
      if [ ! -e "$HOME/.xscreensaver" ]; then
         echo -ne "$HOME/.xscreensaver "; gettext -s "does not exist."
         gettext -s -n "Installing default .xscreensaver file as "; echo "$HOME/.xscreensaver."
         cp ${NSCDE_DATADIR}/config_templates/dot_xscreensaver "${HOME}/.xscreensaver"
      fi
   elif [ "x$ans" == "xn" ]; then
      echo "$locale_disabling nscde_use_xscreensaver --> 0"
      $NSCDE_TOOLSDIR/ised -c 's/InfoStoreAdd nscde_use_xscreensaver 1/InfoStoreAdd nscde_use_xscreensaver 0/g' -f "$FVWM_USERDIR/NsCDE.conf"
      pkill -u $(id -u) xscreensaver
   else
      gettext -s "Answer not understood."
      echo "$locale_enabled_by_default nscde_use_xscreensaver --> 1"
      if [ ! -e "$HOME/.xscreensaver" ]; then
         echo -ne "$HOME/.xscreensaver "; gettext -s "does not exist."
         gettext -s -n "Installing default .xscreensaver file as "; echo "$HOME/.xscreensaver."
         cp ${NSCDE_DATADIR}/config_templates/dot_xscreensaver "${HOME}/.xscreensaver"
      fi
   fi

   # Stalonetray - if it doesn't have configuration yet ready.
   if [ ! -e "${FVWM_USERDIR}/Stalonetray.conf" ]; then
      echo "$setting_default ${FVWM_USERDIR}/Stalonetray.conf ..."
      cp ${NSCDE_DATADIR}/config_templates/Stalonetray.conf "${FVWM_USERDIR}/Stalonetray.conf"
   else
      locale_already_in_place=$(gettext -s "already in place")
      echo "${FVWM_USERDIR}/Stalonetray.conf ${locale_already_in_place}."
   fi

   locale_enable_stalonetray_question=$(gettext -s "Do you want to enable Stalonetray Tray?")
   echo -ne "$locale_enable_stalonetray_question (y|n)[y] \c"
   read ans
   if [ "x$ans" == "x" ]; then
      ans="y"
   fi
   if [ "x$ans" == "xy" ]; then
      echo "$locale_enabled_by_default nscde_use_stalonetray --> 1"
   elif [ "x$ans" == "xn" ]; then
      echo "$locale_disabling nscde_use_stalonetray --> 0"
      $NSCDE_TOOLSDIR/ised -c 's/InfoStoreAdd nscde_use_stalonetray 1/InfoStoreAdd nscde_use_stalonetray 0/g' -f "$FVWM_USERDIR/NsCDE.conf"
   else
      gettext -s "Answer not understood."
      echo "$locale_enabled_by_default nscde_use_stalonetray --> 1"
   fi

   whence -q xsettingsd
   if (($? == 0)); then
      locale_enable_xsettingsd_question=$(gettext -s "Do you want to enable color scheme integration with XSETTINGS daemon xsettingsd?")
      echo -ne "$locale_enable_xsettingsd_question (y|n)[y] \c"
      read ans
      if [ "x$ans" == "x" ]; then
         ans="y"
      fi
      if [ "x$ans" == "xy" ]; then
         echo "$locale_enabling nscde_use_xsettingsd --> 1"
         $NSCDE_TOOLSDIR/ised -c 's/InfoStoreAdd nscde_use_xsettingsd 0/InfoStoreAdd nscde_use_xsettingsd 1/g' -f "$FVWM_USERDIR/NsCDE.conf"
      elif [ "x$ans" == "xn" ]; then
         echo "$locale_not_enabling nscde_use_xsettingsd --> 0"
      else
         gettext -s "Answer not understood."
         echo "$locale_not_enabling nscde_use_xsettingsd --> 0"
      fi
   fi

   whence -q dunst
   if (($? == 0)); then
      locale_enable_dunst_question=$(gettext -s "Do you want to enable dunst notification daemon?")
      echo -ne "$locale_enable_dunst_question (y|n)[y] \c"
      read ans
      if [ "x$ans" == "x" ]; then
         ans="y"
      fi
      if [ "x$ans" == "xy" ]; then
         echo "$locale_enabling nscde_use_dunst --> 1"
         cp -f "$NSCDE_DATADIR/config_templates/Dunst.conf" "$FVWM_USERDIR/Dunst.conf"
         $NSCDE_TOOLSDIR/ised -c 's/InfoStoreAdd nscde_use_dunst 0/InfoStoreAdd nscde_use_dunst 1/g' -f "$FVWM_USERDIR/NsCDE.conf"
      elif [ "x$ans" == "xn" ]; then
         echo "$locale_not_enabling nscde_use_dunst --> 0"
      else
         gettext -s "Answer not understood."
         echo "$locale_not_enabling nscde_use_dunst --> 0"
      fi
   fi

   whence -q picom
   if (($? == 0)); then
      locale_enable_picom_question=$(gettext -s "Do you want to enable Picom X Compositor?")
      echo -ne "$locale_enable_picom_question (y|n)[n] \c"
      read ans
      if [ "x$ans" == "x" ]; then
         ans="n"
      fi
      if [ "x$ans" == "xn" ]; then
         echo "$locale_not_enabling nscde_use_picom --> 0"
      elif [ "x$ans" == "xy" ]; then
         echo "$locale_enabling nscde_use_picom --> 1"
         cp -f "$NSCDE_DATADIR/config_templates/Picom.conf" "$FVWM_USERDIR/Picom.conf"
         $NSCDE_TOOLSDIR/ised -c 's/InfoStoreAdd nscde_use_picom 0/InfoStoreAdd nscde_use_picom 1/g' -f "$FVWM_USERDIR/NsCDE.conf"
      else
         gettext -s "Answer not understood."
         echo "$locale_not_enabling nscde_use_picom --> 0"
      fi
   fi

   whence -q rofi
   if (($? == 0)); then
      locale_run_rofi_question=$(gettext -s "Do you want to run rofi launcher on Alt+F12 or built in Exec Dialog?")
      echo -ne "$locale_run_rofi_question (y|n)[y] \c"
      read ans
      if [ "x$ans" == "x" ]; then
         ans="y"
      fi
      if [ "x$ans" == "xy" ]; then
         echo "$locale_enabling nscde_use_rofi --> 1"
         $NSCDE_TOOLSDIR/ised -c 's/# InfoStoreAdd nscde_use_rofi 1/InfoStoreAdd nscde_use_rofi 1/g' -f "$FVWM_USERDIR/NsCDE.conf"
         if [ ! -e "$HOME/.config/rofi/config" ] ; then
            echo -ne "$HOME/.config/rofi/config "; gettext -s "does not exist."
            gettext -s -n "Installing default rofi config file as "; echo "$HOME/.config/rofi/config."
            mkdir -p $HOME/.config/rofi
            cp -f ${NSCDE_DATADIR}/config_templates/rofi_config "${HOME}/.config/rofi/config"
         fi
      elif [ "x$ans" == "xn" ]; then
         echo "$locale_not_enabling nscde_use_rofi --> 0"
      else
         gettext -s "Answer not understood."
         echo "$locale_not_enabling nscde_use_rofi --> 0"
      fi
   fi

   # Make a default $FVWM_USERDIR/Xset.conf
   if [ ! -e $HOME/.NsCDE/Xset.conf ]; then
      locale_setting_up_default_xset_conf=$(gettext -s "Installing default Xset.conf example.")
      echo "$locale_setting_up_default_xset_conf"
      cp -f ${NSCDE_DATADIR}/config_templates/Xset.conf "${HOME}/.NsCDE/Xset.conf"
      chmod u+x "${HOME}/.NsCDE/Xset.conf"
   fi

   filemgrs=$(<$NSCDE_DATADIR/fallback/app-catalog/filemgr)
   for filemgr in $filemgrs
   do
      whence -q $filemgr
      if (($? == 0)); then
         detected_filemgrs="$detected_filemgrs $filemgr"
      fi
   done
   detected_filemgrs="${detected_filemgrs# *}"
   filemgr_suggest=${detected_filemgrs%% *}

   locale_detected_X_File_Managers=$(gettext -s "Detected X File Managers on the local system:")
   locale_your_favorite_X_File_Manager_command=$(gettext -s "Your favorite X File Manager (command)?")
   locale_setting_filemgr=$(gettext -s "Setting X File Manager:")
   locale_setting_proposed_filemgr=$(gettext -s "Setting Proposed X File Manager:")
   locale_skipping_fm_set_later=$(gettext -s "Skipping File Manager selection. You can set it later in")
   echo -ne "$locale_detected_X_File_Managers ${detected_filemgrs}\n"
   echo -ne "$locale_your_favorite_X_File_Manager_command [${filemgr_suggest}] \c"
   read ans
   if [ "x$ans" != "x" ]; then
      echo "$locale_setting_filemgr filemgr --> ${ans}"
      $NSCDE_TOOLSDIR/ised -c "s/InfoStoreAdd filemgr true/InfoStoreAdd filemgr ${ans}/g" -f "$FVWM_USERDIR/NsCDE.conf"

      # Insanely retarded xdg utils. No exit status, no error, no listings of desktop files.
      whence -q xdg-mime
      if (($? == 0)); then
         xdg-mime default ${ans}.desktop inode/directory
         fmresult=$(xdg-mime query default inode/directory)
         if [ "$fmresult" != "${ans}.desktop" ]; then
            xdg-mime default org.kde.${ans}.desktop inode/directory
            fmresult=$(xdg-mime query default inode/directory)
            if [ "$fmresult" != "org.kde.${ans}.desktop" ]; then
               xdg-mime default org.gnome.${ans}.desktop inode/directory
            fi
         fi
      fi

   elif [ "x${filemgr_suggest}" != "x" ]; then
      echo "$locale_setting_proposed_filemgr filemgr --> ${filemgr_suggest}"
      $NSCDE_TOOLSDIR/ised -c "s/InfoStoreAdd filemgr true/InfoStoreAdd filemgr ${filemgr_suggest}/g" -f "$FVWM_USERDIR/NsCDE.conf"

      # Insanely retarded xdg utils. No exit status, no error, no listings of desktop files.
      whence -q xdg-mime
      if (($? == 0)); then
         xdg-mime default ${filemgr_suggest}.desktop inode/directory
         fmresult=$(xdg-mime query default inode/directory)
         if [ "$fmresult" != "${filemgr_suggest}.desktop" ]; then
            xdg-mime default org.kde.${filemgr_suggest}.desktop inode/directory
            fmresult=$(xdg-mime query default inode/directory)
            if [ "$fmresult" != "org.kde.${filemgr_suggest}.desktop" ]; then
               xdg-mime default org.gnome.${filemgr_suggest}.desktop inode/directory
            fi
         fi
      fi

   else
      echo "$locale_skipping_fm_set_later $FVWM_USERDIR/NsCDE.conf."
   fi

   xeditors=$(<$NSCDE_DATADIR/fallback/app-catalog/text-editor)
   for xeditor in $xeditors
   do
      whence -q $xeditor
      if (($? == 0)); then
         detected_xeditors="$detected_xeditors $xeditor"
      fi
   done
   detected_xeditors="${detected_xeditors# *}"
   xedit_suggest=${detected_xeditors%% *}

   locale_detected_X_Text_Editors=$(gettext -s "Detected X Text Editors on the local system:")
   locale_your_favorite_X_Text_Editor_command=$(gettext -s "Your favorite X Text Editor (command)?")
   echo -ne "$locale_detected_X_Text_Editors ${detected_xeditors}\n"
   echo -ne "$locale_your_favorite_X_Text_Editor_command [$xedit_suggest] \c"
   locale_setting_editor=$(gettext -s "Setting X Text Editor:")
   locale_setting_proposed_editor=$(gettext -s "Setting Proposed X Text Editor:")
   locale_skipping_ed_set_later=$(gettext -s "Skipping X Editor selection. You can set it later in")
   read ans
   if [ "x$ans" != "x" ]; then
      echo "$locale_setting_editor xeditor --> ${ans}"
      if [ "x$ans" != "gvim" ]; then
         $NSCDE_TOOLSDIR/ised -c "s/InfoStoreAdd xeditor gvim/InfoStoreAdd xeditor ${ans}/g" -f "$FVWM_USERDIR/NsCDE.conf"

         # Insanely retarded xdg utils. No exit status, no error, no listings of desktop files.
         txtmimes="application/x-shellscript text/english text/plain text/x-c text/x-c++ text/x-c++hdr text/x-c++src text/x-chdr text/x-csrc text/x-java text/x-makefile text/x-readme text/x-tcl"
         whence -q xdg-mime
         if (($? == 0)); then
            xdg-mime default ${ans}.desktop text/plain
            txtresult=$(xdg-mime query default text/plain)
            if [ "$txtresult" != "${ans}.desktop" ]; then
               xdg-mime default org.kde.${ans}.desktop text/plain
               txtresult=$(xdg-mime query default text/plain)
               if [ "$txtresult" != "org.kde.${ans}.desktop" ]; then
                  xdg-mime default org.gnome.${ans}.desktop text/plain
                  txtresult=$(xdg-mime query default text/plain)
                  if [ "$txtresult" == "org.gnome.${ans}.desktop" ]; then
                     for tm in $txtmimes
                     do
                        xdg-mime default org.gnome.${ans}.desktop $tm
                     done
                  fi
               else
                  for tm in $txtmimes
                  do
                     xdg-mime default org.kde.${ans}.desktop $tm
                  done
               fi
            else
               for tm in $txtmimes
               do
                  xdg-mime default ${ans}.desktop $tm
               done
            fi
         fi

      fi
   elif [ "x${xedit_suggest}" != "x" ]; then
      echo "$locale_setting_proposed_editor xeditor --> ${xedit_suggest}"
      if [ "x$ans" != "gvim" ]; then
         $NSCDE_TOOLSDIR/ised -c "s/InfoStoreAdd filemgr true/InfoStoreAdd xeditor ${xedit_suggest}/g" -f "$FVWM_USERDIR/NsCDE.conf"

         # Insanely retarded xdg utils. No exit status, no error, no listings of desktop files.
         txtmimes="application/x-shellscript text/english text/plain text/x-c text/x-c++ text/x-c++hdr text/x-c++src text/x-chdr text/x-csrc text/x-java text/x-makefile text/x-readme text/x-tcl"
         whence -q xdg-mime
         if (($? == 0)); then
            xdg-mime default ${xedit_suggest}.desktop text/plain
            txtresult=$(xdg-mime query default text/plain)
            if [ "$txtresult" != "${xedit_suggest}.desktop" ]; then
               xdg-mime default org.kde.${xedit_suggest}.desktop text/plain
               txtresult=$(xdg-mime query default text/plain)
               if [ "$txtresult" != "org.kde.${xedit_suggest}.desktop" ]; then
                  xdg-mime default org.gnome.${xedit_suggest}.desktop text/plain
                  txtresult=$(xdg-mime query default text/plain)
                  if [ "$txtresult" == "org.gnome.${xedit_suggest}.desktop" ]; then
                     for tm in $txtmimes
                     do
                        xdg-mime default org.gnome.${xedit_suggest}.desktop $tm
                     done
                  fi
               else
                  for tm in $txtmimes
                  do
                     xdg-mime default org.kde.${xedit_suggest}.desktop $tm
                  done
               fi
            else
               for tm in $txtmimes
               do
                  xdg-mime default ${xedit_suggest}.desktop $tm
               done
            fi
         fi

      fi
   else
      echo "$locale_setting_proposed_editor $FVWM_USERDIR/NsCDE.conf."
   fi

   locale_terminal_program_in=$(gettext -s "terminal program in")
   locale_or_leave_autodiscovery=$(gettext -s "or leave autodiscovery")
   locale_Hint_See=$(gettext -s "Hint: See")
   locale_for_Gkrellm_X_session_and=$(gettext -s "for Gkrellm, X session and")
   sleep 1
   gettext -s '... done!'
   echo ""
   gettext -s "You can set \$[infostore.terminal] variable to your favorite"
   echo "$locale_terminal_program_in \$HOME/.NsCDE/NsCDE.conf ${locale_or_leave_autodiscovery}."
   echo ""
   echo "$locale_Hint_See $NSCDE_DATADIR/examples $locale_for_Gkrellm_X_session_and"
   gettext -s "other integration options you may want to apply."
   sleep 1
   echo ""

   if [ "$mode" == "reconfigure" ]; then
      gettext -s "Press RETURN to finish setup and restart NsCDE."
   fi
   if [ "$mode" == "initial" ]; then
      gettext -s "Press RETURN to finish setup and start NsCDE."
   fi
   read RET
   if [ "$mode" == "initial" ]; then
      $NSCDE_ROOT/bin/nscde_fvwmclnt 'EdgeThickness 1'
      whence -q xrandr && xrandr --dpi 96
   fi
   if [ "$mode" == "initial" ]; then
      $NSCDE_ROOT/bin/nscde_fvwmclnt 'Exec exec $[infostore.terminal]'
      $NSCDE_ROOT/bin/nscde_fvwmclnt 'f_DisplayURL "$[gt.NsCDE Documentation]" NsCDE.pdf'
   fi
   $NSCDE_ROOT/bin/nscde_fvwmclnt 'Schedule 250 Restart'
   exit 0
}

while getopts rih Option
do
   case $Option in
   r)
      setup_nscde reconfigure
   ;;  
   i)
      setup_nscde initial
   ;;
   h)
      echo "Usage: ${0##*/} [ -r ] [ -i ] [ -h ]"
   ;;
   esac
done

