#!/usr/bin/env python3
"""Run from cb-allotment with the original local caches. --test-only runs checks.
Downloaded code alone does not include licensed raw market data.
"""
import argparse
from pathlib import Path
import subprocess
import sys

def main():
    parser=argparse.ArgumentParser(description=__doc__)
    parser.add_argument('--test-only',action='store_true')
    parser.add_argument('--full-search',action='store_true')
    args=parser.parse_args()
    root=Path(__file__).resolve().parents[4]
    entry=root/'research/etf_return20_20261003/backtest.py'
    if not entry.exists():parser.error('Run this script at its original path inside the cb-allotment repository with local data caches.')
    if args.test_only:
        for pattern in ['test_etf_practical_optimization.py','test_etf_return20_study.py']:
            subprocess.run([sys.executable,'-m','unittest','discover','-s','research','-p',pattern,'-v'],cwd=root,check=True)
        return
    command=[sys.executable,str(entry)]
    if args.full_search:
        if 'etf_return20_20261003'!='etf_return20_20261003':parser.error('--full-search is available on the return20 report.')
        command.append('--full-search')
    subprocess.run(command,cwd=root,check=True)
    subprocess.run([sys.executable,'research/publish_etf_reports.py'],cwd=root,check=True)

if __name__=='__main__':main()
