#!/bin/sh -f

makeAbsolute() {
    case $1 in
        /*)
            # already absolute, return it
            echo "$1"
            ;;
        *)
            # relative, prepend $2 made absolute
            echo `makeAbsolute "$2" "$PWD"`/"$1" | sed 's,/\.$,,'
            ;;
    esac
}

prog_name=""
print_help() {
    echo "script usage: $prog_name [-v] {gid_args....}"
}

me=`which "$0"` # Search $PATH if necessary
# if it's a symbolic link, get path of destination
if test -L "$me"; then
    # Try readlink(1)
    readlink=`type readlink 2>/dev/null` || readlink=
    if test -n "$readlink"; then
        # We have readlink(1), so we can use it. Assuming GNU readlink (for -f).
        me=`readlink -nf "$me"`
    else
        # No readlink(1), so let's try ls -l
        me=`ls -l "$me" | sed 's/^.*-> //'`
        base=`dirname "$me"`
        me=`makeAbsolute "$me" "$base"`
    fi
else
    #base=`dirname "$me"`
    base=""
    me=`makeAbsolute "$me" "$base"`
fi
# macOS does not like to have // in the middle of the path
me=`echo "$me" | sed -e "s/\/\//\//g"`

export BASEDIR="$(dirname "$me")"
export MY_STRINGS="$BASEDIR"/strings_static.exe

verbose=0
gid_help=0
prog_name=$0
option=$1
case $option in
    *-h*)
        print_help
        gid_help=1
        # shift
        # do not shift to pass '-h' to gid executable
        ;;
    -v)
        verbose=1
        shift
        ;;
esac

glx_capable=""
# if this script is called by another one 'run_as_administrator' then variable DISPLAY is not set
# surely this script is called as -tclsh interpreter
if [ "$DISPLAY" != "" ]; then
    xdpyinfo=""
    for donde in /usr/bin/X11/xdpyinfo /usr/X11/xdpyinfo /usr/bin/xdpyinfo; do
        if [ -x $donde ]; then
            xdpyinfo=$donde
            break
        fi
    done
    
    if [ "$xdpyinfo" = "" ]; then
        if [ $verbose -ne 0 ]; then
            echo "Warning: couldn't find xdpyinfo to determine GLX availability. "
        fi
        glx_capable=""
    else
        glx_capable=`$xdpyinfo | grep GLX`
    fi
fi

if [ $verbose -ne 0 ]; then
    echo "DISPLAY = $DISPLAY"
    echo "xdpyinfo: glx_capable = $glx_capable"
fi

# check preferences if SoftwareOpenGL is set

current_gid_version=$("$BASEDIR"/gidx -version)
my_preferences_file=$HOME/.gid/$current_gid_version/gid.ini
for file_preferences in $my_preferences_file $HOME/.gid/$current_gid_version/gid.ini $HOME/.gidDefaults ; do
    if [ $verbose -ne 0 ]; then
        echo "checking preference file $file_preferences"
    fi
    if [ -f $file_preferences ] ; then
        softOGL=`/bin/grep -s "SoftwareOpenGL *1" $file_preferences`
        if [ $verbose -ne 0 ]; then
            echo "    found $file_preferences"
            echo "    softOGL = \"$softOGL\""
        fi
        if [ "$softOGL" != "" ]; then
            glx_capable=""
        fi
        break
    fi
done

if [ $verbose -ne 0 ]; then
    echo "preferences: glx_capable = $glx_capable"
fi

# test if really glx is working !
if [ "$glx_capable" != "" ]; then
    glxinfo=""
    for donde in /usr/bin/glxinfo /usr/X11/glxinfo /usr/bin/X11/glxinfo; do
        if [ -x $donde ]; then
        glxinfo=$donde
        break
        fi
    done
    if [ $verbose -ne 0 ]; then
        echo "    glxinfo location = $glxinfo"
    fi

    if [ "$glxinfo" != "" ]; then
        $glxinfo 2> /dev/null 1> /dev/null
        # return code == 0 --> is ok, 1 --> glx error, 127 --> command not found
        error_code_glxinfo=$?
        if [ $verbose -ne 0 ]; then
            echo "    error_code_glxinfo = $error_code_glxinfo"
        fi
        if [ ! "$error_code_glxinfo" -eq "0" ] ; then
            glx_capable=""
            echo "ERROR: glx configuration is not valid. Check if driver installation is correct!"
            export GLX_ERROR=1
        fi
    fi
fi

export VTK_LIB_DIR="$BASEDIR"/scripts/vtk
export OBJARRAY_LIB_DIR="$BASEDIR"/scripts/objarray
export TLS_LIB_DIR="$BASEDIR"/scripts/tls
export TDBC_LIB_DIR="$BASEDIR"/lib/tdbc
export PYTHON_LIB_DIR="$BASEDIR"/scripts/tohil/python/x64/lib

# gid-python environment variables
export PYTHONHOME="$BASEDIR"/scripts/tohil/python
export PYTHONPATH="$PYTHONHOME"/bin:"$PYTHONHOME"/x64/bin:"$PYTHONHOME"/lib:"$PYTHONHOME"/x64/lib:"$PYTHONHOME"/lib/site-packages:"$PYTHONHOME"/x64/lib/:"$PYTHONHOME"/lib:"$PYTHONHOME"/x64/lib

if [ "$glx_capable" = "" ]; then
    if [ $verbose -ne 0 ]; then
        echo "Configuring GiD for Software Rendering"
    fi
    # export MESA_BACK_BUFFER=p
    export ADD_LD_LIBRARY_PATH="$BASEDIR"/lib/mesa1306:"$BASEDIR"/lib:"$VTK_LIB_DIR":"$OBJARRAY_LIB_DIR":"$TLS_LIB_DIR":"$PYTHON_LIB_DIR":"$TDBC_LIB_DIR"
else
    if [ $verbose -ne 0 ]; then
        echo "Configuring GiD for GLX Rendering"
    fi
    export ADD_LD_LIBRARY_PATH="$BASEDIR"/lib:"$OBJARRAY_LIB_DIR":"$VTK_LIB_DIR":"$TLS_LIB_DIR":"$PYTHON_LIB_DIR":"$TDBC_LIB_DIR"
fi

export GIDDEFAULT="$BASEDIR"
export GIDDEFAULTTCL="$GIDDEFAULT"/scripts
export GIDEXE="$BASEDIR"
# export TCL_LIBRARY=$GIDDEFAULTTCL
# export TK_LIBRARY=$GIDDEFAULTTCL

# Check if we've the correct version of glibc
glib_not_found=`ldd "$GIDEXE"/gid.exe 2>&1 | grep GLIBC|grep "not found"`
if [ "$glib_not_found" != "" ]; then
    export ADD_LD_LIBRARY_PATH="$ADD_LD_LIBRARY_PATH":"$BASEDIR"/lib/gcc_libs
fi

# Check if we've the correct version of stdc++
current_stdc=`ldd "$GIDEXE"/gid.exe 2>/dev/null | grep -w -o " /.*stdc++.*6 "`
system=`uname -p`
if [ "$system" = "aarch64" ]; then
    # on arm aarch64 looks for version 3.4.29
    proper_gcc=`"$MY_STRINGS" $current_stdc | grep GLIBCXX_3.4.29`
else
    proper_gcc=`"$MY_STRINGS" $current_stdc | grep GLIBCXX_3.4.26`
fi
if [ -z "$proper_gcc" ]; then
    export ADD_LD_LIBRARY_PATH="$ADD_LD_LIBRARY_PATH":"$BASEDIR"/lib/gcc_libs
fi

# Check if we've the correct version of nss for gdal.so
current_gdal_dep=`ldd "$GIDEXE"/lib/libgdal.so.31 2>&1 | grep -c -w -o "not found"`
if [ "$current_gdal_dep" != "0" ]; then
    export ADD_LD_LIBRARY_PATH="$ADD_LD_LIBRARY_PATH":"$BASEDIR"/lib/GDAL_DEPENDENCIES
fi

# Check if zstd is present ( needed by mesa, netcdf, gdal, ...)
zstd_present=`zstd --version 2> /dev/null`

if [ "$zstd_present" = "" ]; then
    # echo no zstd
    export ADD_LD_LIBRARY_PATH="$ADD_LD_LIBRARY_PATH":"$BASEDIR"/lib/zstd
fi

# trick, sometimes, although OLD_LD_LIBRARY_PATH_DEFINED was not defined, bash handles it
# as defined but with empty value...
if [ -z $OLD_LD_LIBRARY_PATH_DEFINED ]; then
    export OLD_LD_LIBRARY_PATH="$LD_LIBRARY_PATH"
    export OLD_LD_LIBRARY_PATH_DEFINED=1
fi

if [ "$LD_LIBRARY_PATH" != "" ]; then
    export LD_LIBRARY_PATH="$ADD_LD_LIBRARY_PATH":"$LD_LIBRARY_PATH"
else
    export LD_LIBRARY_PATH="$ADD_LD_LIBRARY_PATH"
fi

if [ $gid_help = 1 ] ; then
    echo "GiD executable options:"
fi
"$GIDEXE"/gid.exe "$@"
error_code_gid_exe=$?
if [ "$error_code_gid_exe" != "0" ]; then
    echo "GiD exited with error code = $error_code_gid_exe != 0"
    # echo "Starting GiD Safe Mode"
    # if [ "$softOGL" != "" ]; then
    #     mkdir -p $(dirname $my_preferences_file)
    #     echo "\nSoftwareOpenGL 1\n" >> $my_preferences_file
    # fi
    # "$BASEDIR"/gidx "$@"
    echo "To start GiD in Safe Mode use this command:"
    echo "    \"$BASEDIR/gidx\""
fi
