#! /bin/sh

# This script will configure and build Gnuastro in parallel within a
# temporary tmpfs directory in the RAM. Please see the "Configure and build
# in RAM" section of the Gnuastro book/documentation for a full
# explanation.
#
# Original author: Mohammad Akhlaghi <akhlaghi@gnu.org>
# Contributing author(s):
#   Mosè Giordano <mose@gnu.org>
# Copyright (C) 2016, Free Software Foundation, Inc.
#
# Gnuastro is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# Gnuastro is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with Gnuastro. If not, see <http://www.gnu.org/licenses/>.





# Set the variables:
NUM_THREADS=8
TMPDIR=/dev/shm





# Check if TMPDIR exists
if [ ! -d $TMPDIR ]; then
    echo "$TMPDIR doesn't exist. Aborted."
    exit 1
fi





# Keep the address of this source directory (where this script is being run
# from) which we will need later.
srcdir=$(pwd)





# Set the build directory name in tmpfs. If the .version file exists, use
# it to allow multiple version builds there (which might happen during
# development).
basedir=$(basename -- "$srcdir")
if [ -f .version ]; then
    build_dir=$TMPDIR/"$basedir"-$(cat .version)
else
    build_dir=$TMPDIR/"$basedir"
fi





# Make the build directory in tmpfs (if it doesn't already exist).
if [ ! -d $build_dir ]; then
    mkdir $build_dir
fi





# Make a symbolic link to the tmpfs build directory for users to easily
# access the built files and also follow the progress. We are first
# deleting any existing symbolic link and remaking it since the possible
# deletion of $build_dir during the development can complicate the
# pre-existing symbolic link.
build_sym=build
if [ -h $build_sym ]; then
    # Delete a harmless symbolic link, if present.
    rm $build_sym
fi





# Create the link only if the symbolic link doesn't exist.
if [ ! -e $build_sym ]; then
    ln -s $build_dir $build_sym
else
    echo "$build_sym already exists here and is not a symbolic link."
    echo "Aborted."
    exit 1
fi





# Go into the build directory to start the configure and/or build:
cd $build_dir





# If a 'Makefile' doesn't exist, then configure Gnuastro.
#
# FOR DEBUGGING: uncomment the second half of this line. Gnuastro uses GNU
# Libtool to build shared libraries for highly portable and maintainable
# usage on a wide variety of systems. While this is great for binaries,
# shared libraries can be a pain when debuggin. For this reason,
# compilation of shared libraries can be turned off by specifying the
# --disable-shared option to configure. With static libraries, compilation
# (the `make' command) will also significantly speed up. Also, by default
# (in `configure.ac'), we have set optimization flags which have to be
# cancelled in debugging.
if [ ! -f Makefile ]; then
    $srcdir/configure --srcdir=$srcdir #CFLAGS="-g -O0" --disable-shared
fi



# Build Gnuastro in that directory with the specified number of threads
make -kj$NUM_THREADS
