#!/bin/bash
# Copyright (c) 2023 by the Flatcar Maintainers.
# Use of this source code is governed by the Apache 2.0 license.

. "$(dirname "$0")/common.sh" || exit 1

# Script must run inside the chroot
assert_inside_chroot
assert_not_root_user

# Dependencies and packages to include by default.
packages_default=( "coreos-devel/board-packages" )

# Packages that are rdeps of the above but should not be included.
# (mostly large packages, e.g. programming languages etc.)
skip_packages_default="dev-lang/rust,dev-lang/rust-bin,dev-lang/go,dev-lang/go-bootstrap,dev-go/go-md2man"


# Developer-visible flags.
DEFINE_string board "${DEFAULT_BOARD}" \
  "The board to build packages for."
DEFINE_string skip_packages "${skip_packages_default}" \
  "Comma-separated list of packages in the dependency tree to skip."
DEFINE_boolean pretend "${FLAGS_FALSE}" \
  "List packages that would be built but do not actually build."

FLAGS_HELP="usage: $(basename "$0") [flags] [packages]

build_dev_binpkgs builds binary packages for all dependencies of [packages]
that are not present in '/build/<board>/var/lib/portage/pkgs/'.
Useful for publishing a complete set of packages to a binhost.

[packages] defaults to '${packages_default[*]}' if not specified.
"

# Parse command line
FLAGS "$@" || exit 1
eval set -- "${FLAGS_ARGV}"

# Die on any errors.
switch_to_strict_mode

if [[ $# -eq 0 ]]; then
  set -- "${packages_default[@]}"
fi
# --

function my_board_emerge() {
  PORTAGE_CONFIGROOT="/build/${FLAGS_board}" SYSROOT="${SYSROOT:-/build/${FLAGS_board}}" ROOT="/build/${FLAGS_board}" sudo -E emerge "${@}"
}
# --

pkg_build_list=()
pkg_skipped_list=()

info "Collecting list of binpkgs to build"

# Normally, BDEPENDs are only installed to the SDK, but the point of this script
# is to install them to the board root because the dev container uses a board
# profile. This is easily achieved using --root-deps. Since it is still the SDK
# doing the building, which might have different package versions available to
# the board profile, we have to be careful not to include SDK BDEPENDs in the
# list of binary packages to publish, hence the sed call.
while read -r pkg; do
  [[ -f /build/${FLAGS_board}/var/lib/portage/pkgs/${pkg}.tbz2 ]] && continue
  IFS=,
  for s in ${FLAGS_skip_packages}; do
    if [[ ${pkg} == ${s}-* ]] ; then
      pkg_skipped_list+=("${pkg}")
      continue 2
    fi
  done
  unset IFS
  pkg_build_list+=("=${pkg}")
  echo "      =${pkg}"
done < <(my_board_emerge --pretend --emptytree --root-deps "${@}" |
         sed -n "/\[ebuild .* to \/build\/${FLAGS_board}\/ /s/^\[[^]]\+\] \([^ :]\+\)*:.*/\1/p")
# --

if [[ ${#pkg_skipped_list[@]} -gt 0 ]]; then
  info "Skipping binpkgs '${pkg_skipped_list[*]}' because these are in the skip list."
fi

pretend=""
[[ ${FLAGS_pretend} -eq ${FLAGS_TRUE} ]] && pretend="--pretend"

my_board_emerge --buildpkg ${pretend} "${pkg_build_list[@]}"
