#!/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 ##################################################################

function is_release_usable
{
  local partial_download="$TMP_DIR/${FUNCNAME[0]}".tar.xz
  local rc=1

  # download at least 100 kb of data
  curl -L "$RELEASE_URL" 2>/dev/null | head -c 100000 > "$partial_download"
  # if we got 100 kb, it's not a "404 file not found"
  if [ "$(stat -f%z "$partial_download")" -eq 100000 ]; then
    # look inside: dir needs to match our VER_DIR to be usable
    local dir
    dir=$(basename "$(tar -tvJf "$partial_download" 2>/dev/null |
      head -n 1 | awk '{ print $NF }')")
    if [ "$dir" = "$(basename "$VER_DIR")" ]; then
      rc=0
    fi
  fi

  rm "$partial_download"

  return $rc
}

### 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

#------------------------------------------- check if binary release can be used

if is_release_usable; then
  echo_i "using $RELEASE_URL"
  curl -L "$RELEASE_URL" | tar -C "$WRK_DIR" -xJ
  exit $?   # we can quit now, nothing further to do
else
  echo_i "building everything from scratch"
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 findutils sed tar
