#!/usr/bin/env python3

import birchenv
import birchscript
import os
import subprocess
import sys

#Version Jan. 11, 2020
# Run primer3 using the input file created by primer3b.py
#Synopsis: primer3run.py infile 

""" ensure that there are enough command line arguments to parse """
if len(sys.argv) < 2:
    print("Usage: primer3run.py  INFILE");
    exit();

#Convert arguments to variables
INFILE = sys.argv[1]
NAME   = os.path.splitext(INFILE)[0]
PID    = os.getpid() #process id

os.nice(10)
#subprocess.call(['primer3', '-format_output'], stdin=open(INFILE, 'r'), stdout=open(NAME + '.out', 'w'))
subprocess.call(['primer3_core', '-format_output'], stdin=open(INFILE, 'r'), stdout=open(NAME + '.out', 'w'))

# You can run primer3 withouth the -format_output option. All that does is writes the .out file in a more user-readable
# format. The other files are not affected.
#subprocess.call(['primer3'], stdin=open(INFILE, 'r'), stdout=open(NAME + '.out', 'w'))

os.nice(0)

os.remove(INFILE)


class primerfile:
    def __init__(self):
        """
        Used for working with Primer3 files for forward, reverse and internal primers.
        """
        self.lines = [] # lines from primerfile

    def readprimerfile(FN):
        F = open(FN,"r")

        F.close()

# Special code for text editors used by GDE and scripts called by GDE
# Nedit crashes in some Linux systems due to libraries set in BIRCHLIBS.
# nedit_wrapper unsets LD_LIBRARY_PATH before calling nedit.
# gedit opens all files in a single window. gedit_wrapper.sh forces
# gedit to open each file in different window.
# choose_edit_wrapper.sh returns the name of the wrapper to use
# for each editor, or just returNAME + '.out'ns BL_TextEditor if there is no
# wrapper.
#birchscript.Cleanrun([[birchenv.BL_TextEditor, NAME + '.out']], [NAME + '.out'], True)
#birchscript.Cleanrun([[birchenv.BL_TextEditor, NAME + '.for']], [NAME + '.for'], True)
#birchscript.Cleanrun([[birchenv.BL_TextEditor, NAME + '.rev']], [NAME + '.rev'], True)
#if os.path.exists(NAME + '.int'):
#    birchscript.Cleanrun([[birchenv.BL_TextEditor, NAME + '.int']], [NAME + '.int'])

subprocess.call(['chooseviewer.py', NAME + '.out'])
subprocess.call(['chooseviewer.py', NAME + '.for'])
subprocess.call(['chooseviewer.py', NAME + '.rev'])
if os.path.exists(NAME + '.int'):
    subprocess.call(['chooseviewer.py', NAME + '.int'])


