#! /bin/bash

# This script will configure and build Gnuastro in parallel inside another
# directory (to keep the source and build directories separate). By default
# it is in the tmpfs directory of the RAM. Run with '--help' for a more
# complete description.
#
# Original author:
#   Mohammad Akhlaghi <mohammad@akhlaghi.org>
# Contributing author(s):
#   Mosè Giordano <mose@gnu.org>
#   Raul Infante-Sainz <infantesainz@gmail.com>
#   Pedram Ashofteh Ardakani <pedramardakani@pm.me>
# Copyright (C) 2016-2022 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/>.





# Exit the script if any of the commands fail
set -e





# Default values for variables.
jobs=0
dist=0
debug=0
clean=0
check=0
reconf=0
upload=0
install=0
valgrind=0
prefix=/usr/local



# Check and set the temporary directory
if [ -d /dev/shm ]; then
    top_build_dir=/dev/shm

elif [ -d /tmp ]; then
    top_build_dir=/tmp

else
    top_build_dir=.

fi





if [ -f .version ]; then
    version=$(cat .version)
else
    version=""
fi





# Output of help.
me=$0                           # Executable file name.
help_print() {

    # See if autoreconf is enabled or not.
    if [ $reconf = "0" ]; then
        reconf_status="DISABLED"
    else
        reconf_status="ENABLED"
    fi

    # See if debug is enabled or not.
    if [ $debug = "0" ]; then
        debug_status="DISABLED"
    else
        debug_status="ENABLED"
    fi

    # See if valgrind is enabled or not.
    if [ $valgrind = "0" ]; then
        valgrind_status="DISABLED"
    else
        valgrind_status="ENABLED"
    fi

    # See if clean is enabled or not.
    if [ $clean = "0" ]; then
        clean_status="DISABLED"
    else
        clean_status="ENABLED"
    fi

    # See if check is enabled or not.
    if [ $check = "0" ]; then
        check_status="DISABLED"
    else
        check_status="ENABLED"
    fi

    # See if install is enabled or not.
    if [ $install = "0" ]; then
        install_status="DISABLED"
    else
        install_status="ENABLED"
    fi

    # See dist is enabled or not.
    if [ $dist = "0" ]; then
        dist_status="DISABLED"
    else
        dist_status="ENABLED"
    fi

    # See if upload is enabled or not.
    if [ $upload = "0" ]; then
        upload_status="DISABLED"
    else
        upload_status="ENABLED"
    fi

    # Print the output.
    cat <<EOF

Usage: $me [OPTION]...

Configure and make the package in a separate directory and put a symbolic
link to that directory in the current directory. A top build directory must
be given to host the build directory for this script (see 'top-build-dir'
option below). The symbolic link to the build directory in this directory
will be called 'build'.

Through the options, it also possible to request further operations after
the build (for example checking, making distribution files or
installing). See the "Separate build and source directories" section of the
Gnuastro manual for a more complete introduction to this script.

Options:

 -b, --top-build-dir STR  Address to host the top build directory.
                          Current value: $top_build_dir

 -o, --prefix STR         Install architecture-independent files here.
                          Current value: $prefix

 -V, --version            Print current version to be used in absolute
                          build directory name.
                          Current value: $version

 -a, --autoreconf         Run 'autoreconf -f' (to set the version and
                          update the build system) before anything else.
                          Current status: $reconf_status

 -c, --clean              Delete (with 'rm') all its contents of the build
                          directory before starting new configuration.
                          Current status: $clean_status

 -d, --debug              Build Gnuastro with debugging information,
                          no optimization flags, and without shared
                          libraries.
                          Current status: $debug_status

 -v, --valgrind           Build with '--enable-check-with-valgrind'.
                          Current status: $valgrind_status

 -j, --jobs INT           Number of threads to use in 'make'.
                          Current value: $jobs

 -C, --check              Run 'make check' after the build.
                          Current status: $check_status

 -i, --install            Run 'sudo make install' after the build.
                          Current status: $install_status

 -D, --dist               Build a tar.lz tarball and PDF manual.
                          Current status: $dist_status

 -u, --upload STR         First run '--dist', then upload tarball and PDF
                          manual to the given 'server:folder'.
                          For example: -u my-server:folder
                          Current status: $upload_status

 -p, --publish STR        Short for '-a -c -d -C -u STR'. '-d' is added
                          because it will greatly speed up the build. It
                          will have no effect on the produced tarball.

 -I, --install-archive    Short for '-a -c -C -i -D'.

 -P, --printparams        Another name for '--help', for similarity with
                          Gnuastro's programs. Note that the output of
                          '--help' also includes the variable values.

 -h, --help               Print this help list.

Mandatory or optional arguments to long options are also mandatory or optional
for any corresponding short options.

GNU Astronomy Utilities home page: http://www.gnu.org/software/gnuastro/

Report bugs to bug-gnuastro@gnu.org.
EOF
}





# Parse the arguments.
while [[ $# -gt 0 ]]
do
    key="$1"
    case $key in
        -o|--prefix)
            prefix="$2"
            if [ x"$prefix" = x ]; then
                echo "No argument given to '--prefix' ('-o')."
                exit 1;
            fi
            shift # past argument
            shift # past value
            ;;
        -b|--top-build-dir)
            top_build_dir="$2"
            if [ x"top_build_dir" = x ]; then
                echo "No argument given to '--top-build-dir' ('-b')."
                exit 1;
            fi
            shift # past argument
            shift # past value
            ;;
        -V|--version)
            echo $version
            exit 0
            ;;
        -a|--autoreconf)
            reconf=1
            shift # past argument
            ;;
        -c|--clean)
            clean=1
            shift # past argument
            ;;
        -d|--debug)
            debug=1
            shift # past argument
            ;;
        -v|--valgrind)
            valgrind=1
            shift # past argument
            ;;
        -j|--jobs)
            jobs="$2"
            if [ x"jobs" = x ]; then
                echo "No argument given to '--jobs' ('-j')."
                exit 1;
            fi
            shift # past argument
            shift # past value
            ;;
        -C|--check)
            check=1
            shift # past argument
            ;;
        -i|--install)
            install=1
            shift # past argument
            ;;
        -D|--dist)
            dist=1
            shift # past argument
            ;;
        -u|--upload)
            dist=1
            upload=1
            url="$2"
            if [ x"$url" = x ]; then
                echo "No SERVER:DIR given to '--upload' ('-u') ."
                exit 1;
            fi
            shift # past argument
            shift # past value
            ;;
        -p|--publish)
            dist=1
            clean=1
            debug=1
            check=1
            reconf=1
            upload=1
            url="$2"
            if [ x"$url" = x ]; then
                echo "No SERVER:DIR given to '--publish' ('-p') ."
                exit 1;
            fi
            shift # past argument
            shift # past value
            ;;
        -I|--install-archive)
            dist=1
            clean=1
            check=1
            reconf=1
            install=1
            shift # past argument
            ;;
        -h|-P|--help|--printparams)
            help_print
            exit 0
            ;;
        *)    # unknown option
            echo "'$1' isn't a recognized option. Aborted."
            echo
            echo "Recall that for this script, options and their values must be"
            echo "separated by atleast one white-space character."
            exit 1
            ;;
    esac
done





# Check if the configure script already exists. If not, let the user know
# what to do.
if ! [ -f configure ]; then
    echo "The 'configure' script doesn't exist!"
    echo "It is created after bootstrapping Gnuastro."
    echo;
    echo "The dependencies to bootstrap Gnuastro are described here:"
    echo "   https://www.gnu.org/software/gnuastro/manual/html_node/Bootstrapping-dependencies.html"
    echo;
    echo "Afterwards, the bootstrapping process is described here:"
    echo "   https://www.gnu.org/software/gnuastro/manual/html_node/Bootstrapping.html"
    echo;
    echo "If you don't want to develop Gnuastro, but just build, install and use it, you don't need to bootstrap, please use the tarball:"
    echo "   https://ftp.gnu.org/gnu/gnuastro/"
    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 number of jobs
# ----------------------
#
# If the user hasn't manually specified the number of threads, see if we
# can deduce it from the host:
#  - On systems with GNU Coreutils we have 'nproc'.
#  - On BSD-based systems (for example FreeBSD and macOS), we have a
#    'hw.ncpu' in the output of 'sysctl'.
#  - When none of the above work, just set the number of threads to 1.
if [ $jobs = 0 ]; then
    if type nproc > /dev/null 2> /dev/null; then
        jobs=$(nproc --all);
    else
        jobs=$(sysctl -a | awk '/^hw\.ncpu/{print $2}')
        if [ x"$jobs" = x ]; then jobs=1; fi
    fi
else
    jobs=1
fi





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





# 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=$top_build_dir/"$basedir"-$version
else
    build_dir=$top_build_dir/"$basedir"
fi





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





# If reconfiguration was requested, do it.
if [ $reconf = 1 ]; then
    autoreconf -f
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





# Clean the contents of the build directory if requested.
if [ x$clean = x1 ]; then
    rm -rf $build_sym/*
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.
if [ ! -f Makefile ]; then

    # Set the proper flags.
    if [ x$debug = x1 ]; then
        confopts="--enable-debug"
    fi
    if [ x$valgrind = x1 ]; then
        confopts="--enable-check-with-valgrind"
    fi

    # Run the configure script.
    $srcdir/configure --srcdir=$srcdir $confopts --prefix=$prefix
fi





# Build Gnuastro in that directory with the specified number of threads
make -k -j$jobs





# If requested, also run 'make check'.
if [ x$check = x1 ]; then
    make check -k -j$jobs
fi





# Make the tarball and PDF for distribution. Then put a copy of the PDF in
# the top build directory for easy usage later.
if [ x$dist = x1 ]; then
    make dist-lzip pdf
    cp doc/gnuastro.pdf ./
fi





# Upload the tarball to the requested server.
if [ x$upload = x1 ]; then
    # Get the base package name, and use it to make a generic tarball
    # name. Note that with the '--upload' option, '--dist' is also
    # activated, so the tarball is already built and ready by this
    # step.
    tarball=$(ls *.tar.lz)
    base=$(echo $tarball | sed -e's/-/ /' | awk '{print $1}')

    # File names:
    pdf=$base.pdf
    latest=$base"-latest.tar.lz"

    # Copy the files to the subdirectories of the given URL (must include
    # folders).
    scp $tarball $url/src
    scp $pdf     $url/pdf

    # Make the symbolic links on the server.
    server=$(echo "$url" | sed -e's/:/ /' | awk '{print $1}')
    sdir=$(echo "$url" | sed -e's/:/ /' | awk '{print $2}')
    ssh $server 'cd '"$sdir"' && rm -f '$latest' && ln -s src/'$tarball $latest ' && exit'
    echo; echo "On $url:"
    echo "   $latest -> src/$tarball"
fi





# If requested, run 'sudo make install'.
if [ x$install = x1 ]; then

    # Check if user has permission to write in $prefix, if not, use 'sudo'.
    if [ -w $prefix ]; then
        make install -kj$jobs
    else
        sudo make install -kj$jobs
    fi
fi
