#!/bin/bash

################################################################################
##                                                                            ##
##  This file is part of NCrystal (see https://mctools.github.io/ncrystal/)   ##
##                                                                            ##
##  Copyright 2015-2021 NCrystal developers                                   ##
##                                                                            ##
##  Licensed under the Apache License, Version 2.0 (the "License");           ##
##  you may not use this file except in compliance with the License.          ##
##  You may obtain a copy of the License at                                   ##
##                                                                            ##
##      http://www.apache.org/licenses/LICENSE-2.0                            ##
##                                                                            ##
##  Unless required by applicable law or agreed to in writing, software       ##
##  distributed under the License is distributed on an "AS IS" BASIS,         ##
##  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  ##
##  See the License for the specific language governing permissions and       ##
##  limitations under the License.                                            ##
##                                                                            ##
################################################################################

set -e
set -u

if [ -f ./NCrystalLink -o -d ./NCrystalLink ]; then
    echo "Found ./NCrystalLink already (remove and rerun to force reinstallation)"
    exit 1
fi

nccompfn="NCrystal_sample.comp"
if [ -f ./$nccompfn ]; then
    echo "Found ./$nccompfn (remove and rerun to force reinstallation)"
    exit 1
fi


thisdir="$( cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
nccfgcmd="ncrystal-config"
needs_nc_setup=0
if ! command -v ncrystal-config >/dev/null 2>&1; then
    if [ -f "${thisdir}/ncrystal-config" ]; then
        export PATH="${thisdir}:$PATH"
        needs_nc_setup=1
    fi
fi
if ! command -v ncrystal-config >/dev/null 2>&1; then
    echo "ncrystal-config command not found."
    exit 1
fi

mcstasdir="$(ncrystal-config --show=mcstasdir)"
incdir="$(ncrystal-config --show=includedir)"
libpath="$(ncrystal-config --show=libpath)"

if [ ! -f "$mcstasdir/${nccompfn}" ]; then
    echo "Did not find the ${nccompfn} file (expected it in $mcstasdir due to output from ncrystal-config)."
    exit 1
fi

if [ ! -f "$incdir/NCrystal/ncrystal.h" ]; then
    echo "Did not find the NCrystal/ncrystal.h file (expected it in $incdir due to output from ncrystal-config)."
    exit 1
fi

if [ ! -f "$libpath" ]; then
    echo "Did not find the NCrystal library (expected it in the location $libpath due to output from ncrystal-config)."
    exit 1
fi

mkdir -p NCrystalLink/lib NCrystalLink/include/NCrystal
ln -s "$libpath" NCrystalLink/lib/
ln -s "$incdir/NCrystal/ncrystal.h" NCrystalLink/include/NCrystal/
ln -s "$incdir/NCrystal/ncapi.h" NCrystalLink/include/NCrystal/
ln -s "$mcstasdir/${nccompfn}" .

echo "Succesfully linked ${nccompfn} to current directory and added NCrystalLink which is needed for instrument build."

if [ -f "$mcstasdir/NCrystal_example_mcstas.instr" ]; then
    echo "Note that an example instrument file using ${nccompfn} can be found here: $mcstasdir/NCrystal_example_mcstas.instr"
fi

if [ $needs_nc_setup == 1 ]; then
    echo
    echo "WARNING: you might not have a correctly setup NCrystal environment."
    echo
    echo "You might be able to fix it by typing the following command (including all special characters):"
    echo
    echo '  $("'"${thisdir}/ncrystal-config"'" --setup)'
    echo
fi
