#!/bin/sh
set -eu

release_dir=${UNIDESK_HOST_RELEASE_DIR:-/workspace/release}
source_dir=${UNIDESK_HOST_SOURCE_DIR:-/workspace/source}
metadata_dir=${UNIDESK_HOST_BUILD_METADATA_DIR:-/workspace/build-metadata}
build_log_dir=${UNIDESK_HOST_BUILD_LOG_DIR:-/workspace/image-build-logs}
source_commit=${SOURCE_COMMIT:?SOURCE_COMMIT is required}

network_mode=$(cat "$release_dir/network-mode")
http_proxy=$(cat "$release_dir/http-proxy")
https_proxy=$(cat "$release_dir/https-proxy")
all_proxy=$(cat "$release_dir/all-proxy")
no_proxy=$(cat "$release_dir/no-proxy")
mkdir -p "$metadata_dir" "$build_log_dir"
rm -f "$metadata_dir"/*.json "$build_log_dir"/*.log

built_count=0
skipped_count=0
for service_dir in "$release_dir"/services/*; do
  [ -d "$service_dir" ] || continue
  service_id=$(basename "$service_dir")
  action=$(cat "$service_dir/action")
  reason=$(cat "$service_dir/reason")
  if [ "$action" != build ]; then
    skipped_count=$((skipped_count + 1))
    printf '{"ok":true,"phase":"image-build-service","serviceId":"%s","status":"skipped","action":"%s","reason":"%s","sourceCommit":"%s","valuesPrinted":false}\n' \
      "$service_id" "$action" "$reason" "$source_commit"
    continue
  fi

  image_repository=$(cat "$service_dir/image-repository")
  dockerfile=$(cat "$service_dir/dockerfile")
  image_ref="$image_repository:$(printf '%s' "$source_commit" | cut -c1-12)"
  metadata_file="$metadata_dir/$service_id.json"
  build_log="$build_log_dir/$service_id.log"
  if ! env HTTP_PROXY="$http_proxy" HTTPS_PROXY="$https_proxy" ALL_PROXY="$all_proxy" NO_PROXY="$no_proxy" \
    http_proxy="$http_proxy" https_proxy="$https_proxy" all_proxy="$all_proxy" no_proxy="$no_proxy" \
    buildctl-daemonless.sh build \
      --allow network.host \
      --frontend dockerfile.v0 \
      --local context="$source_dir" \
      --local dockerfile="$source_dir" \
      --opt "filename=$dockerfile" \
      --opt "network=$network_mode" \
      --opt "build-arg:HTTP_PROXY=$http_proxy" \
      --opt "build-arg:HTTPS_PROXY=$https_proxy" \
      --opt "build-arg:ALL_PROXY=$all_proxy" \
      --opt "build-arg:NO_PROXY=$no_proxy" \
      --metadata-file "$metadata_file" \
      --output "type=image,name=$image_ref,push=true,registry.insecure=true" >"$build_log" 2>&1; then
    tail -n 200 "$build_log"
    printf '{"ok":false,"phase":"image-build-service","serviceId":"%s","status":"failed","sourceCommit":"%s","logTailLines":200,"valuesPrinted":false}\n' \
      "$service_id" "$source_commit" >&2
    exit 1
  fi
  cat "$build_log"
  test -s "$metadata_file"
  built_count=$((built_count + 1))
  printf '{"ok":true,"phase":"image-build-service","serviceId":"%s","status":"built","sourceCommit":"%s","imageRef":"%s","metadataReady":true,"valuesPrinted":false}\n' \
    "$service_id" "$source_commit" "$image_ref"
done

printf '{"ok":true,"phase":"image-build","status":"completed","sourceCommit":"%s","builtCount":%s,"skippedCount":%s,"valuesPrinted":false}\n' \
  "$source_commit" "$built_count" "$skipped_count"
