import sys
import os
import time
import datetime
import pymqi
import glob

now = datetime.datetime.now()

# Import VIMS .py
import default_setting

# Get Current Path
dir_path = os.path.dirname(os.path.realpath(__file__))

def main(argvs):
# get Upload Directories from company settings
	defaults = default_setting.defaultSettings()
	uploadDirs = default_setting.companySettings(defaults)
	print(uploadDirs['interfacein'])
	
	return read_file(argvs,uploadDirs['interfaceout'])	
	
def read_file(argvs, directory):
	for file_name in glob.glob(directory+'/*.txt'):
		message_file = open(file_name,"r")
		message_string = ""
		i = 0
		for line in message_file:
			line = line.replace('\n','')
			message_string +=line
			i=i+1
		message_file.close()
	
#		return file_name+' '+str(i)+message_string
	
		if sys.version_info[0] < 3:
			message = str(message_string)
		else:
			message = bytes(message_string, 'utf-8')
		put_message(argvs, message)
		
	return 'OK'
#	return message
		
def put_message(argvs, message):

	queue_manager = 'VIMSRRI10'
	channel = 'SYSTEM.DEF.SVRCONN'
	host = '172.18.21.5'
	port = '1415'
#	queue_name = 'BMW.LSP_SHPCON.VIMS'
	queue_name = 'VIMS.LSP_ASN.BMW'
	
# load values from input params
	if (len(argvs) > 1):
		queue_manager = argvs[1]
	if (len(argvs) > 2):
		queue_name = argvs[2]
	if (len(argvs) > 3):
		host = argvs[3]
	if (len(argvs) > 4):
		port = argvs[4]
	if (len(argvs) > 5):
		channel = argvs[5]
	if (len(argvs) > 6):
		directory = argvs[6]

	if (queue_name == 'VIMS.LSP_ASN.BMW'):
		prefix = 'ASN'
	if (queue_name == 'VIMS.LSP_SHP.BMW'):
		prefix = 'PCK'	
	
	conn_info = '%s(%s)' % (host, port)

	qmgr = pymqi.connect(queue_manager, channel, conn_info)
	pcf = pymqi.PCFExecute(qmgr)
		
	putq = pymqi.Queue(qmgr, queue_name)

	md = pymqi.MD()
	md.Format = pymqi.CMQC.MQFMT_STRING
	putq.put(message, md, None)

	putq.close()
	qmgr.disconnect()

# Initial Processing
if __name__ == '__main__':
	result = main(sys.argv)
if (result != "OK"):
	print("FAIL-",result)
else:
	print("OK")