#!/usr/bin/env -S jq -f

def extract_fields: {
        pname : .Package,
        version : .Version,
        provides : .Package | sub("[0-9.-]*$"; ""),
        filename : .Filename,
        size : ."Installed-Size" | tonumber,
        sha256 : .SHA256,
        dependencies : .Depends,
    } ;

# parses dependencies into a list of [{.pname, .kind, .version}]
# some dependencies do not have a version specified, in which case, version = version = null
#
# example dependencies:
# intel-oneapi-common-vars (>= 2023.0.0-25325), intel-oneapi-common-licensing-2023.0.0

def split_dependencies : map(try(.dependencies |= split(",\\s?"; "")) // .dependencies |= []) ;

def match_version : capture("(?<pname>[a-zA-Z0-9_\\-.]*) *(\\((?<kind>[<>=]*) *(?<version>.*)\\))?"; "") ;

def parse_dependencies : map_values(.dependencies.[] |= match_version) ;

def sort_version_decreasing : sort_by(.version | split("[-.]"; "") | map(tonumber)) | reverse ;

map(extract_fields) | split_dependencies | parse_dependencies | sort_version_decreasing

# [.[] | select(.pname == "intel-hpckit") | .version]
