#!/bin/sh
# 2011 Written by Mark Vejvoda <mark_vejvoda@hotmail.com>
# 2014 Rewritten by filux <heross(@@)o2.pl>
# Copyright (c) 2011-2017 under GNU GPL v3.0+
# ----------------------------------------------------------------------------
# If you want to have core files generated (for debugging purposes):
# ulimit -c unlimited
LANG=C
SCRIPTLOCATION="$(readlink -f "$0")"
GAMEDIR="$(dirname "$SCRIPTLOCATION")"
ARCHITECTURE="$(uname -m | tr '[A-Z]' '[a-z]')"
BugReportEngine="https://github.com/MegaGlest/megaglest-source/issues"
BugReportBoth="https://forum.megaglest.org/index.php?board=23.0"
# -------
# useful for mods
BugReportBoth2="$BugReportBoth"
BugReportData="https://github.com/MegaGlest/megaglest-data/issues"

EXC_BINARY="megaglest"
if [ -f "${GAMEDIR}/megaglest_shim" ]; then EXC_BINARY="megaglest_shim"; fi

SHORT_GAME_NAME="megaglest"
# if long game name contain spaces then write it inside quotes
# e.g. "\"long name with spaces\""
LONG_GAME_NAME="MegaGlest"

# -------
LIBDIR="lib"; ExitStatus=0; LibsWarnings=""; IgnoredLibs=""; FirstLnError=""
LibDir_wP="$GAMEDIR/$LIBDIR"; LIBDIR_B=".${LIBDIR}_bak"; LibDirB_wP="$GAMEDIR/$LIBDIR_B"
if [ ! -d "$LibDir_wP" ]; then mkdir "$LibDir_wP"; fi
if [ ! -d "$LibDirB_wP" ]; then mkdir "$LibDirB_wP"; fi
if [ "$1" = "--tool-editor" ] || [ "$1" = "--tool-g3dviewer" ] || [ "$1" = "--tool-buginfo" ]; then
    OperatingMode="$1"; else OperatingMode="--game"
fi
BeginTime="$(date +"%s")"
if [ -e "/etc/os-release" ]; then
    OS_INFO="$(cat "/etc/os-release" | grep '^PRETTY_NAME=' | awk -F '"' '{print $2}')"
else
    WhichLsbRelease="$(which lsb_release 2>/dev/null)"
    if [ "$WhichLsbRelease" != "" ]; then
        OS_INFO="$($WhichLsbRelease -d | awk -F ':[ \t]*' '{print $2}')"
    fi
fi
if [ "$OperatingMode" != "--tool-buginfo" ]; then
    if [ "$LDCONFIG" = "" ]; then
        WhichLdconfig="$(which ldconfig 2>/dev/null)"
        if [ "$WhichLdconfig" != "" ]; then
            LDCONFIG="$WhichLdconfig"; LDconfigType=1
        elif [ -x "/sbin/ldconfig" ]; then
            LDCONFIG="/sbin/ldconfig"; LDconfigType=2
        else
            echo 'ERROR: Could not find the "ldconfig" command.' >&2
            echo 'Please re-run using: "LDCONFIG=/path/to/ldconfig '"$0" >&2
            exit 1
        fi
    else
        LDconfigType=9
    fi
    LibsWarnings="Found LDCONFIG variable [$LDconfigType][$LDCONFIG]..."
    if [ "$ARCHITECTURE" = "x86_64" ]; then GrepPattern="x86[_-]64"
        else GrepPattern="$ARCHITECTURE"; fi
    LDCONFIG_P="$($LDCONFIG -p)"; ArchSortLibs="no"
    LDCONFIG_P_ARCH="$(echo "$LDCONFIG_P" | grep "$GrepPattern")"
    if [ "$LDCONFIG_P_ARCH" != "" ]; then
        LDCONFIG_P_R_ARCH="$(echo "$LDCONFIG_P" | grep -v "$GrepPattern")"
        ArchSortLibs="yes"
    fi
    if [ "$OperatingMode" = "--game" ]; then
        ldd_target="$GAMEDIR/$EXC_BINARY"
    else ldd_target="$GAMEDIR/$2"
    fi

    hasSOlib() {
        if [ "$2" = "myarch" ]; then
            if [ "$ArchSortLibs" = "yes" ]; then OS_libs_list="$LDCONFIG_P_ARCH"; else OS_libs_list="$LDCONFIG_P"; fi
        else
            if [ "$ArchSortLibs" = "yes" ]; then OS_libs_list="$LDCONFIG_P_R_ARCH"; else OS_libs_list=""; fi
        fi
        if [ "$OS_libs_list" != "" ]; then
            echo "$OS_libs_list" | grep -m 1 "[^_-]$1" | cut "-d>" -f2 | cut "-d " -f2
        fi
    }

    findMissingSO() {
        SO_WANTED_LIB="$1"; SO_WANTED_LIB2="$2"; SO_LIB_PATTERN="$3"
        SO_LINKED_LIB="$LibDir_wP/$SO_WANTED_LIB2"

        hasSO="$(hasSOlib "$SO_LIB_PATTERN" "myarch")"; CommonPart="no"
        if [ ! -L "$SO_LINKED_LIB" ] && [ -e "$SO_LINKED_LIB" ]; then EmbLibExist="yes"; else EmbLibExist="no"; fi
        if [ "$hasSO" != "" ] && ( [ "$6" != "|L" ] || [ "$EmbLibExist" = "no" ] ); then
            CommonPart="yes"
        elif [ "$SO_WANTED_LIB" = "$SO_LIB_PATTERN" ] && [ "$EmbLibExist" = "yes" ] && [ "$6" != "|H" ]; then
            if [ "$6" = "|L" ]; then Emb_Keyword=" is used as"; else Emb_Keyword=" is missing in your OS, using"; fi
            LibsWarnings="$LibsWarnings
            ||- default library ${5}[$SO_WANTED_LIB2]$Emb_Keyword embedded library."
            return 0
        else
            hasSO="$(hasSOlib "$SO_LIB_PATTERN")"
            if [ "$ArchSortLibs" = "yes" ] && [ "$hasSO" != "" ]; then
                CommonPart="yes"
            else
                if [ "$SO_WANTED_LIB" = "$SO_LIB_PATTERN" ] && ( [ "$EmbLibExist" = "no" ] || [ "$6" = "|H" ] ); then
                    if [ "$6" = "|H" ] && [ "$EmbLibExist" = "yes" ]; then Emb_Keyword2=" (ae)"; else Emb_Keyword2=""; fi
                LibsWarnings="$LibsWarnings
           ||- default library ${5}[$SO_WANTED_LIB2] is missing in your OS${Emb_Keyword2}, attempting to find and link to different version if is available..."
        fi
        if [ "$4" != "no" ]; then
            LibsWarnings="$LibsWarnings
            ||  > not found !"
        fi
        return 1
        fi
    fi
    if [ "$CommonPart" = "yes" ]; then
        if [ "$SO_WANTED_LIB" != "$SO_LIB_PATTERN" ]; then
        ln -f -s "$hasSO" "$SO_LINKED_LIB"
        if [ "$?" -eq "0" ]; then
            LibsWarnings="$LibsWarnings
            ||  new library link [.../$LIBDIR/$SO_WANTED_LIB2] pointed to [$hasSO]."
        else
            LibsWarnings="$LibsWarnings
            ||  ERROR: Detected some problems during process of creating symlink."
            if [ "$FirstLnError" = "" ]; then FirstLnError="no"
            LibsWarnings="$LibsWarnings
            ||  ... lack of write permissions in game location?"
            fi
        fi
        else
        if [ -e "$SO_LINKED_LIB" ]; then mv -f "$SO_LINKED_LIB" "$LibDirB_wP"; fi
        if [ "$(echo "$c_required_libs" | grep "${SO_LIB_PATTERN}[ \t]*=>[ \t]*not found")" != "" ] && [ "$EmbLibExist" = "no" ]; then
            #rare exception
            LibsWarnings="$LibsWarnings
            ||- default library: ${5}[$SO_WANTED_LIB2] is missing in your OS, attempting to find and link to different version if is available...
            ||  > not found !"
        fi
        fi
        return 0
    fi
    }

    checkLibsStatus() {
    if [ "$1" != "" ]; then
        check_nr=1
        while [ "$check_nr" -le "2" ]; do
        if [ "$check_nr" -eq "1" ]; then c_required_libs="$(ldd "$ldd_target")"; else
            c_required_libs="$(LD_LIBRARY_PATH="${LD_LIBRARY_PATH:+$LD_LIBRARY_PATH:}${LibDir_wP}" ldd "$ldd_target")"
        fi
        list_of_libs="$(echo "$1" | sed -e 's|\([^\]\)\.|\1\\.|g' -e 's|\[num\]|[0-9]\\+|g')"
        for var in $list_of_libs; do
            patterns="$(echo "$var" | sed -e 's|^ */||' -e 's|/| |g' -e 's/\(|[HL]\?\) /\1/')"
            wanted_lib="$(echo "$patterns" | awk '{print $1}')"; i=2
            if [ "$check_nr" -ne "1" ] && [ "$(echo "$IgnoredLibs" | grep "$wanted_lib")" != "" ]; then WantedLibIgnored="yes"
            else WantedLibIgnored="no"; fi
            if [ "$check_nr" -eq "1" ] || [ "$WantedLibIgnored" = "yes" ]; then
            wanted_lib2="$(echo "$wanted_lib" | sed 's/\\//g')"; patterns_num="$(echo "$patterns" | wc -w)"
            last_pattern="$(echo "$patterns" | awk '{print $(NF-1)}')"; end_mark="$(echo "$patterns" | awk '{print $NF}')"
            if [ "$check_nr" -ne "1" ]; then foreign_dep="(sd) "; else foreign_dep=""; fi
            if [ "$(echo "$c_required_libs" | grep "[^_-]$last_pattern")" != "" ]; then
                if [ "$WantedLibIgnored" = "yes" ]; then IgnoredLibs="$(echo "$IgnoredLibs" | sed "s|$wanted_lib||")"; fi
                for var2 in $patterns; do
                if [ "$var2" = "|" ] || [ "$var2" = "|H" ] || [ "$var2" = "|L" ]; then break; fi
                lib_pattern="$(echo "$var" | awk -F '/' -v ii="$i" '{print $ii}')"
                if [ "$wanted_lib" != "$last_pattern" ] && [ "$i" -lt "$patterns_num" ]; then end_w="no"
                    else end_w="yes"; fi
                findMissingSO "$wanted_lib" "$wanted_lib2" "$lib_pattern" "$end_w" "$foreign_dep" "$end_mark"
                if [ "$?" -eq "0" ]; then break; else i=$((i + 1)); fi
                done
            elif [ "$WantedLibIgnored" = "no" ]; then
                IgnoredLibs="$IgnoredLibs $wanted_lib2"
                if [ -L "$LibDir_wP/$wanted_lib2" ]; then rm -f "$LibDir_wP/$wanted_lib2"; fi
            fi
            fi
        done
        check_nr=$((check_nr + 1))
        done
    fi
    }
    # Each line /.../.../|/ .
    # First entry libs_list="...\n...\n..." ,
    # each next libs_list="$libs_list\n...\n..." .
    # First word should be original name, in any other you can use [0-9] what mean "any digit",
    # or [num] what mean "any number with one or more digits".
    # Last word should have length at least 6 chars, but use longer if string is too much common.

    # If line is ended by /|H/ this mean 'H'igher priority about finding local library instead of existing
    # (if exist) embedded version, useful in case if embedded library has a lot of own dependencies.
    # If line is ended by /|L/ this mean 'L'ower priority about finding local library instead of existing
    # (if exist) embedded version, (rarely) useful in case if embedded library may eliminate ABI-API
    # conflixts, e.g. binary <> WxWidgets.
    # ignored: linux-vdso.so*, linux-gate.so*, ld-linux-*.so*, libpthread.so*, libpulsecommon-*.so
    libs_list="/libSDL2-2.0.so.0/libSDL2-2.[num].so/libSDL2-[0-9].[num].so/libSDL2-/|H/
    /libGLU.so.1/libGLU.so/|/
    /libGL.so.1/libGL.so/|/
    /libX11.so.6/libX11.so/|/
    /libidn.so.11/libidn.so/|/
    /librtmp.so.0/librtmp.so/|/
    /libgcrypt.so.11/|/
    /libgnutls.so.26/|/
    /libgssapi_krb5.so.2/libgssapi_krb[0-9].so/libgssapi_krb[num].so/libgssapi_krb/|/
    /liblber-2.4.so.2/liblber-2.[num].so/liblber-[0-9].[num].so/liblber-/liblber/|/
    /libldap_r-2.4.so.2/libldap_r-2.[num].so/libldap_r-[0-9].[num].so/libldap_r-/libldap_r/|/
    /libz.so.1/libz.so/|/
    /libdl.so.2/libdl.so/|/
    /libfontconfig.so.1/libfontconfig.so/libfontconfig/|/
    /libfreetype.so.6/libfreetype.so/|/
    /libfribidi.so.0/libfribidi.so/libfribidi/|/
    /libm.so.6/libm.so/|/
    /libstdc++.so.6/libstdc++.so/|/
    /libgcc_s.so.1/libgcc_s.so/|/
    /libc.so.6/libc.so/|/
    /libasound.so.2/libasound.so/|/
    /libpulse-simple.so.0/libpulse-simple.so/libpulse-simple/|/
    /libpulse.so.0/libpulse.so/|/
    /libXext.so.6/libXext.so/libXext/|/
    /libXcursor.so.1/libXcursor.so/libXcursor/|/
    /libXinerama.so.1/libXinerama.so/libXinerama/|/
    /libXi.so.6/libXi.so/|/
    /libXrandr.so.2/libXrandr.so/libXrandr/|/
    /libXss.so.1/libXss.so/|/
    /libXxf86vm.so.1/libXxf86vm.so/libXxf86vm/|/
    /libwayland-egl.so.1/libwayland-egl.so/libwayland-egl/|/
    /libwayland-client.so.0/libwayland-client.so/libwayland-client/|/
    /libwayland-cursor.so.0/libwayland-cursor.so/libwayland-cursor/|/
    /libxkbcommon.so.0/libxkbcommon.so/libxkbcommon/|/
    /librt.so.1/librt.so/|/
    /libglapi.so.0/libglapi.so/libglapi/|/
    /libXdamage.so.1/libXdamage.so/libXdamage/|/
    /libXfixes.so.3/libXfixes.so/libXfixes/|/
    /libX11-xcb.so.1/libX11-xcb.so/libX11-xcb/|/
    /libxcb-glx.so.0/libxcb-glx.so/libxcb-glx/|/
    /libxcb-dri2.so.0/libxcb-dri2.so/libxcb-dri2/|/
    /libxcb-dri3.so.0/libxcb-dri3.so/libxcb-dri3/|/
    /libxcb-present.so.0/libxcb-present.so/libxcb-present/|/
    /libxcb-sync.so.1/libxcb-sync.so/libxcb-sync/|/
    /libxcb.so.1/libxcb.so/|/
    /libxshmfence.so.1/libxshmfence.so/libxshmfence/|/
    /libdrm.so.2/libdrm.so/|/
    /libgpg-error.so.0/libgpg-error.so/libgpg-error/|/
    /libtasn1.so.6/libtasn1.so/libtasn[num].so/libtasn[0-9]/libtasn/|/
    /libp11-kit.so.0/libp11-kit.so/libp11-kit/|/
    /libkrb5.so.3/libkrb[0-9].so.[num]/libkrb[num].so/|/
    /libk5crypto.so.3/libk[0-9]crypto.so/libk[num]crypto/|/
    /libcom_err.so.2/libcom_err.so/libcom_err/|/
    /libkrb5support.so.0/libkrb[0-9]support.so/libkrb[num]support/|/
    /libresolv.so.2/libresolv.so/|/
    /libsasl2.so.2/libsasl[0-9].so/libsasl/|/
    /libgssapi.so.3/libgssapi.so/|/
    /libexpat.so.1/libexpat.so/|/
    /libpng12.so.0/libpng1[0-9].so/libpng[num].so/libpng[0-9]/|/
    /libjson-c.so.2/libjson-c.so/libjson-c/|/
    /libdbus-1.so.3/libdbus-[0-9].so/libdbus-[num]/|/
    /libXrender.so.1/libXrender.so/libXrender/|/
    /libffi.so.6/libffi.so/|/
    /libXau.so.6/libXau.so/|/
    /libXdmcp.so.6/libXdmcp.so/libXdmcp/|/
    /libkeyutils.so.1/libkeyutils.so/libkeyutils/|/
    /libheimntlm.so.0/libheimntlm.so/libheimntlm/|/
    /libkrb5.so.26/libkrb[0-9].so.[num]/libkrb[num].so/|/
    /libasn1.so.8/libasn1.so/libasn[num].so/|/
    /libhcrypto.so.4/libhcrypto.so/libhcrypto/|/
    /libroken.so.18/libroken.so/libroken/|/
    /libwrap.so.0/libwrap.so/|/
    /libsndfile.so.1/libsndfile.so/|/
    /libasyncns.so.0/libasyncns.so/libasyncns/|/
    /libwind.so.0/libwind.so/|/
    /libheimbase.so.1/libheimbase.so/libheimbase/|/
    /libhx509.so.5/libhx509.so/libhx509/|/
    /libsqlite3.so.0/libsqlite3.so/|/
    /libcrypt.so.1/libcrypt.so/|/
    /libnsl.so.1/libnsl.so/|/
    /libFLAC.so.8/libFLAC.so/|/
    /libvorbisenc.so.2/libvorbisenc.so/libvorbisenc/|/
    /libvorbis.so.0/libvorbis.so/|/
    /libogg.so.0/libogg.so/|/
    /libGLEW.so.1.10/libGLEW.so.1.[num]/libGLEW.so/|/"
    if [ "$OperatingMode" = "--game" ]; then
    libs_list="$libs_list
    /libopenal.so.1/libopenal.so/libopenal/|H/
    /libvlccore.so.7/libvlccore.so/libvlccore/|/
    /libvlc.so.5/libvlc.so/|/"
    else
    libs_list="$libs_list
    /libwx_gtk2u_core-3.0.so.0/libwx_gtk2u_core-3.[num].so/|L/
    /libwx_baseu-3.0.so.0/libwx_baseu-3.[num].so/|L/
    /libwx_gtk2u_gl-3.0.so.0/libwx_gtk2u_gl-3.[num].so/|L/
    /libgtk-x11-2.0.so.0/libgtk-x11-2.[num].so/libgtk-x11-[0-9].[num].so/libgtk-x11-[0-9]/libgtk-x11/|/
    /libgdk-x11-2.0.so.0/libgdk-x11-2.[num].so/libgdk-x11-[0-9].[num].so/libgdk-x11-[0-9]/libgdk-x11/|/
    /libpangocairo-1.0.so.0/libpangocairo-1.[num].so/libpangocairo-[0-9].[num].so/libpangocairo-[0-9]/libpangocairo/|/
    /libgdk_pixbuf-2.0.so.0/libgdk_pixbuf-2.[num].so/libgdk_pixbuf-[0-9].[num].so/libgdk_pixbuf-[0-9]/|/
    /libcairo.so.2/libcairo.so/|/
    /libpango-1.0.so.0/libpango-1.[num].so/libpango-[0-9].[num].so/libpango-[0-9]/libpango-/|/
    /libgobject-2.0.so.0/libgobject-2.[num].so/libgobject-[0-9].[num].so/libgobject-[0-9]/libgobject/|/
    /libglib-2.0.so.0/libglib-2.[num].so/libglib-[0-9].[num].so/libglib-[0-9]/libglib-/|/
    /libSM.so.6/libSM.so/|/
    /libjpeg.so.8/|/
    /libtiff.so.5/libtiff.so/|/
    /libgmodule-2.0.so.0/libgmodule-2.[num].so/libgmodule-[0-9].[num].so/libgmodule-[0-9]/libgmodule/|/
    /libatk-1.0.so.0/libatk-1.[num].so/libatk-[0-9].[num].so/libatk-[0-9]/|/
    /libgio-2.0.so.0/libgio-2.[num].so/libgio-[0-9].[num].so/libgio-[0-9]/|/
    /libpangoft2-1.0.so.0/libpangoft2-1.[num].so/libpangoft2-[0-9]/libpangoft2-/libpangoft/|/
    /libXcomposite.so.1/libXcomposite.so/libXcomposite/|/
    /libpixman-1.so.0/libpixman-1.so/libpixman-[0-9].so/libpixman/|/
    /libxcb-shm.so.0/libxcb-shm.so/libxcb-shm/|/
    /libxcb-render.so.0/libxcb-render.so/|/
    /libthai.so.0/libthai.so/|/
    /libpcre.so.3/libpcre.so/|/
    /libICE.so.6/libICE.so/|/
    /libuuid.so.1/libuuid.so/|/
    /liblzma.so.5/liblzma.so/|/
    /libjbig.so.0/libjbig.so/|/
    /libselinux.so.1/libselinux.so/|/
    /libharfbuzz.so.0/libharfbuzz.so/|/
    /libdatrie.so.1/libdatrie.so/|/
    /libgraphite2.so.3/libgraphite2.so/libgraphite[0-9].so/|/"
    fi
    #if [ "$ARCHITECTURE" = "x86_64" ]; then
    #fi

    IfRealLibExist() {
    RealEmbLib="$LibDir_wP/$1"
    if [ ! -L "$RealEmbLib" ] && [ -e "$RealEmbLib" ]; then
        if [ "$2" = "remove" ]; then mv -f "$RealEmbLib" "$LibDirB_wP"; else return 0; fi
    else return 1; fi
    }
    # exceptions, keep them in sync with listed libraries on 'libs_list'
    if [ "$(echo "$OS_INFO" | grep 'Debian\|Ubuntu\|LinuxMint')" = "" ]; then IfRealLibExist "libSDL2-2.0.so.0" "remove"; fi
    if [ "$OperatingMode" = "--game" ]; then
    if [ "$(IfRealLibExist "libvlc.so.5"; echo "$?")" -ne "0" ] && [ "$(IfRealLibExist "libvlccore.so.7"; echo "$?")" -ne "0" ] && \
        [ -d "$LibDir_wP/vlc" ]; then mv -f "$LibDir_wP/vlc" "$LibDirB_wP"; fi
    else
        if [ "$(IfRealLibExist "libwx_gtk2u_core-3.0.so.0"; echo "$?")" -ne "0" ] || \
        [ "$(IfRealLibExist "libwx_baseu-3.0.so.0"; echo "$?")" -ne "0" ] || \
        [ "$(IfRealLibExist "libwx_gtk2u_gl-3.0.so.0"; echo "$?")" -ne "0" ]; then
            IfRealLibExist "libwx_gtk2u_core-3.0.so.0" "remove"
            IfRealLibExist "libwx_baseu-3.0.so.0" "remove"
            IfRealLibExist "libwx_gtk2u_gl-3.0.so.0" "remove"
        fi
    fi

    lw_log="$LibDir_wP/.last_w_check.log"; li_log="$LibDir_wP/.last_i_check.log"
    pw_log="$LibDir_wP/.previous_w_check.log"; pi_log="$LibDir_wP/.previous_i_check.log"
    pw_err="$LibDir_wP/.previous_err_d_check.log"
    LibsWarningsLogs() {
    if [ "$1" = "create_new" ]; then
        echo "#" > "$lw_log"; echo "#" > "$li_log"
    elif [ "$1" = "save_new" ] && [ "$OperatingMode" = "--game" ]; then
        echo "$LibsWarnings" | sed 's/^[\t ]*||/ ||/g' > "$lw_log"; echo "$IgnoredLibs" > "$li_log"
    elif [ "$1" = "read_old" ]; then
        LibsWarnings="$(cat "$pw_log")"; IgnoredLibs="$(cat "$pi_log")"
    elif [ "$1" = "check_old" ]; then
        if [ -f "$pw_log" ] && [ -f "$pi_log" ] && \
        [ "$(find "$LibDir_wP" -name '.previous_w_check.log' -mtime -29)" ]; then return 0
        else return 1; fi
    elif [ "$1" = "check_current" ] || [ "$1" = "rotate" ]; then
        if [ -f "$lw_log" ] && [ -f "$li_log" ]; then
        if [ "$1" = "rotate" ] || [ "$(find "$LibDir_wP" -name '.last_w_check.log' -mmin +10)" ]; then
            mv -f "$lw_log" "$pw_log"; mv -f "$li_log" "$pi_log"
            else return 0; fi
        elif [ "$1" != "rotate" ]; then return 1; fi
    fi
    }
    checkLibsStatus2() {
    if [ -f "$pw_err" ] && [ -d "$LibDirB_wP" ]; then rm -f "$pw_err"
        mv -f "$LibDirB_wP"/* "$LibDir_wP"; sleep 1s; fi
    checkLibsStatus "$libs_list"; LibsWarningsLogs "save_new"; sleep 1s; LibsWarningsLogs "rotate"
    }
    if [ "$OperatingMode" = "--game" ] && [ "$(LibsWarningsLogs "check_current"; echo "$?")" -eq "0" ]; then
    LibsWarningsLogs "read_old"
    elif [ "$OperatingMode" = "--game" ] && [ "$(LibsWarningsLogs "check_old"; echo "$?")" -eq "0" ]; then
    ( LibsWarningsLogs "create_new"; checkLibsStatus2 ) &
    LibsWarningsLogs "read_old"
    else
    checkLibsStatus2
    fi
    if [ -f "$GAMEDIR/${SHORT_GAME_NAME}-configure-desktop.sh" ]; then
    "$GAMEDIR/${SHORT_GAME_NAME}-configure-desktop.sh"
    rm -f "$GAMEDIR/${SHORT_GAME_NAME}-configure-desktop.sh"
    fi
else
    ExitStatus="$2"
    LibsWarnings="$3"
    BeginTime="$4"
    ErrorTime="$5"
    BinaryToCheck="$6"
fi
if [ "$OperatingMode" = "--game" ]; then
    if [ -d "$LibDir_wP" ]; then
    export LD_LIBRARY_PATH="${LD_LIBRARY_PATH:+$LD_LIBRARY_PATH:}${LibDir_wP}"
    if [ -d "$LibDir_wP/vlc" ]; then export VLC_PLUGIN_PATH="${LibDir_wP}/vlc"; fi
    fi
    "$GAMEDIR/$EXC_BINARY" "$@"
    ExitStatus="$?"

    BinaryToCheck="$EXC_BINARY"
    ErrorTime="$(date +"%s")"
fi
if [ "$ExitStatus" -ne "0" ] || [ "$OperatingMode" = "--tool-editor" ] || [ "$OperatingMode" = "--tool-g3dviewer" ]; then
    if [ "$(echo "$@" | grep '^[ \t]*--help[ \t]*$\|^[ \t]*-h[ \t]*$')" = "" ]; then
    DiffTime="$(($ErrorTime-$BeginTime))"
    if [ "$OperatingMode" = "--game" ]; then ShowLibsTime=35; else ShowLibsTime=45; fi
    if [ "$DiffTime" -le "$ShowLibsTime" ] && [ -e "$GAMEDIR/$BinaryToCheck" ] && [ -x "$GAMEDIR/$BinaryToCheck" ]; then
        if [ "$OperatingMode" = "--game" ] || [ "$OperatingMode" = "--tool-buginfo" ]; then
        echo "#" > "$pw_err"; LibsWarnings="
        || $LONG_GAME_NAME requires some libraries to be present on this operating system.
        || Missing libraries (if any) will be listed next, along with full names
        || and default versions. Use the package manager provided by your Linux
        || distribution to install them. Most Linux distributions provide a web search
        || allowing you to identify the packages containing these libraries.
        || Once you installed additional libraries, just run this script again.
        ||
        || Other issues preventing $LONG_GAME_NAME from starting can occur if
        || this version of game has not been tested with this version of your Linux
        || distribution. If so, we may provide compatibility updates which can be
        || downloaded and installed by running this script:
        ||    '${SHORT_GAME_NAME}-mini-update.sh'
        ||
        || If after this, $LONG_GAME_NAME still fails to start, please report a bug at
        ||    '$BugReportEngine'
        ||    or '$BugReportBoth'
        ||
        || OS info. [$OS_INFO], Arch.[$ARCHITECTURE], Dir.[$GAMEDIR]
        || $LibsWarnings"
        fi
        echo "$LibsWarnings" | sed 's/^[\t ]*||//g' >&2; echo
        if [ "$OperatingMode" != "--tool-buginfo" ]; then
        IgnoredLibs="$(echo "$IgnoredLibs" | sed -e 's/  */ /g' -e 's/^ //g')"
        if [ "$IgnoredLibs" = "" ]; then IgnoredLibs="-none-"; fi
        echo "Ignored libraries (please include in bug reports):" >&2
        echo "$IgnoredLibs" >&2; echo
        fi
    else
        BugReportInfo="$BugReportInfo
        || OS info. [$OS_INFO], Arch.[$ARCHITECTURE], Dir.[$GAMEDIR]
        ||
        || If you see an unknown error then please report a bug
        || here: '$BugReportBoth2',
        || or if you think that bug is related with game engine here:
        || '$BugReportEngine'"
        if [ "$BugReportData" != "" ] && [ "$BugReportData" != "$BugReportBoth2" ] && [ "$BugReportData" != "$BugReportEngine" ]; then
        BugReportInfo="$BugReportInfo,
        || or if you think that bug is related with game data here:
        || '$BugReportData'."
        else
        BugReportInfo="$BugReportInfo."
        fi
        echo "$BugReportInfo" | sed 's/^[\t ]*||//g' >&2; echo
    fi
    fi
fi
exit "$ExitStatus"
