#!/bin/bash

# MX Tour for MX Linux 21
#
# Hastily put together Tour Guide application for MX Linux 21.

# set some stuff up
KEY=$(( ($$ * $RANDOM) % 2**15))

# set to run dir if images and text exists
[[ ${0%%/*} == . ]] && [[ -d text   ]] && TXTDIR=./text
[[ ${0%%/*} == . ]] && [[ -d images ]] && IMGDIR=./images

: ${TXTDIR:=/usr/share/mx-tour/text}
: ${IMGDIR:=/usr/share/mx-tour/images}

WINDOW_ICON=/usr/share/icons/hicolor/scalable/apps/mx-tour.svg
#pretty sure we don't need the width and height anymore here
WINDOW_CLASS=mx-tour
WINDOW_WIDTH=1024
WINDOW_HEIGHT=740

# l10n
export TEXTDOMAIN=mx-tour
export TEXTDOMAINDIR=/usr/share/locale
#. gettext.sh

        TITLE=$(gettext "MX Tour")
        INTRO=$(gettext "Intro")
        PANEL=$(gettext "Panel")
      TASKBAR=$(gettext "Taskbar")
        CONKY=$(gettext "Conky")
     MX_TOOLS=$(gettext "MX Tools")
   MX_WELCOME=$(gettext "MX Welcome")
     MX_TWEAK=$(gettext "MX Tweak")
 INSTALL_APPS=$(gettext "Install Apps")
     UPDATING=$(gettext "Updating")
  MX_SNAPSHOT=$(gettext "MX Snapshot")
    TIMESHIFT=$(gettext "Timeshift")
  SYSTEM_INFO=$(gettext "System Info")
     GET_HELP=$(gettext "Get Help")
 WHISKER_MENU=$(gettext "Whisker Menu")
      WELCOME=$(gettext "WELCOME TO THE MX LINUX TOUR")

     HELPTEXT=$(gettext "Below are some available resources to get help")

NO_TOUR=$(gettext "\n<b>You are not using a MX Tour compatible Desktop Environment or Window Manager.\n</b>
MX Tour is only compatible with the MX Linux Xfce, KDE, and Fluxbox editions.\n
This window will close automatically in 10 seconds.")

        CLOSE=$(gettext gtk30 "_Close")
         NEXT=$(gettext gtk30 "_Next")
     PREVIOUS=$(gettext gtk30 "_Previous")


# some functions
pushtext(){  # push text file into var
    local file=${1} var=${2}
    : ${file:=-}
    IFS='' read -r -d '' ${var} < <( eval cat "${file}") || true
}
# push texfiles into bash var's , where varname corresponds to filename 
for F in ${TXTDIR}/*; do
    [[ -f ${F}      ]] || continue     # only files
    
    N=${F##*/}   # basename
    N=${N//-/_}  # replace hyphens with underline
    [[ -z ${N##*.*} ]] && continue  # ignore any backup files with dot
    [[ -z ${N##*~}  ]] && continue  # ignore any backup files with ~

    declare -n nameref=$N
    pushtext "$F" $N
	nameref=$(gettext "$nameref")
done

notour(){ #there is no compatible MX Tour to use
	yad --title "MX Tour" --width=455 --height=250 --fixed --center \
	--image=face-sad \
	--borders=15 \
	--timeout=10 --timeout-indicator=bottom \
	--text="$NO_TOUR\n" --text-align=center \
	--button=gtk-close:1 &
}

# find the Window Managaer in use
WM=$(xprop -id $(xprop -root -notype _NET_SUPPORTING_WM_CHECK| cut -d\# -f2) -notype -len 100 -f _NET_WM_NAME 8t | grep ^_NET_WM_NAME | cut -d\" -f2)
# output what xprop found in case we need to debug something
echo "The Window Manager found being used is $WM"

# launch the appropriate MX Tour or display incompatibility warning
if [ "$WM" == "Xfwm4" ];
then
	source mx-xfce-tour
elif [ "$WM" == "KWin" ];
then
	source mx-kde-tour
elif [ "$WM" == "Fluxbox" ];
then
	source mx-fb-tour
else
	notour
fi

"${MAIN[@]}"

exit 0
