#!/usr/bin/env bash

# Очистка предыдущих файлов
> sha1sum.txt
> sha256sum.txt
> releases.json

for f in *VC*; do mv "$f" "${f//VC/vc}"; done
for f in *-SSE4.2*; do mv "$f" "${f//-SSE4.2/}"; done

set -euo pipefail

tmp_json="releases.tmp.json"
echo "{}" > "$tmp_json"

# Функции
extract_version() {
    echo "$1" | grep -oP '[0-9]+\.[0-9]+\.[0-9]+'
}

extract_branch() {
    echo "$1" | awk -F. '{print $1"."$2}'
}

extract_build_key() {
    local name="$1"
    echo "$name" | sed -E 's/^php(-debug-pack|-devel-pack)?-//; s/-Win32-/-/; s/\.zip$//' | sed 's/-src//; s/-test-pack//'
}

get_file_size() {
    local size=$(stat -c%s "$1")
    numfmt --to=iec --format="%.2f" "$size"
}

get_sha256() {
    sha256sum "$1" | awk '{print $1}'
}

get_mtime() {
    date -u -r "$1" +"%Y-%m-%dT%H:%M:%S+00:00"
}

# Добавляем файл в sha1sum и sha256sum
add_hashes() {
    local file="$1"
    local sha1=$(sha1sum "$file" | awk '{print $1}')
    local sha256=$(sha256sum "$file" | awk '{print $1}')
    echo "$sha1 *$file" >> sha1sum.txt
    echo "$sha256 *$file" >> sha256sum.txt
}

# Добавление в JSON
add_to_json() {
    local branch="$1"
    local version="$2"
    local type="$3"
    local build="$4"
    local file="$5"

    local size_human=$(get_file_size "$file")
    local sha256=$(get_sha256 "$file")
    local mtime=$(get_mtime "$file")

    # Устанавливаем версию
    jq ".[\"$branch\"].version = \"$version\"" "$tmp_json" > tmp && mv tmp "$tmp_json"

    # Добавляем нужные поля
    if [[ "$type" == "source" || "$type" == "test_pack" ]]; then
        jq ".[\"$branch\"].$type = {
            \"path\": \"$file\",
            \"size\": \"$size_human\",
            \"sha256\": \"$sha256\"
        }" "$tmp_json" > tmp && mv tmp "$tmp_json"
    else
        # mtime и zip/debug_pack/devel_pack
        jq ".[\"$branch\"].\"$build\".mtime = \"$mtime\"" "$tmp_json" > tmp && mv tmp "$tmp_json"
        jq ".[\"$branch\"].\"$build\".\"$type\" = {
            \"path\": \"$file\",
            \"size\": \"$size_human\",
            \"sha256\": \"$sha256\"
        }" "$tmp_json" > tmp && mv tmp "$tmp_json"
    fi
}

# Основной цикл
for file in php-*.zip; do
    [[ -f "$file" ]] || continue

    add_hashes "$file"

    version=$(extract_version "$file")
    branch=$(extract_branch "$version")
    build_key=$(extract_build_key "$file")

    case "$file" in
        *-src.zip)
            type="source"
            build=""
            ;;
        *-test-pack*.zip)
            type="test_pack"
            build=""
            ;;
        *-debug-pack-*.zip)
            type="debug_pack"
            build="$build_key"
            ;;
        *-devel-pack-*.zip)
            type="devel_pack"
            build="$build_key"
            ;;
        *)
            type="zip"
            build="$build_key"
            ;;
    esac

    add_to_json "$branch" "$version" "$type" "$build" "$file"
done

# Финальное форматирование JSON
jq '.' "$tmp_json" > releases.json
rm "$tmp_json"

echo "✅ Готово: releases.json, sha1sum.txt и sha256sum.txt созданы."