#! /bin/bash -
# Specific script for the final Dragora release process.
#
# Copyright (c) 2021-2023 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
## ChangeLog.
#
#= Version 0.7:
#
# - Change of strategy to build the 'phase-2' using a for loop,
#   this also makes the awk filter to exclude recipes work.
#
#= Version 0.6:
#
# - Adjusted the PATH in order to avoid possible influence of
#   the temporary system at /tools.
#
#= Version 0.5:
#
# - Added "libs/barelibs" recipe to the $exclude_list on 'phase-2'.
#   Thus shared libraries provided in barelibs must be replaced
#   with the installation of the proper software packages.
#
#= Version 0.4:
#
# - Build the final system using real cores (instead of threads,
#   if possible) to try to guarantee an error-free compilation.
#   Instructions taken from https://stackoverflow.com/questions/6481005/how-to-obtain-the-number-of-cpus-cores-in-linux-from-the-command-line
#
#= Version 0.3:
#
# - Use the -f (force) option when building on 'phase-1'.
#
#= Version 0.2:
#
# - Switch /bin/sh to /bin/bash for catching errors on pipes.
#
# - Upgrade packages on 'phase-2' to ensure no dependency on
#   /tools (temporary system) or to ensure possible system
#   breaks by cutting symbolic links using qi/graft.
#
#= Version 0.1:
#
# - Initial version.
#

set -e -o pipefail

rm -f /build-phase1-log.txt /build-phase2-log.txt

#
# Build all the packages to conform the final system.
#

echo ""
echo "*** Building Dragora for release [phase-1]..."
echo ""
echo "Using 1 parallel job for the compiler."
echo ""

qi order /usr/src/qi/recipes/*.order | \
 qi build -f -j1 -p -i - 2>&1 | tee build-phase1-log.txt

#
# Build all the important packages against the final system
#

# Variables.
#
# Parallel jobs for the final system (phase-2)
#
# The default is to use the maximum number of
# installed processors (this should work on
# hyperthreading and non-hyperthreading systems)
JOBS="${JOBS:-$(awk -F: '/^physical/ && !ID[$2] { P++; ID[$2]=1 }; /^cpu cores/ { CORES=$2 };  END { print CORES*P }' /proc/cpuinfo)}"

# Declare recipes that do not need to be a rebuild:
exclude_list='!/pass-?|^data\/|docbook\/|xorg\/doc\/|xorg\/font\/|tde\/tde-i18n|devel\/fortify-headers|kernel\/headers|kernel\/generic|kernel\/buildtree-generic|kernel\/firmware|libs\/barelibs|tools\/dragora-installer/'

echo ""
echo "*** Building Dragora for release [phase-2]..."
echo ""
echo "Using $JOBS parallel job(s) for the compiler."
echo ""

# In order to cover a purest build, avoid influence of /tools

export PATH=/bin:/usr/bin:/sbin:/usr/sbin:/opt/trinity/bin

if test -d /tools
then
    mv -f /tools /tools-to-be-renamed
    sync
fi

# Start to build the final phase (phase-2)

: > build-phase2-log.txt
for i in $(qi order /usr/src/qi/recipes/*.order | awk "$exclude_list")
do
    echo "building -> $i"
    qi build -f -j${JOBS} -p -u "$i" 2>&1
done | tee -a build-phase2-log.txt

unset -v JOBS exclude_list i

# Restore /tools for `enter-chroot`

if test -d /tools-to-be-renamed
then
    mv -f /tools-to-be-renamed /tools
    sync
fi

echo ""
echo "^^^ Done."
echo ""

