#!/usr/bin/env python3
#
# purpose: get the two alignment vectors which Igt10 uses
#          to build dictionaries that map between lang and trans 
import sys
import re

ID_RE = re.compile(r'Igt_id=\d+')

def get_align_info(a1reslines):
    sg_tag = '#######################  Q4: src and gloss alignment\n'
    gt_tag = '######################### Q5: gloss and translation alignment\n'
    align_dict = {}

    while len(a1reslines) > 0: # loop through all lines
        m = ID_RE.match(a1reslines[0]) 
        if m:  # found id, do an inner loop to find the info we need
            # print("found an id: "+m.group(0))
            sgalign = []
            gtalign = []

            while a1reslines[0] != sg_tag:
                a1reslines.pop(0)

            # ok found sg align
            # skip 3 lines then read
            # into the array
            a1reslines = a1reslines[4:]
            while a1reslines[0][0] != '#':
                sgalign.append(a1reslines[0].strip())
                a1reslines.pop(0)
            
            # okay get the next set of align
            while a1reslines[0] != gt_tag:
                a1reslines.pop(0)

            # ok found gt align
            # read it into the array
            a1reslines = a1reslines[4:]
            while a1reslines[0].strip() != '':
                gtalign.append(a1reslines[0].strip())
                a1reslines.pop(0)

            align_dict[m.group(0)] = {}
            align_dict[m.group(0)]['sg'] = sgalign
            align_dict[m.group(0)]['gt'] = gtalign
                 
 
        else: # no match, keep looping
            a1reslines.pop(0)
          
    return align_dict             
  
    

def parse_igt(igt_lines):
    igt_items = []
    igt_blk = []
    while len(igt_lines) > 0:
        # print("dealing with line: [" + igt_lines[0] + "]")
        if igt_lines[0].strip() == '': 
            # print("discard and new block")
            # create a new igt block
            igt_items.append(igt_blk)
            igt_blk = []
            igt_lines.pop(0)
        else:
            # print("part of igt item")
            igt_blk.append(igt_lines[0])
            igt_lines.pop(0)

    # finish off the last igt block
    igt_items.append(igt_blk)

    return igt_items        
        

if __name__ == '__main__':
    a1resfile = sys.argv[1]
    igtfile = sys.argv[2]

    # create an array of igt items
    igt_lines = open(igtfile).readlines()
    igt_items = parse_igt(igt_lines)

    # get the a1res lines
    a1reslines = open(a1resfile).readlines()
    align_dicts = get_align_info(a1reslines)

#    for k in align_dicts:
#        print(k+": "+str(align_dicts[k]['sg'])+"\n        "+str(align_dicts[k]['gt']))

    out = open(igtfile+".all",'w')
    for i in range(0,len(igt_items)):
        # get the id
        id_str = ID_RE.match(igt_items[i][0]).group(0)
        for l in igt_items[i]:
            if l.find("\n") == -1:
                # print("appending newline to igt",file=sys.stderr)
                l+="\n"
            # else:
                # print("igt line ends in newline already",file=sys.stderr)
                # print(l,file=sys.stderr)
            out.write(l) 

        out.write("\t".join(align_dicts[id_str]['sg']) + "\n")
        out.write("\t".join(align_dicts[id_str]['gt']) + "\n")
        if i+1 != len(igt_items):
            out.write("\n")
