#!/bin/bash

#
# Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#

set -euo pipefail

GRADLE_ARGS=(--no-configuration-cache --console=plain)

echo "actualizing Kotlin JS/Wasm lock files"
echo
./gradlew kotlinUpgradeYarnLock kotlinWasmUpgradeYarnLock "${GRADLE_ARGS[@]}"

echo "building all artifacts"
echo
./gradlew publishToMavenLocal site:jsBrowserDistribution buildInstallablePlugin :lyng:linkReleaseExecutableLinuxX64 :lyng:installJvmDist --parallel "${GRADLE_ARGS[@]}"

echo
echo "publishing remote Maven artifacts"
echo
tasks_log="$(mktemp)"
./gradlew tasks --all "${GRADLE_ARGS[@]}" >"$tasks_log"
mapfile -t publish_tasks < <(
    sed -n 's/^\([^[:space:]]*publish.*PublicationToMavenRepository\) - .*$/:\1/p' "$tasks_log" |
        sort -u
)
rm -f "$tasks_log"

if [ "${#publish_tasks[@]}" -eq 0 ]; then
    echo "No remote Maven publication tasks found."
    exit 1
fi

for task in "${publish_tasks[@]}"; do
    echo
    echo "Publishing $task"
    publish_log="$(mktemp)"
    if ./gradlew "$task" "${GRADLE_ARGS[@]}" >"$publish_log" 2>&1; then
        cat "$publish_log"
        rm -f "$publish_log"
        continue
    fi

    if grep -Eiq '(^|[^0-9])409([^0-9]|$)|already exists|already been published|cannot be updated|Package file already exists|Repository does not allow updating assets' "$publish_log"; then
        cat "$publish_log"
        echo "Skipping $task: artifact already exists in the Maven repository."
        rm -f "$publish_log"
        continue
    fi

    cat "$publish_log"
    rm -f "$publish_log"
    exit 1
done

echo
echo "building CLI tools"
echo
bin/local_jrelease
bin/local_release

echo
echo "Deploying site"
echo
./bin/deploy_site
