#!/bin/bash

set -eux
set -o pipefail

# The script needs to be run with V package prepared in data_v
# and Verilator package prepared in data_verilator.

# Note that config.json will contain output of this script.
# It will be basically data_v/config.json with "datasets" key removed
# so that it's regenerated by info-process with minor other modifications.
python3 <<END >config.json
import json

with open('data_v/config.json') as f:
  config = json.load(f)

with open('data_verilator/config.json') as f:
  verilator_config = json.load(f)

db_count_v = config['additional'].pop('db_count')
config['additional']['db_count_verilator'] = verilator_config['additional']['db_count']
config['additional']['db_count_v'] = db_count_v

config['timestamp'] = '`date +"%Y-%m-%dT%H:%M:%S.%3N%z"`'

del config['datasets']

print(json.dumps(config, indent=2))
END

_out_dir=data_both

# The order of INFO files influences order of datasets that will be
# generated based on passed INFO files and added to config.json.
info-process pack --output $_out_dir --config config.json \
    --coverage-files data_v/*.info data_verilator/*.info \
    --description-files data_verilator/*.desc data_v/*.desc \
    --extra-files data_v/logo.svg

cat $_out_dir/config.json

echo "Merged coverage data ready to be packaged in $PWD/$_out_dir"
