#!/bin/bash -e

set -e

# sudo apt-get install sharutils texi2html

VERSION=2.31.1

export NUM_JOBS="$(($(nproc) * 2))"
export SRC_DIR=${HOME}/src

NAME=binutils
SOURCE_DIR=${SRC_DIR}/${NAME}-${VERSION}
BUILD_DIR=${SOURCE_DIR}_build
ARCHIVE_FILE=${NAME}-${VERSION}.tar.xz
URL=http://ftp.gnu.org/gnu/${NAME}/${ARCHIVE_FILE}

pushd ${SRC_DIR}

# Download package
if [ ! -f ${ARCHIVE_FILE} ]; then
    wget ${URL}
fi

# Untar
if [ ! -d ${SOURCE_DIR} ]; then
    tar xvf ${ARCHIVE_FILE}
fi

# Create build directory, configure and build
mkdir -p ${BUILD_DIR}
pushd ${BUILD_DIR}


${SOURCE_DIR}/configure \
--prefix=/usr \
--enable-gold       \
--enable-ld=default \
--enable-plugins    \
--enable-shared     \
--disable-werror    \
--enable-64-bit-bfd \
--with-system-zlib \

make -j ${NUM_JOBS}
sudo make install

popd
