#!/usr/bin/env bash
#
# SPDX-FileCopyrightText: 2021 René de Hesselle <dehesselle@web.de>
#
# SPDX-License-Identifier: GPL-2.0-or-later

### description ################################################################

# This script bootstraps JHBuild so it's ready to build any module.

### shellcheck #################################################################

# Nothing here.

### dependencies ###############################################################

#------------------------------------------------------ source jhb configuration

source "$(dirname "${BASH_SOURCE[0]}")"/../../etc/jhb.conf.sh

#------------------------------------------- source common functions from bash_d

# bash_d is already available (it's part of jhb configuration)

bash_d_include echo
bash_d_include error

#--------------------------------------------------- source additional functions

for file in "$(dirname "${BASH_SOURCE[0]}")"/../src/jhb/*.sh; do
  # shellcheck disable=SC1090 # can't point to a single source here
  source "$file"
done

### variables ##################################################################

SELF_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" || exit 1; pwd)

### functions ##################################################################

# Nothing here.

### main #######################################################################

error_trace_enable

#-------------------------------------------------------- print main directories

echo_i "WRK_DIR = $WRK_DIR"
echo_i "VER_DIR = $VER_DIR"

#--------------------------------------------------------- perform system checks

if  sys_check_wrkdir &&
    sys_check_sdkroot &&
    sys_check_usr_local; then
  sys_check_versions
else
  exit 1    # cannot continue
fi

#--------------------------------------------------- initialize directory layout

if [ "$SELF_DIR" = "$USR_DIR"/bin ]; then
  : # we are already running inside target directory layout
else
  # sync repository into target structure, remove everything git-related
  rsync -a "$SELF_DIR"/../../../jhb/ "$VER_DIR"/
  find "$VER_DIR" -type f -name ".gitignore" -delete
  rm -rf "$VER_DIR"/.git
  rm "$VER_DIR"/.gitmodules
fi

#---------------------------------------------------------------- install ccache

ccache_install
ccache_configure

#------------------------------------------ log relevant versions to release.log

sys_create_log

#------------------------------------------------- install and configure JHBuild

jhbuild_install
jhbuild_configure

#------------------------------------------------------- run bootstrap procedure

jhbuild bootstrap-gtk-osx

#--------------------------------------------------------- install GNU utilities

# GNU versions of various utilites make life signifanctly easier on macOS.

jhbuild build coreutils sed tar
