#!/usr/bin/env python3
"""Download or refresh citations from INSPIRE-HEP for all downloaded papers.\n
Scans the repository for all papers (identified by PDF files) and fetches
their citation data from INSPIRE-HEP. Downloads citations for papers that
don't yet have a _citations.bib file, and refreshes existing citation files
when the citation count has changed on INSPIRE-HEP.\n
After processing individual papers, merges all citation .bib files into
all_citations.bib, all_citations.md, and all_citations.html at the repository
root.\n
Options:
    --force   Re-download citations even if the count has not changed,
              otherwise only download when the count differs or the file
              is missing.\n
Usage:
    python scripts/sync_all_paper_citations.py
    python scripts/sync_all_paper_citations.py --force
"""

import argparse
import os
import sys

sys.path.insert(0, os.path.dirname(__file__))

from action_lib import combine_all_citations, sync_all_paper_citations
from inspire_lib import log_to_stderr

def main() -> None:
    """Parse CLI arguments and refresh citations for all downloaded papers."""
    parser = argparse.ArgumentParser(
        description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
    )
    parser.add_argument(
        "--force",
        action="store_true",
        help="Re-download even if the citation count has not changed",
    )
    args = parser.parse_args()
    #
    sync_all_paper_citations(args.force)
    #
    log_to_stderr("\nMerging all citations...")
    combine_all_citations()

if __name__ == "__main__":
    main()
