#!/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/mcstas/tools"
MCCODE_LIBDIR="${MCCODE_BINDIR}/../lib"
MCCODE_RESOURCEDIR="${MCCODE_BINDIR}/../share/mcstas/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="mcstas"
VERSION="3.5.39_nightly"
MC="mc"

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/mcstas
    checklinkrm /usr/local/bin/mcstas
    checklinkrm /usr/bin/mcstas-pygen
    checklinkrm /usr/local/bin/mcstas-pygen
        # MCPL tool
    checklinkrm /usr/bin/mcpltool
    checklinkrm /usr/local/bin/mcpltool  
        # mcrun variants
    checklinkrm /usr/bin/mcrun
    checklinkrm /usr/bin/mcrun.pl
    checklinkrm /usr/bin/mcrun-py
    checklinkrm /usr/local/bin/mcrun
    checklinkrm /usr/local/bin/mcrun.pl
    checklinkrm /usr/local/bin/mcrun-py
	# mcgui variants
    checklinkrm /usr/bin/mcgui
    checklinkrm /usr/bin/mcgui.pl
    checklinkrm /usr/bin/mcgui-py
    checklinkrm /usr/local/bin/mcgui
    checklinkrm /usr/local/bin/mcgui.pl
    checklinkrm /usr/local/bin/mcgui-py
	# mcplot 
    checklinkrm /usr/bin/mcplot
    checklinkrm /usr/bin/mcplot.pl
    checklinkrm /usr/bin/mcplot-chaco-py
    checklinkrm /usr/bin/mcplot-chaco
    checklinkrm /usr/bin/mcplot-matplotlib-py
    checklinkrm /usr/bin/mcplot-matplotlib
    checklinkrm /usr/bin/mcplot-matlab
    checklinkrm /usr/bin/mcplot-pyqtgraph-py
    checklinkrm /usr/bin/mcplot-pyqtgraph
    checklinkrm /usr/local/bin/mcplot
    checklinkrm /usr/local/bin/mcplot.pl
    checklinkrm /usr/local/bin/mcplot-chaco-py
    checklinkrm /usr/local/bin/mcplot-chaco
    checklinkrm /usr/local/bin/mcplot-matplotlib-py
    checklinkrm /usr/local/bin/mcplot-matplotlib
    checklinkrm /usr/local/bin/mcplot-matlab
    checklinkrm /usr/local/bin/mcplot-pyqtgraph-py
    checklinkrm /usr/local/bin/mcplot-pyqtgraph
	# mcdisplay
    checklinkrm /usr/bin/mcdisplay
    checklinkrm /usr/bin/mcdisplay.pl
    checklinkrm /usr/bin/mcdisplay-py
    checklinkrm /usr/bin/mcdisplay-x3d-py
    checklinkrm /usr/bin/mcdisplay-x3d
    checklinkrm /usr/bin/mcdisplay-matplotlib-py
    checklinkrm /usr/bin/mcdisplay-matplotlib
    checklinkrm /usr/bin/mcdisplay-vtk-py
    checklinkrm /usr/bin/mcdisplay-vtk
    checklinkrm /usr/bin/mcdisplay-webgl-py
    checklinkrm /usr/bin/mcdisplay-webgl
    checklinkrm /usr/local/bin/mcdisplay
    checklinkrm /usr/local/bin/mcdisplay.pl
    checklinkrm /usr/local/bin/mcdisplay-py
    checklinkrm /usr/local/bin/mcdisplay-x3d-py
    checklinkrm /usr/local/bin/mcdisplay-x3d
    checklinkrm /usr/local/bin/mcdisplay-matplotlib-py
    checklinkrm /usr/local/bin/mcdisplay-matplotlib
    checklinkrm /usr/local/bin/mcdisplay-vtk-py
    checklinkrm /usr/local/bin/mcdisplay-vtk
    checklinkrm /usr/local/bin/mcdisplay-webgl-py
    checklinkrm /usr/local/bin/mcdisplay-webgl
    checklinkrm /usr/local/bin/mcdisplay-pyqtgraph-py
    checklinkrm /usr/local/bin/mcdisplay-pyqtgraph
        # other stuff
    checklinkrm /usr/bin/mcdaemon
    checklinkrm /usr/bin/mcdaemon.pl
    checklinkrm /usr/bin/mcdoc
    checklinkrm /usr/bin/mcdoc.pl
    checklinkrm /usr/bin/mcformat
    checklinkrm /usr/bin/mcformat.pl
    checklinkrm /usr/bin/mcformatgui
    checklinkrm /usr/bin/mcformatgui.pl
    checklinkrm /usr/bin/mcresplot
    checklinkrm /usr/bin/mcresplot.pl
    checklinkrm /usr/bin/mcstastovitess
    checklinkrm /usr/bin/mcstastovitess.pl
    checklinkrm /usr/local/bin/mcdaemon
    checklinkrm /usr/local/bin/mcdaemon.pl
    checklinkrm /usr/local/bin/mcdoc
    checklinkrm /usr/local/bin/mcdoc.pl
    checklinkrm /usr/local/bin/mcformat
    checklinkrm /usr/local/bin/mcformat.pl
    checklinkrm /usr/local/bin/mcformatgui
    checklinkrm /usr/local/bin/mcformatgui.pl
    checklinkrm /usr/local/bin/mcresplot
    checklinkrm /usr/local/bin/mcresplot.pl
    checklinkrm /usr/local/bin/mcstastovitess
    checklinkrm /usr/local/bin/mcstastovitess.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/mcstas/resources/${FLAVOR}-module ]]; then
	  cp ${PREFIX}/share/mcstas/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/mcstas/resources/${FLAVOR}-module ];
	then
	    sed -i.bak s+/usr/local+${RPM_INSTALL_PREFIX}+g ${RPM_INSTALL_PREFIX}/share/mcstas/resources/${FLAVOR}-module
	    cp ${RPM_INSTALL_PREFIX}/share/mcstas/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/mcstas/editors/mccode.lang ];
then
    # Recent GTK on Linux
    if [ -d /usr/share/gtksourceview-4/language-specs/ ];
    then
	cp ${PREFIX}/share/mcstas/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/mcstas/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/mcstas/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/mcstas/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/mcstas/resources ]; then
	    ln -sf ${PREFIX}/share/mcstas/resources /usr/local/share/mcstas/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
