#!/usr/local/bin/bash

# Extract CMake macros during configure
PREFIX="/usr/local"
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="mcstas"
VERSION="2.5"
MC="mc"
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
	echo Removing link $1
	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
        # 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
	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
# 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
