#!/usr/bin/env bash

source ${HOME}/.secrets
function get_package_info(){
  packages_array=()

  while read i; do
#    echo "${i}"
    package_name=$(jq -r '.packageName' <<<"${i}")
    upstream_url=$(jq -r '.upstreamURL' <<<"${i}")
    package_version=$(apt-cache show "${package_name}" | grep Version | awk '{print $2}' | head -n1)
#    package_version=$(dpkg-query -W -f='${Version}' "${package_name}")
    upstream_version=$(lastversion "${upstream_url}")
    if [[ "${package_version}" == *"${upstream_version}"* ]]; then
      up_to_date='true'
    else
      up_to_date='false'
    fi
    package_json=$( jq -n --arg name "${package_name}" --arg pkgver "${package_version}" --arg upstreamver "${upstream_version}" --arg uptodate "${up_to_date}" '{package_name: $name, package_version: $pkgver, upstream_version: $upstreamver, up_to_date: $uptodate}' )
    packages_array+=("${package_json}")
  done < <(jq -c '.[]' "${HOME}/.config/software-release-monitor/config.json")
packages_json=$(printf '%s\n' "${packages_array[@]}" | jq -s .)
echo "${packages_json}"

#echo "${package_array_item}"
#echo "${package_array[@]}"
}

get_package_info

#printf '%s\n' "${package_array[@]}" | jq -s .


#dpkg-query -W -f='${Version}' swayfx
