#!/usr/bin/env bash

# Extract CMake macros during configure
UNAME=`uname -s`

############################################
# Start of standard CMake-generated preamble
set -e
FILE=${0}
# First, probe if we are calling script at
# install-time (postinst/postrm) or we are 
# running in an installation "userland"
SCRIPT=`basename $FILE`
if [[ ${SCRIPT} == *.postinst || ${SCRIPT} == *.postrm || ${SCRIPT} == *rpm-tmp* ]];
  then
  MCCODE_BINDIR=/usr/bin
else
  readlinkf(){ python3 -c 'import sys,pathlib
print(pathlib.Path(sys.argv[1]).resolve().absolute())' "$1"
  }
  LINK=$(readlinkf ${FILE}||true)
  if [ "x${LINK}" != "x" ]
 then
    FILE=${LINK}
  fi
  MCCODE_BINDIR="$( cd -P "$( dirname "${FILE}" )" && pwd )"
fi
MCCODE_TOOLDIR="${MCCODE_BINDIR}/../share/mcxtrace/tools"
MCCODE_LIBDIR="${MCCODE_BINDIR}/../lib"
MCCODE_RESOURCEDIR="${MCCODE_BINDIR}/../share/mcxtrace/resources"
if [ -d "${MCCODE_TOOLDIR}" ]
 then
    MCCODE_TOOLDIR="$( cd -P "${MCCODE_TOOLDIR}" && pwd )"
else
    MCCODE_TOOLDIR=""
fi
if [ -d "${MCCODE_LIBDIR}" ]
 then
    MCCODE_LIBDIR="$( cd -P "${MCCODE_LIBDIR}" && pwd )"
else
    MCCODE_LIBDIR=""
fi
if [ -d "${MCCODE_RESOURCEDIR}" ]
 then
    MCCODE_RESOURCEDIR="$( cd -P "${MCCODE_RESOURCEDIR}" && pwd )"
else
    MCCODE_RESOURCEDIR=""
fi
# End of standard preamble
############################################
export PATH=${MCCODE_BINDIR}:${PATH}

PREFIX="/usr"

if [ "$1" == "set_mccode_default" ]; then
    # If we are being called with the input parameter default, this is a call
    # for post-configuration using mcgui and the PREFIX value should be
    # defined from the location of this script
    PREFIX=${MCCODE_RESOURCEDIR}
fi

FLAVOR="mcxtrace"
VERSION="v3.5.39"
MC="mx"

echo "Post-install configuring $FLAVOR v. $VERSION on $UNAME."
echo "PREFIX was set to $PREFIX"
# Include installation path before any attempt to postconfig
export PATH=${MCCODE_BINDIR}:${PATH}
MCCODE=${FLAVOR}

# Function to check if link exists, then remove it
function checklinkrm {
    if [ -h $1 ]; then
	rm $1
    fi
}

# Function probing / removing old links
function checklinks {
    # - Check for possibly existing links in /usr/bin or /usr/local/bin and remove them...
        # McCode itself
    checklinkrm /usr/bin/mcxtrace
    checklinkrm /usr/local/bin/mcxtrace
    checklinkrm /usr/bin/mcxtrace-pygen
    checklinkrm /usr/local/bin/mcxtrace-pygen
        # MCPL tool
    checklinkrm /usr/bin/mcpltool
    checklinkrm /usr/local/bin/mcpltool  
        # mcrun variants
    checklinkrm /usr/bin/mxrun
    checklinkrm /usr/bin/mxrun.pl
    checklinkrm /usr/bin/mxrun-py
    checklinkrm /usr/local/bin/mxrun
    checklinkrm /usr/local/bin/mxrun.pl
    checklinkrm /usr/local/bin/mxrun-py
	# mcgui variants
    checklinkrm /usr/bin/mxgui
    checklinkrm /usr/bin/mxgui.pl
    checklinkrm /usr/bin/mxgui-py
    checklinkrm /usr/local/bin/mxgui
    checklinkrm /usr/local/bin/mxgui.pl
    checklinkrm /usr/local/bin/mxgui-py
	# mcplot 
    checklinkrm /usr/bin/mxplot
    checklinkrm /usr/bin/mxplot.pl
    checklinkrm /usr/bin/mxplot-chaco-py
    checklinkrm /usr/bin/mxplot-chaco
    checklinkrm /usr/bin/mxplot-matplotlib-py
    checklinkrm /usr/bin/mxplot-matplotlib
    checklinkrm /usr/bin/mxplot-matlab
    checklinkrm /usr/bin/mxplot-pyqtgraph-py
    checklinkrm /usr/bin/mxplot-pyqtgraph
    checklinkrm /usr/local/bin/mxplot
    checklinkrm /usr/local/bin/mxplot.pl
    checklinkrm /usr/local/bin/mxplot-chaco-py
    checklinkrm /usr/local/bin/mxplot-chaco
    checklinkrm /usr/local/bin/mxplot-matplotlib-py
    checklinkrm /usr/local/bin/mxplot-matplotlib
    checklinkrm /usr/local/bin/mxplot-matlab
    checklinkrm /usr/local/bin/mxplot-pyqtgraph-py
    checklinkrm /usr/local/bin/mxplot-pyqtgraph
	# mcdisplay
    checklinkrm /usr/bin/mxdisplay
    checklinkrm /usr/bin/mxdisplay.pl
    checklinkrm /usr/bin/mxdisplay-py
    checklinkrm /usr/bin/mxdisplay-x3d-py
    checklinkrm /usr/bin/mxdisplay-x3d
    checklinkrm /usr/bin/mxdisplay-matplotlib-py
    checklinkrm /usr/bin/mxdisplay-matplotlib
    checklinkrm /usr/bin/mxdisplay-vtk-py
    checklinkrm /usr/bin/mxdisplay-vtk
    checklinkrm /usr/bin/mxdisplay-webgl-py
    checklinkrm /usr/bin/mxdisplay-webgl
    checklinkrm /usr/local/bin/mxdisplay
    checklinkrm /usr/local/bin/mxdisplay.pl
    checklinkrm /usr/local/bin/mxdisplay-py
    checklinkrm /usr/local/bin/mxdisplay-x3d-py
    checklinkrm /usr/local/bin/mxdisplay-x3d
    checklinkrm /usr/local/bin/mxdisplay-matplotlib-py
    checklinkrm /usr/local/bin/mxdisplay-matplotlib
    checklinkrm /usr/local/bin/mxdisplay-vtk-py
    checklinkrm /usr/local/bin/mxdisplay-vtk
    checklinkrm /usr/local/bin/mxdisplay-webgl-py
    checklinkrm /usr/local/bin/mxdisplay-webgl
    checklinkrm /usr/local/bin/mxdisplay-pyqtgraph-py
    checklinkrm /usr/local/bin/mxdisplay-pyqtgraph
        # other stuff
    checklinkrm /usr/bin/mxdaemon
    checklinkrm /usr/bin/mxdaemon.pl
    checklinkrm /usr/bin/mxdoc
    checklinkrm /usr/bin/mxdoc.pl
    checklinkrm /usr/bin/mxformat
    checklinkrm /usr/bin/mxformat.pl
    checklinkrm /usr/bin/mxformatgui
    checklinkrm /usr/bin/mxformatgui.pl
    checklinkrm /usr/bin/mxresplot
    checklinkrm /usr/bin/mxresplot.pl
    checklinkrm /usr/bin/mcxtracetovitess
    checklinkrm /usr/bin/mcxtracetovitess.pl
    checklinkrm /usr/local/bin/mxdaemon
    checklinkrm /usr/local/bin/mxdaemon.pl
    checklinkrm /usr/local/bin/mxdoc
    checklinkrm /usr/local/bin/mxdoc.pl
    checklinkrm /usr/local/bin/mxformat
    checklinkrm /usr/local/bin/mxformat.pl
    checklinkrm /usr/local/bin/mxformatgui
    checklinkrm /usr/local/bin/mxformatgui.pl
    checklinkrm /usr/local/bin/mxresplot
    checklinkrm /usr/local/bin/mxresplot.pl
    checklinkrm /usr/local/bin/mcxtracetovitess
    checklinkrm /usr/local/bin/mcxtracetovitess.pl
}


# If this Unix has "modulefiles" installed in /etc, dump environment config there,
# else a simple set of links will do...
if [ -d /etc/modulefiles ]; 
then
    mkdir -p /etc/modulefiles/${FLAVOR}
    # Is this RPM-postinstall, potentially relocated?
    if [[ -z "$RPM_INSTALL_PREFIX" ]]; 
    then
	if [[ -f ${PREFIX}/share/mcxtrace/resources/${FLAVOR}-module ]]; then
	  cp ${PREFIX}/share/mcxtrace/resources/${FLAVOR}-module /etc/modulefiles/${FLAVOR}/${VERSION}
	elif  [[ -f ${MCCODE_RESOURCEDIR}/${FLAVOR}-module ]]; then
	  cp ${MCCODE_RESOURCEDIR}/${FLAVOR}-module /etc/modulefiles/${FLAVOR}/${VERSION}
	fi
    else
	if [ -f ${RPM_INSTALL_PREFIX}/share/mcxtrace/resources/${FLAVOR}-module ];
	then
	    sed -i.bak s+/usr/local+${RPM_INSTALL_PREFIX}+g ${RPM_INSTALL_PREFIX}/share/mcxtrace/resources/${FLAVOR}-module
	    cp ${RPM_INSTALL_PREFIX}/share/mcxtrace/resources/${FLAVOR}-module /etc/modulefiles/${FLAVOR}/${VERSION}
	fi
	if [ ${RPM_INSTALL_PREFIX} == /usr/local ];
	then
	    ln -sf ${RPM_INSTALL_PREFIX}/bin/* /usr/local/bin/
	fi
    fi
fi

# Look for relevant locations of gtksourceview folder
if [ -f  ${PREFIX}/share/mcxtrace/editors/mccode.lang ];
then
    # Recent GTK on Linux
    if [ -d /usr/share/gtksourceview-4/language-specs/ ];
    then
	cp ${PREFIX}/share/mcxtrace/editors/mccode.lang  /usr/share/gtksourceview-4/language-specs/
    fi
    # Slighly older GTK on Linux
    if [ -d /usr/share/gtksourceview-3/language-specs/ ];
    then
	cp ${PREFIX}/share/mcxtrace/editors/mccode.lang  /usr/share/gtksourceview-3/language-specs/
    fi
    # homebrew Arm on mac
    if [ -d /opt/homebrew/share/gtksourceview-4/language-specs/ ];
    then
	cp ${PREFIX}/share/mcxtrace/editors/mccode.lang  /opt/homebrew/share/gtksourceview-4/language-specs/
    fi
    # homebrew Intel on mac
    if [ -d /usr/local/share/gtksourceview-4/language-specs/ ];
    then
	cp ${PREFIX}/share/mcxtrace/editors/mccode.lang  /usr/local/share/gtksourceview-4/language-specs/
    fi
fi


# Try to figure out if this is a Debian package, a Mac OS X bundle or a generic unix...
if [[ ${PREFIX} = /usr/share* ]]; 
then
    # Looks like a Debian package - should really be handled via update-alternatives...
    BINTARGET="/usr/bin"
    checklinks
elif [[ ${UNAME} = Darwin* || ${PREFIX} = /Applications* ]]
then
    # Looks like a Mac - dump links in /usr/local/bin
    BINTARGET="/usr/local/bin"
    # Also drop a link to the mccode "system folder" as it may otherwise be hard to find for "expert users"
    mkdir -p /usr/local/${FLAVOR}
    # - Should not be done if called from the 'app bundle' installer - and only once
    if [[ ${PREFIX} = */Contents/Resources ]]; then
	if [ ! -h /usr/local/share/mcxtrace/resources ]; then
	    ln -sf ${PREFIX}/share/mcxtrace/resources /usr/local/share/mcxtrace/resources
	fi
    fi
    checklinks
else
    # OK - nothing clever then
    BINTARGET=${PREFIX}/bin
    checklinks
fi
if [ -d ${BINTARGET} ];
then
    if [[ ${BINTARGET} != ${MCCODE_BINDIR} ]]; then
      for i in ${MCCODE_BINDIR}/*; do
        if [[ ! -f ${BINTARGET}/$i ]]; then
          ln -sf ${MCCODE_BINDIR}/$i ${BINTARGET}/$i
        fi
      done
    fi
    if [[ -f ${MCCODE_RESOURCEDIR}/${FLAVOR}-environment ]]; then
      ln -sf ${MCCODE_RESOURCEDIR}/${FLAVOR}-environment ${BINTARGET}/${FLAVOR}-${VERSION}-environment
    elif [[ -f ${MCCODE_RESOURCEDIR}/../${FLAVOR}-environment ]]; then
      ln -sf ${MCCODE_RESOURCEDIR}/../${FLAVOR}-environment ${BINTARGET}/${FLAVOR}-${VERSION}-environment
    fi
else
    echo "Sorry, your BINTARGET folder \"${BINTARGET}\" does not exist, dropping link creation"
fi

# Check if mcdoc is installed and if it needs to run
if [ -x ${MCCODE_BINDIR}/${MC}doc ] && \
    [ -d ${MCCODE_RESOURCEDIR}/examples ]; then
    ${MCCODE_BINDIR}/${MC}doc -i &>/dev/null
fi
