#!/usr/bin/python
# -*- coding: utf-8 -*-
#PYTHON_ARGCOMPLETE_OK

# <one line to give the library's name and an idea of what it does.>
# Copyright (C) 2014  <copyright holder> <email>
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

__doc__ = """
Sprawdza czy w zestawie grafow znajdują się zdania z paczek lingiwstów.
"""

import shutil
import os, sys
import gentbase

def check_directory(gdir, pack_dir, graph_dict):
  for pfile in os.listdir(pack_dir):
    pfilepath = os.path.join(pack_dir, pfile)
    print >> sys.stderr, "Checking file: {}".format(pfilepath)
    docdict = {}
    with open(pfilepath) as pf:
      lastkey = None
      for line in pf:
        line = line.strip()
        if line.find('Dokument: ') != -1:
          lastkey = line.split('Dokument: ')[1]
          docdict[lastkey] = []
          continue
        if not lastkey:
          print >> sys.stderr, "ERROR!"
          return -1
        if line.startswith('==='):
          continue
        spline = line.split()
        if len(spline) < 2: 
          continue
        docdict[lastkey].append(int(line.split()[0]))
        for k, vals in docdict.iteritems():
          for v in vals:
            gname = '{}#s{}.pickle'.format(k, v)
            gpath = os.path.join(gdir, gname)
            if os.path.exists(gpath):
              # print >> sys.stderr, '[JEST]', gpath
              #
              # gdestdir="/mnt/dane_eksperymenty/entailment/grafon-experiments/wikinews-graphs/"
              # gdistpath=os.path.join(gdestdir, gname)
              # shutil.copyfile(gpath, gdistpath)
              pass
            else:
              print >> sys.stderr, '[BRAK]', gpath

def main(argv=None):
  p = gentbase.make_parser(__doc__)
  args = p.parse_args(argv)

  gdict = gentbase.read_graphs(args.graphs_dir)
  for pdir in os.listdir(args.pack_dir):
    check_directory(
      args.graphs_dir, 
      os.path.join(args.pack_dir, pdir), 
      gdict)

if __name__ == '__main__':
  main()