#!/usr/bin/env bash
set -euo pipefail

script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
# shellcheck source=common.sh
source "$script_dir/common.sh"

if [[ $# -ne 1 ]]; then
  echo "usage: $0 <ios|tvos>" >&2
  exit 64
fi
platform=$1
case "$platform" in
  ios)
    product=MobileVLCKit
    os_style=iPhone
    minimum=$BEAM_IOS_DEPLOYMENT_TARGET
    upstream_args=(-n -f -v)
    ;;
  tvos)
    product=TVVLCKit
    os_style=AppleTV
    minimum=$BEAM_TVOS_DEPLOYMENT_TARGET
    upstream_args=(-n -f -v -t)
    ;;
  *) beam_die "platform must be ios or tvos" ;;
esac
if [[ ${BEAM_VLCKIT_SKIP_LIBVLC:-0} == 1 ]]; then
  upstream_args+=(-l)
elif [[ ${BEAM_VLCKIT_SKIP_LIBVLC:-0} != 0 ]]; then
  beam_die "BEAM_VLCKIT_SKIP_LIBVLC must be 0 or 1"
fi

beam_assert_work_dir
trap beam_release_canonical_lock EXIT
trap 'exit 130' INT
trap 'exit 143' TERM
beam_acquire_canonical_lock
[[ -d "$beam_vlckit_source_dir/libvlc/vlc/.git" ]] || beam_die "prepared source tree is missing"
[[ -d "$beam_vlckit_staging_dir" ]] || beam_die "candidate staging directory is missing"
mkdir -p "$beam_vlckit_staging_dir/logs" "$beam_vlckit_staging_dir/frameworks" \
  "$beam_vlckit_staging_dir/audit/modules/$platform" "$beam_vlckit_staging_dir/audit/link-inputs/$platform"

export LC_ALL=C
export SOURCE_DATE_EPOCH=$BEAM_SOURCE_DATE_EPOCH
export ZERO_AR_DATE=1
export BEAM_VLCKIT_IOS_DEPLOYMENT_TARGET=$BEAM_IOS_DEPLOYMENT_TARGET
export BEAM_VLCKIT_TVOS_DEPLOYMENT_TARGET=$BEAM_TVOS_DEPLOYMENT_TARGET
export BEAM_VLCKIT_MODULE_FILTER="$script_dir/audit_static_modules.py"
export BEAM_VLCKIT_LICENSE_AUDIT_ROOT="$beam_vlckit_staging_dir/audit/modules/$platform"
export BEAM_VLCKIT_PINS="$script_dir/pins.env"
export BEAM_VLCKIT_BASELINE="$script_dir/baseline-modules.txt"
export BEAM_VLCKIT_FORBIDDEN_PREFIX="$beam_mobile_dir"

log="$beam_vlckit_staging_dir/logs/build-$platform.log"
set +e
python3 "$script_dir/disk_guard.py" \
  --path "$beam_vlckit_build_root" \
  --minimum-free-gib "$BEAM_STOP_FREE_GIB" \
  --cwd "$beam_vlckit_source_dir" \
  -- ./buildMobileVLCKit.sh "${upstream_args[@]}" \
  2>&1 | tee "$log"
build_status=${PIPESTATUS[0]}
set -e
if [[ $build_status -ne 0 ]]; then
  beam_die "$platform VLCKit build failed with status $build_status; see $log"
fi

source_framework="$beam_vlckit_source_dir/build/$product.xcframework"
[[ -d "$source_framework" ]] || beam_die "upstream build did not produce $source_framework"
rm -rf "$beam_vlckit_staging_dir/frameworks/$product.xcframework"
cp -a "$source_framework" "$beam_vlckit_staging_dir/frameworks/$product.xcframework"

python3 "$script_dir/audit_link_inputs.py" \
  --vlc-root "$beam_vlckit_source_dir/libvlc/vlc" \
  --os-style "$os_style" \
  --minimum-os "$minimum" \
  --audit-dir "$beam_vlckit_staging_dir/audit/link-inputs/$platform" \
  --forbidden-prefix "$beam_mobile_dir"

python3 "$script_dir/audit_xcframework.py" \
  --xcframework "$beam_vlckit_staging_dir/frameworks/$product.xcframework" \
  --product "$product" \
  --platform "$platform" \
  --minimum-os "$minimum" \
  --approved-inventory \
    "$beam_vlckit_staging_dir/audit/modules/$platform/$os_style/approved-module-entries.tsv" \
  --forbidden-prefix "$beam_mobile_dir" \
  --report "$beam_vlckit_staging_dir/audit/$platform-xcframework.json"

echo "Built and audited $product."
