#!/home/kr2/GitHub/cancerit/telomerecat/venv/bin/python3
# main
import textwrap
import pkg_resources
import sys

# Do not remove these imports
# Crucial for pyinstaller (as of v2.0)
from telomerecat.bam2length import Bam2Length
from telomerecat.bam2telbam import Bam2Telbam
from telomerecat.telbam2length import Telbam2Length
from telomerecat.csv2length import Csv2Length

#from telomerecat2.cli import cli as tc2_cli

import parabam


def handle_input():
  command_map = {
    "bam2length": Bam2Length,
    "bam2telbam": Bam2Telbam,
    # "bam2telbam": tc2_cli,
    "telbam2length": Telbam2Length,
    "csv2length": Csv2Length,
  }

  help_text = get_help_text()

  cmd_interface = parabam.core.CmdLineInterface(program_name="telomerecat")
  cmd_interface.handle(command_map, help_text)


def get_help_text():
  help_text = (
    textwrap.dedent(
      """\

    telomerecat
    -----------------------------------------------------------------------------

    About:
      Telomere Computational Analysis Tool

    Version:
      %s

    Usage:
      telomerecat <command> [options] input:{BAM/TELBAM}

    Quick Start Example:

      telomerecat bam2length -p8 { path_to_bamfile(s) }

      The above genereates a CSV file containing an estimation of average
      telomere length for BAM files provided.

    More:
      Users may wish to segregate the heavy lifting process of creating
      a telbam (essentially a subset of telomeric reads in a BAM file)
      from the reletively quick process of generating the length estimation.

      Generating telbams first, (with the command `bam2telbam`) allows
      users to run multiple analysis on the same file without having to
      run the entire process multiple times. Type `telomerecat bam2telbam`
      for more information.

      If you have already generated a set of telbams and just want to
      run length estimation on these files type `telomerecat telbam2length`
      for more information.

    Commands:
      bam2length      Estimate telomere length within BAM files
      bam2telbam      Create telbams from BAM files
      telbam2length   Estimate telomere length within TELBAMs
      csv2length      Estimate telomere length using bam2length csv files
      version         Prints the version and exits
    """.expandtabs()
    )
    % (pkg_resources.require("telomerecat")[0].version,)
  )
  return help_text


if __name__ == "__main__":
  if len(sys.argv) >= 2:
    if sys.argv[1] == 'version':
      print(pkg_resources.require("telomerecat")[0].version)
      sys.exit(0)

  handle_input()
