#!/usr/bin/env python
""" Adds links in the manual pages to tutorials that utilize the functions"""

import os
import errno
import subprocess
import shutil
import argparse


'''manexamples:
	-@base=$$(basename $$(pwd)); \
        if [ "$${base}" = "tutorials" ] ; then \
          petsc_dir=$$(realpath ${PETSC_DIR}); LOCDIR=$$(pwd | sed s"?$${petsc_dir}/??"g)/; \
          for i in ${EXAMPLESALL} foo ; do \
            if [ "$$i" != "foo" ] ; then \
              a=`cat $$i | ${MAPNAMES} -map ${LOC}/manualpages/manualpages.cit \
                   -printmatch-link -o /dev/null| cut -f 2 | cut -d '#' -f 1 |sed -e s~^../~~ | grep \\.md$$ | sort | uniq` ;  \
              for j in $$a ; do \
                b=`ls ${LOC}/manualpages/$${j} | grep -v /all/ | cut -f9` ; \
                l=`grep "^<A HREF=\"PETSC_DOC_OUT_ROOT_PLACEHOLDER.*/tutorials/" $${b} | wc -l`; \
                if [ $$l -le 10 ] ; then \
                  if [ $$l -eq 0 ] ; then \
                    printf "\n## Examples\n" >> $$b; \
                  fi; \
                  echo  "<A HREF=\"PETSC_DOC_OUT_ROOT_PLACEHOLDER/XX\">BB</A><BR>" | sed s?XX?$${LOCDIR}$$i.html?g | sed s?BB?$${LOCDIR}$$i?g >> $$b; \
                fi; \
              done; \
            fi; \
	  done; \
        fi
'''

'''
r = re.compile(".*cat")
newlist = list(filter(r.match, mylist)) # Read Note below
'''


def processfile(petsc_dir,dir,file,keyre,mdict,counts):
  '''Find all functions used in the tutorial and add links to the manual page for the function'''
  #print('Processing '+os.path.join(dir,file))
  with open(os.path.join(dir,file),'r') as fd:
    text = fd.read()
  found = list(set(keyre.findall(text)))
  for i in found: counts[i] = counts[i]+1

def processdir(petsc_dir,dir,keyre,mdict,counts):
  '''Loop over tutorials, call processfile() on each'''
  print('Processing '+dir)
  for file in os.listdir(dir):
    if os.path.isfile(os.path.join(dir,file)) and (file.endswith('.c') or file.endswith('.cxx')): processfile(petsc_dir,dir,file,keyre,mdict,counts)

def loadmanualpagescit(loc):
  '''Loads and parses the manualpages.cit file generated by Sowing doctext'''
  import re
  mdict = {}
  PATTERN = re.compile(r'man:\+(.*)\+\+(.*)\+\+\+\+man\+(.*)#.*')
  EXCLUDE_PATTERN = re.compile('PetscCall|Petsc[A-Z]*Int|PetscReal|PetscScalar|PetscBool|PetscComplex|PetscErrorCode|SETERR|PetscLog|PETSC_FALSE|PETSC_TRUE')
  with open(os.path.join(loc,'manualpages','manualpages.cit'),'r') as fd:
    text = fd.read()
  for line in  text.split():
    m = re.match(PATTERN, line)
    # print('Manual page '+m.group(1)+' location '+m.group(3))
    if re.match(EXCLUDE_PATTERN,m.group(1)): continue
    mdict[m.group(1)] = m.group(3)
  # sort to find enclosing names first
  mdict = dict(sorted(mdict.items(), key=lambda item: len(item[0]), reverse = True))
  keyre = re.compile('|'.join(list(mdict.keys())))
  return keyre,mdict

def main(petsc_dir,loc):
    keyre,mdict = loadmanualpagescit(loc)
    counts = dict(zip(mdict.keys(), [0]*len(mdict.keys())))
    for dirpath, dirnames, filenames in os.walk(os.path.join(petsc_dir,'src'),topdown=True):
      dirnames[:] = [d for d in dirnames if d not in ['output', 'ftn-custom', 'f90-custom', 'ftn-auto', 'f90-mod', 'tests', 'binding']]
      if dirpath.endswith('tutorials'):
        processdir(petsc_dir,dirpath,keyre,mdict,counts)
    print(counts['VecCreate'])

if __name__ == "__main__":
   main(os.path.abspath(os.environ['PETSC_DIR']),os.path.abspath(os.environ['LOC']))
