#! /bin/bash

set -eux
set -o pipefail

CVE_MOUNTS="--mount cve /buildstream-build --mount cve-reports /cve-reports --mount manifests /manifests"
GEN_CVE_REPORT="${BST} ${ARCH_OPT} shell freedesktop-sdk.bst:utils/generate-cve-report.bst --mount cve /buildstream-build"

if [[ ! -d "nvd-cve-database" ]] ; then
    git clone --depth=1 https://gitlab.com/freedesktop-sdk/nvd-cve-database.git
fi

${BST} ${ARCH_OPT} artifact pull gnomeos/devel/manifest.bst
${BST} artifact pull flatpak/platform/manifest.bst flatpak/sdk/manifest.bst
${BST} build freedesktop-sdk.bst:utils/generate-cve-report.bst

rm -rf cve/ manifests/ cve-reports/
mkdir cve/ manifests/ cve-reports/

${BST} artifact checkout flatpak/platform/manifest.bst --directory manifests/platform
${BST} artifact checkout flatpak/sdk/manifest.bst --directory manifests/sdk
${BST} ${ARCH_OPT} artifact checkout gnomeos/devel/manifest.bst --directory manifests/gnomeos

cp -r nvd-cve-database/nvd-cve-database/*.json.gz cve/

${GEN_CVE_REPORT} ${CVE_MOUNTS} -- generate_cve_report --db-path /buildstream-build --feed-version 2.0 /manifests/platform/usr/manifest.json /cve-reports/platform.html
${GEN_CVE_REPORT} ${CVE_MOUNTS} -- generate_cve_report --db-path /buildstream-build --feed-version 2.0 /manifests/sdk/usr/manifest.json /cve-reports/sdk.html
${GEN_CVE_REPORT} ${CVE_MOUNTS} -- generate_cve_report --db-path /buildstream-build --feed-version 2.0 /manifests/gnomeos/usr/manifest.json /cve-reports/gnomeos.html
