#!/usr/bin/env python3
from clipoverlap import clip
from clipoverlap.__version import __versionstr__
import getopt, os, errno
from sys import stdout, stdin, stderr, argv
if __name__ == '__main__':
    threads = 1
    maxTLen = 1000
    outFormat = ''
    ordered = False
    alternate = False
    trimTail = False
    clipOnly = False
    verbose = False

    #Command line options
    stderr.write("Clip Overlap {}\n".format(__versionstr__))
    ops, paths = getopt.gnu_getopt(argv[1:], 't:m:ho:svabc')
    if not len(ops) and not len(paths):
        stderr.write("Use -h for help.\n")
    else:
        for op, val in ops:
            if op == '-h':
                stderr.write("Clip overlapping reads from SAM/BAM/CRAM file\n"
                             "Use: clip [-tmabcosv] [input file path] [output file path]\n"
                             "If no paths are given stdin and stdout are used.\n"
                             "-t # Threads to use for processing (Default=1)\n"
                             "-m # Maximum template length guaranteeing no read overlap (Default=1000)\n"
                             "-a Alternate strand being clipped to avoid strand bias (RAM intensive)\n"
                             "-b Trim tails of reads that extend past end of mate. Used to trim barcode remnants.\n"
                             "-c Clip only, do not merge clipped region into mate.\n"
                             "-o [sbuc] Output format: s=SAM (Default), b=BAM compressed, bu=BAM uncompressed, c=CRAM\n"
                             "-s Maintain input order (High depth regions may fill RAM), if not set will output in arbitrary order (Minimal RAM)\n"
                             "-v Verbose status output\n")
                exit()
            elif op == '-t':
                threads = int(val or 1)
            elif op == '-m':
                maxTLen = int(val or 1000)
            elif op == '-a':
                alternate = True
            elif op == '-b':
                trimTail = True
            elif op == '-c':
                clipOnly = True
            elif op == '-o' and val != 's':
                outFormat += val
            elif op == '-s':
                ordered = True
            elif op == '-v':
                verbose = True

    if len(paths) > 1:
        if os.path.exists(paths[1]):
            raise FileExistsError(paths[1])
        elif not os.path.exists(os.path.dirname(paths[1])):
            try:
                os.makedirs(os.path.dirname(paths[1]))
            except OSError as e:
                if e.errno != errno.EEXIST:
                    raise

    clip(paths[0] if len(paths) and paths[0] != '-' else stdin, open(paths[1], 'wb+') if len(paths) > 1 and paths[1] != '-' else stdout, threads, maxTLen, outFormat, ordered, alternate, clipOnly, trimTail, verbose)