#!/usr/bin/env bash

# Extract CMake macros during configure
PREFIX="@CMAKE_INSTALL_PREFIX@"
UNAME=`uname -s`

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=`dirname $0`
    cd $PREFIX
    PREFIX=`pwd -P`
    cd -
    PREFIX=`dirname $PREFIX`
    PREFIX=`dirname $PREFIX`
    PREFIX=`dirname $PREFIX`
fi
if [ "$1" == "osx_app_default" ]; then
    # Hook to change which mcgui will run using the App bundle 
    if [[ ${UNAME} = Darwin* ]]; then
	PREFIX=`dirname $0`
	cd $PREFIX/../../../../MacOS/
	if [[ -x "./mccodegui.$2" ]]; then
	    cp mccodegui.$2 mccodegui
	    echo Set up your bundle to run the $2 version of mcgui
	fi
	cd -
	exit 0
    fi
fi

FLAVOR="@FLAVOR@"
VERSION="2.7.2"
MC="@MCCODE_PREFIX@"
NO_ALTERNATIVES="@NO_ALTERNATIVES@" 

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=${PREFIX}/${FLAVOR}/${VERSION}/bin:${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/@FLAVOR@
    checklinkrm /usr/local/bin/@FLAVOR@
        # MCPL tool
    checklinkrm /usr/bin/mcpltool
    checklinkrm /usr/local/bin/mcpltool  
        # mcrun variants
    checklinkrm /usr/bin/@MCCODE_PREFIX@run
    checklinkrm /usr/bin/@MCCODE_PREFIX@run.pl
    checklinkrm /usr/bin/@MCCODE_PREFIX@run-py
    checklinkrm /usr/local/bin/@MCCODE_PREFIX@run
    checklinkrm /usr/local/bin/@MCCODE_PREFIX@run.pl
    checklinkrm /usr/local/bin/@MCCODE_PREFIX@run-py
	# mcgui variants
    checklinkrm /usr/bin/@MCCODE_PREFIX@gui
    checklinkrm /usr/bin/@MCCODE_PREFIX@gui.pl
    checklinkrm /usr/bin/@MCCODE_PREFIX@gui-py
    checklinkrm /usr/local/bin/@MCCODE_PREFIX@gui
    checklinkrm /usr/local/bin/@MCCODE_PREFIX@gui.pl
    checklinkrm /usr/local/bin/@MCCODE_PREFIX@gui-py
	# mcplot 
    checklinkrm /usr/bin/@MCCODE_PREFIX@plot
    checklinkrm /usr/bin/@MCCODE_PREFIX@plot.pl
    checklinkrm /usr/bin/@MCCODE_PREFIX@plot-chaco-py
    checklinkrm /usr/bin/@MCCODE_PREFIX@plot-chaco
    checklinkrm /usr/bin/@MCCODE_PREFIX@plot-matplotlib-py
    checklinkrm /usr/bin/@MCCODE_PREFIX@plot-matplotlib
    checklinkrm /usr/bin/@MCCODE_PREFIX@plot-matlab
    checklinkrm /usr/bin/@MCCODE_PREFIX@plot-pyqtgraph-py
    checklinkrm /usr/bin/@MCCODE_PREFIX@plot-pyqtgraph
    checklinkrm /usr/local/bin/@MCCODE_PREFIX@plot
    checklinkrm /usr/local/bin/@MCCODE_PREFIX@plot.pl
    checklinkrm /usr/local/bin/@MCCODE_PREFIX@plot-chaco-py
    checklinkrm /usr/local/bin/@MCCODE_PREFIX@plot-chaco
    checklinkrm /usr/local/bin/@MCCODE_PREFIX@plot-matplotlib-py
    checklinkrm /usr/local/bin/@MCCODE_PREFIX@plot-matplotlib
    checklinkrm /usr/local/bin/@MCCODE_PREFIX@plot-matlab
    checklinkrm /usr/local/bin/@MCCODE_PREFIX@plot-pyqtgraph-py
    checklinkrm /usr/local/bin/@MCCODE_PREFIX@plot-pyqtgraph
	# mcdisplay
    checklinkrm /usr/bin/@MCCODE_PREFIX@display
    checklinkrm /usr/bin/@MCCODE_PREFIX@display.pl
    checklinkrm /usr/bin/@MCCODE_PREFIX@display-py
    checklinkrm /usr/bin/@MCCODE_PREFIX@display-x3d-py
    checklinkrm /usr/bin/@MCCODE_PREFIX@display-x3d
    checklinkrm /usr/bin/@MCCODE_PREFIX@display-matplotlib-py
    checklinkrm /usr/bin/@MCCODE_PREFIX@display-matplotlib
    checklinkrm /usr/bin/@MCCODE_PREFIX@display-vtk-py
    checklinkrm /usr/bin/@MCCODE_PREFIX@display-vtk
    checklinkrm /usr/bin/@MCCODE_PREFIX@display-webgl-py
    checklinkrm /usr/bin/@MCCODE_PREFIX@display-webgl
    checklinkrm /usr/local/bin/@MCCODE_PREFIX@display
    checklinkrm /usr/local/bin/@MCCODE_PREFIX@display.pl
    checklinkrm /usr/local/bin/@MCCODE_PREFIX@display-py
    checklinkrm /usr/local/bin/@MCCODE_PREFIX@display-x3d-py
    checklinkrm /usr/local/bin/@MCCODE_PREFIX@display-x3d
    checklinkrm /usr/local/bin/@MCCODE_PREFIX@display-matplotlib-py
    checklinkrm /usr/local/bin/@MCCODE_PREFIX@display-matplotlib
    checklinkrm /usr/local/bin/@MCCODE_PREFIX@display-vtk-py
    checklinkrm /usr/local/bin/@MCCODE_PREFIX@display-vtk
    checklinkrm /usr/local/bin/@MCCODE_PREFIX@display-webgl-py
    checklinkrm /usr/local/bin/@MCCODE_PREFIX@display-webgl
    checklinkrm /usr/local/bin/@MCCODE_PREFIX@display-pyqtgraph-py
    checklinkrm /usr/local/bin/@MCCODE_PREFIX@display-pyqtgraph
        # other stuff
    checklinkrm /usr/bin/@MCCODE_PREFIX@daemon
    checklinkrm /usr/bin/@MCCODE_PREFIX@daemon.pl
    checklinkrm /usr/bin/@MCCODE_PREFIX@doc
    checklinkrm /usr/bin/@MCCODE_PREFIX@doc.pl
    checklinkrm /usr/bin/@MCCODE_PREFIX@format
    checklinkrm /usr/bin/@MCCODE_PREFIX@format.pl
    checklinkrm /usr/bin/@MCCODE_PREFIX@formatgui
    checklinkrm /usr/bin/@MCCODE_PREFIX@formatgui.pl
    checklinkrm /usr/bin/@MCCODE_PREFIX@resplot
    checklinkrm /usr/bin/@MCCODE_PREFIX@resplot.pl
    checklinkrm /usr/bin/@FLAVOR@tovitess
    checklinkrm /usr/bin/@FLAVOR@tovitess.pl
    checklinkrm /usr/local/bin/@MCCODE_PREFIX@daemon
    checklinkrm /usr/local/bin/@MCCODE_PREFIX@daemon.pl
    checklinkrm /usr/local/bin/@MCCODE_PREFIX@doc
    checklinkrm /usr/local/bin/@MCCODE_PREFIX@doc.pl
    checklinkrm /usr/local/bin/@MCCODE_PREFIX@format
    checklinkrm /usr/local/bin/@MCCODE_PREFIX@format.pl
    checklinkrm /usr/local/bin/@MCCODE_PREFIX@formatgui
    checklinkrm /usr/local/bin/@MCCODE_PREFIX@formatgui.pl
    checklinkrm /usr/local/bin/@MCCODE_PREFIX@resplot
    checklinkrm /usr/local/bin/@MCCODE_PREFIX@resplot.pl
    checklinkrm /usr/local/bin/@FLAVOR@tovitess
    checklinkrm /usr/local/bin/@FLAVOR@tovitess.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
	cp ${PREFIX}/${FLAVOR}/${VERSION}/module /etc/modulefiles/${FLAVOR}/${VERSION}
    else
	if [ -e ${RPM_INSTALL_PREFIX}/${FLAVOR}/${VERSION}/module ];
	then
	    sed -i.bak s+/usr/local+${RPM_INSTALL_PREFIX}+g ${RPM_INSTALL_PREFIX}/${FLAVOR}/${VERSION}/module
	    cp ${RPM_INSTALL_PREFIX}/${FLAVOR}/${VERSION}/module /etc/modulefiles/${FLAVOR}/${VERSION}
	fi
	if [ ${RPM_INSTALL_PREFIX} == /usr/local ];
	then
	    ln -sf ${RPM_INSTALL_PREFIX}/${FLAVOR}/${VERSION}/bin/* /usr/local/bin/
	fi
    fi
fi

# Look for relevant locations of gtksourceview folder

if [ -f  ${PREFIX}/${FLAVOR}/${VERSION}/editors/mccode.lang ];
then
    # Recent GTK on Linux
    if [ -d /usr/share/gtksourceview-4/language-specs/ ];
    then
	cp ${PREFIX}/${FLAVOR}/${VERSION}/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}/${FLAVOR}/${VERSION}/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}/${FLAVOR}/${VERSION}/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}/${FLAVOR}/${VERSION}/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"
    APPTARGET="/usr/share/applications"
    LOGOTARGET="/usr/share/pixmaps"
    checklinks
elif [[ ${UNAME} = Darwin* || ${PREFIX} = /Applications* ]]
then
    # Looks like a Mac - dump links in /usr/bin
    BINTARGET="/usr/local/bin"
    APPTARGET="none"
    LOGOTARGET="none"
    # 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/${FLAVOR}/${VERSION} ]; then
	    ln -sf ${PREFIX}/${FLAVOR}/${VERSION} /usr/local/${FLAVOR}/${VERSION}
	fi
    fi
    checklinks
else
    # OK - nothing clever then
    BINTARGET=${PREFIX}/bin
    APPTARGET="/usr/share/applications"
    LOGOTARGET="/usr/share/pixmaps"
    checklinks
fi
if [ -d ${BINTARGET} ];
then
    cd ${BINTARGET}
    ln -sf ${PREFIX}/${FLAVOR}/${VERSION}/bin/* .
    ln -sf ${PREFIX}/${FLAVOR}/${VERSION}/environment ${FLAVOR}-${VERSION}-environment
else
    echo "Sorry, your BINTARGET folder \"${BINTARGET}\" does not exist, dropping link creation"
fi
if [ -d ${APPTARGET} ];
then
    cd ${APPTARGET}
    ln -sf ${PREFIX}/${FLAVOR}/${VERSION}/launchers/*.desktop .
else
    echo "Sorry, your APPTARGET folder \"${APPTARGET}\" does not exist, dropping link creation"
fi
if [ -d ${LOGOTARGET} ];
then
    cd ${LOGOTARGET}
    ln -sf ${PREFIX}/${FLAVOR}/${VERSION}/launchers/*.png .
else
    echo "Sorry, your LOGOTARGET folder \"${LOGOTARGET}\" does not exist, dropping link creation"
fi

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