#!/usr/bin/env python3

import os
import sys
import json
import shutil

for root, dirs, files in os.walk('data/frames/v5'):
    for f in files:
        if not f.endswith('-side-by-side.jpg'):
            continue
        frame_idx = f.split('-')[0]
        parent_dir = os.path.basename(root)
        outd = f'/data/samples/frame-{parent_dir}-{frame_idx}'
        os.makedirs(outd, exist_ok=True)
        frame_file = os.path.join(root, f)
        if os.path.exists(frame_file):
            shutil.copy(frame_file, os.path.join(outd, 'frame.jpg'))
        qr_file = os.path.join(root, f'{frame_idx}-qr.jpg')
        if os.path.exists(qr_file):
            shutil.copy(qr_file, os.path.join(outd, 'frame-qr.jpg'))
        orig_file = os.path.join(root, f'{frame_idx}-orig.jpg')
        if os.path.exists(orig_file):
            shutil.copy(orig_file, os.path.join(outd, 'std.jpg'))
        side_by_side_file = os.path.join(root, f'{frame_idx}-side-by-side.jpg')
        if os.path.exists(side_by_side_file):
            shutil.copy(side_by_side_file, os.path.join(outd, 'side-by-side.jpg'))
        metadata_file = os.path.join(root, f'{frame_idx}')
        shutil.copy(metadata_file, os.path.join(outd, 'metadata.json'))