import subprocess
from pathlib import Path
import yaml
import snakemake
import pathlib


PROJECT_DIR = Path(config['project_path'])
RESOURCES_DIR = PROJECT_DIR / "resources"
INPUT_DATA = config['data']
SNAKE_DIR = Path(workflow.basedir)
SCRIPT_DIR = SNAKE_DIR / "scripts"


# overwrite config with project specific file
configfile: Path(config['project_path']) / "config.yaml"


# use the config file to update our default resources
_extra = config["all"].get("update_default_resources", None)
if _extra:
    for k, v in _extra.items():
        workflow.default_resources.set_resource(k, v)

# Ensure log directory exists. Required for cluster job submission.
# TODO(SM): Not sure if this is needed now with the slurm profile...
Path("logs").mkdir(exist_ok=True)


include: "rules/combine.smk"
include: "rules/align.smk"
include: "rules/tree.smk"
include: "rules/phytest.smk"
include: "rules/dynamicbeast.smk"
include: "rules/beast.smk"
include: "rules/report.smk"


onstart:
    # Print some environment info
    print("Input data:")
    for path in INPUT_DATA:
        print(f"- {path}")
    print("Workflow paths:")
    print(f"\t{'snakefile':20s} ➡  {workflow.snakefile}")
    print(f"\t{'working directory':20s} ➡  {workflow.basedir}")

    print("Environment:")
    shell = lambda cmd: subprocess.run(cmd, shell=True, stdout=subprocess.PIPE).stdout.decode().rstrip()
    print(f"\t{shell('python --version'):20s} ➡  {shell('which python')}")
    print(f"\t{' '.join(('snakemake', shell('snakemake --version'))):20s} ➡  {shell('which snakemake')}")
    print("Conda:")
    print(f"\t{'frontend':20s} ➡  {workflow.conda_frontend}")
    print(f"\t{'prefix':20s} ➡  {workflow.conda_prefix}")
    print(f"\t{'base path':20s} ➡  {workflow.conda_base_path}")


rule all:
    input:
        f"{config['id']}-report.html",
