import glob
import attrmap as ap
import attrmap.utils as au


configfile: os.path.join(workflow.basedir, '../', 'config', 'config.yaml')
configfile: os.path.join(workflow.basedir, '../', 'config', 'system_config.yaml')
config = ap.AttrMap(config)


def copy_log_file():
    """Concatenate Snakemake's own logfile with the CLI logfile"""
    files = glob.glob(os.path.join(".snakemake", "log", "*.snakemake.log"))
    if files:
        current_log = max(files, key=os.path.getmtime)
        shell("cat " + current_log + " >> " + config.args['log'])

onsuccess:
    copy_log_file()

onerror:
    copy_log_file()


# Import rules
include: os.path.join("rules", "preflight.smk")
include: os.path.join("rules", "hostRemoval.smk")
include: os.path.join("rules", "fastp.smk")
include: os.path.join("rules", "prinseq.smk")
include: os.path.join("rules", "roundAB.smk")
include: os.path.join("rules", "nanopore.smk")
include: os.path.join("rules", "notrim.smk")
include: os.path.join("rules", "reports.smk")


# Mark target rules
target_rules = []
def targetRule(fn):
    assert fn.__name__.startswith('__')
    target_rules.append(fn.__name__[2:])
    return fn


@targetRule
rule fastp:
    input:
        targets.fastp,
        targets.reports


@targetRule
rule prinseq:
    input:
        targets.prinseq,
        targets.reports


@targetRule
rule roundAB:
    input:
        targets.roundAB,
        targets.reports


@targetRule
rule nanopore:
    input:
        targets.nanopore,
        targets.reports


@targetRule
rule notrim:
    input:
        targets.notrim,
        targets.reports


@targetRule
rule print_targets:
    run:
        print("\nTop level rules are: \n", file=sys.stderr)
        print("* " + "\n* ".join(target_rules) + "\n\n", file=sys.stderr)
