#!/usr/bin/env python3

import click
import os
import sys
import shutil
from pathlib import Path
from invoke import run
from tqdm import tqdm


# delete test data
datadir = Path(os.environ['TEST_DATA_DIR']).resolve()


def confirm(cmd, run=False):
    if click.confirm(cmd):
        if run:
            os.system(cmd)
    else:
        if run:
            print("Coward!")
        sys.exit(0)

confirm(f"rm -rf {str(datadir)}", run=True)

confirm(f"Create test data in {str(datadir)}")

# test data dir
test_datadir = Path(os.environ['TEST_DATA_DIR'])
print(str(test_datadir))
test_datadir.mkdir(parents=True)

# tests/data/src
src_dir = test_datadir / 'src'
print(str(src_dir))
run(f'gh release download v0.0.3 --archive tar.gz --output - | tar zx -C {str(test_datadir)}')
tarball = test_datadir / 'cptree-0.0.3'
tarball.rename(src_dir)

# tests/data/dst
dst_dir = test_datadir / 'dst'
print(str(dst_dir))
dst_dir.mkdir()

# tests/data/output/file_list
output_dir = test_datadir / 'output'
print(str(output_dir))
output_dir.mkdir()

SORT="sort $(get-default-sort-args)"

proc = run(f'find {str(src_dir)} -type f | {SORT}', hide=True)
paths = proc.stdout.strip().split('\n')

files = [ str(Path(path).relative_to(src_dir)) for path in paths ]

file_list = output_dir / 'file_list'
file_list.write_text('\n'.join(files) + "\n")

for hash in ['sha256']:
    sumfile = output_dir / f"reference.{hash}"
    print(str(sumfile))
    cmd = f"cd {str(src_dir)}; find . -type f -exec {hash}sum --tag \x5c\x7b\x5c\x7d \x5c; | sort"
    proc = run(cmd)
    sumfile.write_text(proc.stdout)

shared = Path('tests/data')
shutil.rmtree(shared, ignore_errors=True)
shutil.copytree(test_datadir, shared)

exclude_patterns = [
    'make/*',
    'tests/*',
    '.git/*'
]

exclude_file = shared / 'exclude_patterns'
exclude_file.write_text('\n'.join(exclude_patterns) + '\n')

# make expected results file
cmd = f"cd {str(src_dir)}; find . -type f"
proc = run(cmd)
files = sorted(proc.stdout.strip().split('\n'))

(shared / "expected_exclude_none").write_text('\n'.join(files) + '\n')

filtered_args = [f for f in files if (f == './README.md') or (f.startswith('./docs/'))]
args_files = [f for f in files if f not in filtered_args]
(shared / "expected_exclude_args").write_text('\n'.join(args_files) + '\n')

filtered_file = [f for f in files if f.startswith('./make/') or f.startswith('./tests/') or f.startswith('.git/')]
file_files = [f for f in files if f not in filtered_file]
(shared / "expected_exclude_file").write_text('\n'.join(file_files) + '\n')

both_files = [f for f in files if (f not in filtered_args) and (f not in filtered_file)]
(shared / "expected_exclude_both").write_text('\n'.join(both_files) + '\n')
