#!/bin/bash

# Running this script, you will get gdb installed along with hscPipe
# To use the gdb installed by this script, you have to setup it. Thus:
#   [user@example ~]$ setup gdb
#
# If the system's gdb does not work with hscPipe,
# you have to build one from the source either by this script or by
# yourself.

set -e

. "$(dirname "$0")"/../bashrc
setup-eups
setup python

# get $product_ver from this file name: add-$product_name-$product_ver.sh
product_ver="$(basename "$0")"
product_ver="${product_ver##*-}"
product_ver="${product_ver%.*}"
product_dir=$EUPS_PATH/$(eups flavor)/gdb/$product_ver

wget http://ftp.gnu.org/gnu/gdb/gdb-$product_ver.tar.xz \
    -O gdb-$product_ver.tar.xz

tar xaf gdb-$product_ver.tar.xz
cd gdb-$product_ver

[[ -n "$CPATH"       ]] && CPATH=":$CPATH"
[[ -n "$LD_RUN_PATH" ]] && LD_RUN_PATH=":$LD_RUN_PATH"
export CPATH="$PYTHON_DIR/include/python2.7$CPATH"
export LD_RUN_PATH="$PYTHON_DIR/lib$LD_RUN_PATH"
export LIBRARY_PATH="$LD_RUN_PATH"

if ! type -P makeinfo
then
    disable_makeinfo_if_absent="MAKEINFO=true"
fi

./configure --prefix=$product_dir
make $disable_makeinfo_if_absent -j
make $disable_makeinfo_if_absent install

mkdir -p $product_dir/ups
cat > $product_dir/ups/gdb.table << 'EOF'
setupRequired(python)
pathPrepend(PATH, ${PRODUCT_DIR}/bin)
EOF

eups declare -r $product_dir gdb $product_ver
