#!/bin/bash

# Extract CMake macros during configure
PREFIX="/usr/local"
FLAVOR="mcstas"
VERSION="2.9999-svn"
MC="mc"
NO_ALTERNATIVES=""

# 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
        # mcrun variants
    checklinkrm /usr/bin/mcrun
    checklinkrm /usr/bin/mcrun-py
    checklinkrm /usr/local/bin/mcrun
    checklinkrm /usr/local/bin/mcrun-py
	# mcgui variants
    checklinkrm /usr/bin/mcgui
    checklinkrm /usr/bin/mcgui-py
    checklinkrm /usr/local/bin/mcgui
    checklinkrm /usr/local/bin/mcgui-py
	# mcplot 
    checklinkrm /usr/bin/mcplot
    checklinkrm /usr/bin/mcplot-chaco-py
    checklinkrm /usr/bin/mcplot-matplotlib-py
    checklinkrm /usr/bin/mcplot-matlab
    checklinkrm /usr/local/bin/mcplot
    checklinkrm /usr/local/bin/mcplot-chaco-py
    checklinkrm /usr/local/bin/mcplot-matplotlib-py
    checklinkrm /usr/local/bin/mcplot-matlab
	# mcdisplay
    checklinkrm /usr/bin/mcdisplay
    checklinkrm /usr/bin/mcdisplay-py
    checklinkrm /usr/bin/mcdisplay-x3d-py
    checklinkrm /usr/bin/mcdisplay-matplotlib-py
    checklinkrm /usr/bin/mcdisplay-vtk-py
    checklinkrm /usr/local/bin/mcdisplay
    checklinkrm /usr/local/bin/mcdisplay-py
    checklinkrm /usr/local/bin/mcdisplay-x3d-py
    checklinkrm /usr/local/bin/mcdisplay-matplotlib-py
    checklinkrm /usr/local/bin/mcdisplay-vtk-py
        # other stuff
    checklinkrm /usr/bin/mcdaemon
    checklinkrm /usr/bin/mcdoc
    checklinkrm /usr/bin/mcformat
    checklinkrm /usr/bin/mcformatgui
    checklinkrm /usr/bin/mcresplot
    checklinkrm /usr/bin/mcstastovitess
    checklinkrm /usr/local/bin/mcdaemon
    checklinkrm /usr/local/bin/mcdoc
    checklinkrm /usr/local/bin/mcformat
    checklinkrm /usr/local/bin/mcformatgui
    checklinkrm /usr/local/bin/mcresplot
    checklinkrm /usr/local/bin/mcstastovitess
}


# 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 [[ ${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 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 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 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 --text &>/dev/null
fi
