#! /usr/bin/env python

import os
import re
import shutil
import subprocess

from exceptionclasses import CustomError, CritError, SyntaxError, LogicError
import messages as messages
import metadata as metadata


class RunProgramDirective(object):

    def __init__(self, kwargs, program_bool = True):
        dict((k.lower(), v) for k, v in kwargs.iteritems())

        if program_bool:
            if 'program' in kwargs.keys():
                program_input = kwargs['program']
                self.program_path = os.path.dirname(program_input)
                program_base = os.path.basename(program_input)
                self.program_name, self.program_ext = os.path.splitext(program_base)
            else:
                raise SyntaxError(messages.syn_error_noprogram)

            if self.program_path == '':
                self.program_path = './'
        else:
            self.program_ext = ''

        if 'makelog' in kwargs.keys():
            self.makelog = kwargs['makelog']
            if self.makelog:
                self.makelog = os.path.abspath( self.makelog )
        else:
            self.makelog = os.path.abspath( metadata.settings['makelog_file'] )

        if 'option' in kwargs.keys():
            self.option = kwargs['option']
            self.option_dict = self.parse_options()
            self.option_overlap_error_check(kwargs)
        else:
            self.option = '';
            self.option_dict = {}

        if 'log' in kwargs.keys():
            self.log = os.path.abspath( kwargs['log'] )
        else:
            self.log = self.option_assigned('log', '')

        if 'lst' in kwargs.keys():
            self.lst = os.path.abspath( kwargs['lst'] )
        else:
            self.lst = self.option_assigned('lst', metadata.settings['output_dir'])

        if 'changedir' in kwargs.keys():
            self.changedir = kwargs['changedir']
        else:
            self.changedir = False

        if 'executable' in kwargs.keys():
            self.executable = kwargs['executable']
        else:
            self.executable = '';

        if 'args' in kwargs.keys():
            self.args = kwargs['args']
        else:
            self.args = '';

        if 'handout' in kwargs.keys():
            self.handout = kwargs['handout']
        else:
            self.handout = False
            
        if 'comments' in kwargs.keys():
            self.comments = kwargs['comments']
        else:
            self.comments = False
            
        if 'pdfout' in kwargs.keys():
            self.pdfout = os.path.abspath( kwargs['pdfout'] )
        else:
            pdfout_dir = ''
            if self.handout or self.comments:
                pdfout_dir = metadata.settings['temp_dir']
            else:
                pdfout_dir = metadata.settings['output_dir']
            self.pdfout = self.option_assigned('pdfout', pdfout_dir)

        self.osname = os.name
        self.option = self.update_option()


    def parse_options(self):
        option_list = self.option.strip().replace("=", " ").split()
        option_dict = {}