#! /bin/sh
prefix=/usr
exec_prefix=${prefix}
bindir=${exec_prefix}/bin
libdir=/usr/lib
datarootdir=${prefix}/share
pkgdatadir=${datarootdir}/gds-utilities

#--------------------------------------  Find root.exe in the executable path
rconfig=`which root-config 2>/dev/null`
[ -n "$rconfig" ] || rconfig=none
rpath=`echo $rconfig | sed s,/bin/root-config,,g`

#--------------------------------------  Compare with ROOTSYS
if [ -n "$ROOTSYS" ]; then
    if [ "$ROOTSYS" != "$rpath" ]; then
        echo "*****************************************************************"
        echo "*                                                               *"
	echo "*  Warning: ROOTSYS doesn't point to the first root executable  *"
        echo "*                                                               *"
        echo "*****************************************************************"
    fi

#--------------------------------------  No ROOTSYS. Set it, add it to PATHs
else 
    export ROOTSYS=$rpath
fi
echo "Your ROOT system directory is:  $ROOTSYS"

#-------------------------------------  Find the sandbox libraries
echo "Your gds directory path is: $libdir"

#-------------------------------------  Check root/DMT version compatibility
rootvsn=`$ROOTSYS/bin/root-config --version`
dmtroot=`cat $pkgdatadir/root-version`
if [ "$rootvsn" != "$dmtroot" ]; then
    echo "******************************************************************"
    echo "*                                                                *"
    echo "*    Your ROOTSYS version ($rootvsn) is different than            *"
    echo "*    that used to compile the DMT libraries ($dmtroot)            *"
    echo "*    You may need to 'unsetenv ROOTSYS' before setup             *"
    echo "*                                                                *"
    echo "******************************************************************"
    return 1
else
    echo "You are using root version:     $rootvsn"
fi

#-------------------------------------  See if .rootrc is defined
if [ -h $HOME/.rootrc -o ! -e $HOME/.rootrc ]; then
    echo " "
    echo "Linking $HOME/.rootrc to $pkgdatadir/.rootrc"
    rm -f $HOME/.rootrc
    ln -fs $pkgdatadir/.rootrc $HOME/.rootrc
else
    echo "******************************************************************"
    echo "*                                                                *"
    echo "* Warning: Your \$HOME/.rootrc file is not a link, and therefore  *"
    echo "* will not be replaced. This may cause the incorrect version of  *"
    echo "* the sandbox libraries to be loaded.                            *"
    echo "*                                                                *"
    echo "******************************************************************"
fi
