#!/bin/bash
set -o errexit

prefix_install=/home/test_user2/.local
src_dir=${prefix_install:?}/usr/src
src_install=${src_dir:?}/guix
guix_store=/var/tmp/test_guix/gnu/store
guix_state=/var/tmp/tets_guix/var/guix
guix_branch='v0.14.0'

[ -z "$CLEANED" ] \
    && guix_bin="/usr/local/bin/guix" \
    && exec env -i CLEANED=1 ${guix_bin} environment guix --ad-hoc \
            automake bash-minimal bzip2 gcc-toolchain gettext-minimal git gnutls \
            graphviz guile guile-git guile-json guile-ssh gzip help2man libgcrypt \
            nss-certs sqlite texinfo zlib \
            -- bash --login --noprofile --norc "${0:?}" "${@-}"

echo "==== DISPLAY ENV ====="
env
echo "==== ===="

#####
# PREPARE SOURCES
#####
echo "==== Preparing sources ====="

mkdir -p ${src_dir:?}
mkdir -p ${guix_store:?} ${guix_state:?}

if [[ -d ${src_install:?} ]];then
    cd ${src_install:?}
    git fetch --all --recurse-submodules
else
    git clone --recurse https://git.savannah.gnu.org/git/guix.git ${src_install:?}
fi
cd ${src_install:?}
git checkout ${guix_branch:?}
git clean -d -x -f
./bootstrap
./configure --with-store-dir=${guix_store:?} --localstatedir=${guix_state:?}
echo "==== ===="

####
# BUILDING
####
echo "Building....."
make -j 8
