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

# You can install a custom configuration file by passing it as first
# argument. It has to be named '*.conf.sh'.

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

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

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

bash_d_include error

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

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

FORCE_BUILD_FROM_SOURCE=${FORCE_BUILD_FROM_SOURCE:-false}

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

function is_release_usable
{
  local url=$1

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

  # download at least 100 kb of data
  curl -L "$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_wrkdir_is_usable &&
    sdkroot_exists &&
    sys_usrlocal_is_clean; then

  # these checks may issue warnings but have have no consequences otherwise
  sys_macos_is_recommended || true
  sys_sdk_is_recommended || true
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

for URL in "${RELEASE_URLS[@]}"; do
  if is_release_usable "$URL" && ! $FORCE_BUILD_FROM_SOURCE; then
    echo_i "using $URL"
    curl -L "$URL" | tar -C "$WRK_DIR" -xJ
    exit $?   # we can quit here and now, nothing further to do
  fi
done

echo_i "building everything from scratch"

#---------------------------------------------------------------- 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 significantly easier on macOS.

jhbuild build coreutils findutils sed tar

#-------------------------------------------------------------- install dmgbuild

dmgbuild_install
