#!/usr/bin/env python3
from pathlib import Path
import json, re, sys
try:
    import yaml
except Exception:
    yaml = None
ROOT = Path(__file__).resolve().parents[1]
required = [
    'SKILL.md',
    'modules/ip-licensing-intake-router/SKILL.md',
    'modules/ip-licensing-project-workspace/SKILL.md',
    'modules/ip-licensing-source-research/SKILL.md',
    'modules/ip-licensing-trademark-logo-check/SKILL.md',
    'modules/ip-licensing-resource-value-map/SKILL.md',
    'modules/ip-licensing-gallery-system/SKILL.md',
    'modules/ip-licensing-product-mockup-system/SKILL.md',
    'modules/ip-licensing-final-introduction/SKILL.md',
    'modules/ip-licensing-delivery-qa/SKILL.md',
    'modules/ip-licensing-case-library/SKILL.md',
    'references/workflows/00-operating-model.md',
    'templates/PROJECT_CONTEXT.template.md',
]
errors=[]
for rel in required:
    if not (ROOT/rel).exists(): errors.append(f'missing {rel}')
for p in ROOT.rglob('SKILL.md'):
    text=p.read_text(encoding='utf-8')
    if not text.startswith('---'):
        errors.append(f'{p}: no frontmatter'); continue
    m=re.search(r'
---\s*
', text[3:])
    if not m:
        errors.append(f'{p}: no frontmatter close'); continue
    if yaml:
        fm=yaml.safe_load(text[3:m.start()+3])
        if not isinstance(fm, dict) or not fm.get('name') or not fm.get('description'):
            errors.append(f'{p}: missing name/description')
json.loads((ROOT/'skillpack.manifest.json').read_text(encoding='utf-8'))
if errors:
    print('
'.join(errors)); sys.exit(1)
print('ip-licensing-skillpack-ok')
