#
#  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#  SLEPc - Scalable Library for Eigenvalue Problem Computations
#  Copyright (c) 2002-, Universitat Politecnica de Valencia, Spain
#
#  This file is part of SLEPc.
#  SLEPc is distributed under a 2-clause BSD license (see LICENSE).
#  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#

chk_slepcdir:
	@mypwd=`pwd`; cd ${SLEPC_DIR} 2>&1 > /dev/null; true_SLEPC_DIR=`pwd`; cd $${mypwd} 2>&1 >/dev/null; \
        newpwd=`echo $${mypwd} | sed "s+$${true_SLEPC_DIR}+DUMMY+g"`;\
        hasslepc=`echo $${mypwd} | sed "s+slepc-+DUMMY+g"`;\
        if [ $${mypwd} = $${newpwd} -a $${hasslepc} != $${mypwd} ]; then \
          printf ${PETSC_TEXT_HILIGHT}"*********************Warning*************************\n" ; \
          echo "Your SLEPC_DIR may not match the directory you are in";\
          echo "SLEPC_DIR " $${true_SLEPC_DIR} "Current directory" $${mypwd};\
          printf "******************************************************"${PETSC_TEXT_NORMAL}"\n" ; \
        fi

chk_in_slepcdir:
	@if [ ! -f include/slepcversion.h ]; then \
          printf ${PETSC_TEXT_HILIGHT}"*********************** ERROR **********************************************\n" ; \
          echo " This target should be invoked in top level SLEPc source dir!"; \
          printf "****************************************************************************"${PETSC_TEXT_NORMAL}"\n" ;  false; fi

slepc_manualpages:
	-@if [ "${MANSEC}" = "" ] ; then \
          for f in ${SOURCED}; do \
            LMANSEC=`grep SUBMANSEC $${f} | sed s'?[ ]*/\*[ ]*SUBMANSEC[ ]*=[ ]*\([a-zA-Z]*\)[ ]*\*/?\1?'g`; \
            if [ "$${LMANSEC}" = "" ] ; then LMANSEC="MissingSUBMANSEC"; fi; \
            DOCTEXT_PATH=${PETSC_DIR}/doc/classic/doctext; export DOCTEXT_PATH; \
            ${DOCTEXT} -html \
                   -mpath ${LOC}/docs/manualpages/$${LMANSEC} -heading SLEPc \
                   -defn ${SLEPC_DIR}/src/docs/doctext/html.def \
                   -locdir ${LOCDIR} -mapref ${LOC}/docs/manualpages/htmlmap -Wargdesc $${f} 2>&1 | tee -a ${SLEPC_DIR}/${PETSC_ARCH}/manualpages.err; \
            if [ -f "${LOC}/docs/manualpages/$${LMANSEC}" ]; then chmod g+w "${LOC}"/docs/manualpages/$${LMANSEC}/*; fi; \
          done; \
        else \
          if [ "${SUBMANSEC}" = "" ] ; then LMANSEC=${MANSEC}; else LMANSEC=${SUBMANSEC}; fi; \
          DOCTEXT_PATH=${PETSC_DIR}/doc/classic/doctext; export DOCTEXT_PATH; \
          ${DOCTEXT} -html \
                 -mpath ${LOC}/docs/manualpages/$${LMANSEC} -heading SLEPc \
                 -defn ${SLEPC_DIR}/src/docs/doctext/html.def \
                 -locdir ${LOCDIR} -mapref ${LOC}/docs/manualpages/htmlmap -Wargdesc ${SOURCED} 2>&1 | tee -a ${SLEPC_DIR}/${PETSC_ARCH}/manualpages.err; \
          if [ -f "${LOC}/docs/manualpages/$${LMANSEC}" ]; then chmod g+w "${LOC}"/docs/manualpages/$${LMANSEC}/*; fi; \
        fi

slepc_manexamples:
	-@base=`basename ${LOCDIR}`; \
        if [ "$${base}" = "tutorials" -o "$${base}" = "nlevp" ] ; then \
          echo "Generating manual example links" ; \
          for i in ${EXAMPLESALL} foo ; do \
            if [ "$$i" != "foo" ] ; then \
              a=`cat $$i | ${MAPNAMES} -map ${LOC}/docs/manualpages/manualpages.cit \
                   -printmatch-link -o /dev/null| cut -f 2 | cut -d '#' -f 1 |sed -e s~^../~~  | sort | uniq` ;  \
              for j in $$a ; do \
                b=`ls ${LOC}/docs/manualpages/$${j} | grep -v /all/ | cut -f9` ; \
                l=`grep -e "^<A HREF=\"\.\./\.\./\.\..*/tutorials/" -e "^<A HREF=\"\.\./\.\./\.\..*/nlevp/" $${b} | wc -l`; \
                if [ $$l -le 10 ] ; then \
                  if [ $$l -eq 0 ] ; then \
                    echo "<P><H3><FONT COLOR=\"#883300\">Examples</FONT></H3>" >> $$b; \
                  fi; \
                  echo  "<A HREF=\"../../../BB\">BB</A><BR>" | sed s?BB?${LOCDIR}$$i.html?g >> $$b; \
                  grep -v /BODY $$b > ltmp; \
                  echo "</BODY></HTML>" >> ltmp; \
                  mv -f ltmp $$b; \
                fi; \
              done; \
            fi; \
          done; \
        fi

petsc_manualpages_buildcite:
	-@export petscidx_tmp=$$(mktemp -d); \
          if [ ! -d "${LOC}/docs/manualpages" ]; then ${MKDIR} ${LOC}/docs/manualpages; fi; \
          echo Generating index of PETSc man pages; \
          petscrelease=`grep '^#define PETSC_VERSION_RELEASE ' ${PETSC_DIR}/include/petscversion.h |tr -s ' ' | cut -d ' ' -f 3`; \
          if [ $${petscrelease} = 1 ]; then petscbranch="release"; else petscbranch="main"; fi; \
          DOCTEXT_PATH=${PETSC_DIR}/doc/classic/doctext; export DOCTEXT_PATH; \
          TEXTFILTER_PATH=${PETSC_DIR}/doc/classic/doctext; export TEXTFILTER_PATH; \
          cp ${PETSC_DIR}/include/*.h $$petscidx_tmp; \
          doctext_common_def=${PETSC_DIR}/doc/classic/doctext/doctextcommon.txt; \
          for f in `ls $${petscidx_tmp}`; do \
            LMANSEC=`grep SUBMANSEC $${petscidx_tmp}/$${f} | sed s'?[ ]*/\*[ ]*SUBMANSEC[ ]*=[ ]*\([a-zA-Z]*\)[ ]*\*/?\1?'g`; \
            if [ "$${LMANSEC}" = "" ]; then LMANSEC="MissingSUBMANSEC"; fi; \
            if [ ! -d $${petscidx_tmp}/$${LMANSEC} ]; then ${MKDIR} $${petscidx_tmp}/$${LMANSEC}; fi; \
            ${DOCTEXT} -html -indexdir "https://petsc.org/$${petscbranch}/docs/manualpages/$${LMANSEC}" \
              -index $${petscidx_tmp}/petscmanualpages.cit \
              -mpath $${petscidx_tmp}/$${LMANSEC} $${doctext_common_def} $${petscidx_tmp}/$${f}; \
          done; \
          if [ -d ${PETSC_DIR}/src ]; then \
            for f in `find ${PETSC_DIR}/src \( -name tutorials -o -name tests -o -name "ftn-*" \) -prune -o -name "*.c" -print`; do \
              base=`basename $$f`; \
              makef=$${f%$${base}}makefile; \
              if [ -f $${makef} ]; then \
                LMANSEC=`grep SUBMANSEC $${makef} | cut -d= -f2 | tr -d " \t"`; \
                if [ "$${LMANSEC}" = "" ]; then LMANSEC=`grep MANSEC $${makef} | cut -d= -f2 | tr -d " \t"`; fi; \
                if [ "$${LMANSEC}" != "" ]; then \
                  if [ ! -d $${petscidx_tmp}/$${LMANSEC} ]; then ${MKDIR} $${petscidx_tmp}/$${LMANSEC}; fi; \
                  ${DOCTEXT} -html -indexdir "https://petsc.org/$${petscbranch}/docs/manualpages/$${LMANSEC}" \
                    -index $${petscidx_tmp}/petscmanualpages.cit \
                    -mpath $${petscidx_tmp}/$${LMANSEC} $${doctext_common_def} $${f}; \
                fi; \
              fi; \
            done; \
          fi; \
          cp $${petscidx_tmp}/petscmanualpages.cit ${LOC}/docs/manualpages; \
          ${RM} -r $${petscidx_tmp}

slepc_manualpages_buildcite:
	@-DOCTEXT_PATH=${PETSC_DIR}/doc/classic/doctext; export DOCTEXT_PATH; \
          TEXTFILTER_PATH=${PETSC_DIR}/doc/classic/doctext; export TEXTFILTER_PATH; \
          if [ "${MANSEC}" = "" ] ; then \
            for f in ${SOURCED}; do \
              LMANSEC=`grep SUBMANSEC $${f} | sed s'?[ ]*/\*[ ]*SUBMANSEC[ ]*=[ ]*\([a-zA-Z]*\)[ ]*\*/?\1?'g`; \
              if [ "$${LMANSEC}" = "" ] ; then \
                LMANSEC="MissingSUBMANSEC"; \
                pwd | grep -e ftn-custom -e slepc/finclude -e slepc/private > /dev/null; fnd=$?; \
                if [ "$${fnd}" == "1" ]; then \
                  echo "Missing MANSEC or SUBMANSEC definition in " `pwd`/$${f} ; \
                fi; \
              fi; \
              if [ ! -d "${LOC}/docs/manualpages/$${LMANSEC}" ] && ([ "$${LMANSEC}" != "MissingSUBMANSEC" ] || [ "$${fnd}" == "1" ]); then \
                echo Making directory ${LOC}/docs/manualpages/$${LMANSEC} for manual pages from $${f}; ${MKDIR} ${LOC}/docs/manualpages/$${LMANSEC}; \
              fi; \
              ${DOCTEXT} -html -indexdir ../$${LMANSEC} \
                -index ${LOC}/docs/manualpages/manualpages.cit \
                -mpath ${LOC}/docs/manualpages/$${LMANSEC} $${f}; \
            done; \
          else \
            if [ "${SUBMANSEC}" = "" ] ; then LMANSEC=${MANSEC}; else LMANSEC=${SUBMANSEC}; fi; \
            if [ ! -d "${LOC}/docs/manualpages/$${LMANSEC}" ]; then \
              echo Making directory ${LOC}/docs/manualpages/$${LMANSEC} for manual pages; ${MKDIR} ${LOC}/docs/manualpages/$${LMANSEC}; \
            fi; \
            ${DOCTEXT} -html -indexdir ../$${LMANSEC} \
              -index ${LOC}/docs/manualpages/manualpages.cit \
              -mpath ${LOC}/docs/manualpages/$${LMANSEC} ${SOURCED}; \
          fi

slepc_html: chk_c2html
	-@export htmlmap_tmp=$$(mktemp) ;\
          sed -e s?man+../?man+ROOT/docs/manualpages/? ${LOC}/docs/manualpages/manualpages.cit > $$htmlmap_tmp ;\
          cat ${LOC}/docs/manualpages/petscmanualpages.cit >> $$htmlmap_tmp ;\
          cat ${PETSC_DIR}/doc/classic/mpi.www.index >> $$htmlmap_tmp ;\
          ROOT=`echo ${LOCDIR} | sed -e s?/[a-z]*?/..?g -e s?src/??g -e s?include/??g` ;\
          loc=`pwd | sed -e s?\$${SLEPC_DIR}?$${LOC}/?g -e s?/disks??g`;  \
          ${MKDIR} -p $${loc} ;\
          for i in ${SOURCEALL} ${EXAMPLESALL} foo ; do\
            if [ -f $$i ]; then \
              idir=`dirname $$i`;\
              if [ ! -d $${loc}/$${idir} ]; then ${MKDIR} -p $${loc}/$${idir}; fi ; \
              iroot=`echo $$i | sed -e "s?[a-z.]*/??g"`;\
              IROOT=`echo $${i} | sed -e s?[.][.]??g` ;\
              if [ "$${IROOT}" != "$${i}" ] ; then \
                IROOT=".."; \
              else \
                IROOT=$${ROOT};\
              fi;\
              ${RM} $${loc}/$$i.html; \
              echo "<center><a href=\"$${iroot}\">Actual source code: $${iroot}</a></center><br>" > $${loc}/$$i.html; \
              sed -E -e "/PetscCallBack|PetscCallBLAS|PetscCallExternal|PetscCallP4est|PetscCallHDF5|PetscCallParmetis|PetscCallMKLSparse|PetscCallELPA|PetscCallEVSL|PetscCallMAGMA/!s/PetscCall[A-Za-z_]*\((.*)\)\;/\1;/g" \
                     -e "s/\;ierr\;/;/g" \
                     -e "/\s*PetscFunctionBegin\;/d" \
                     -e "s/PetscFunctionReturn\(([-a-zA-Z_0-9]+)\)/return \1/g" \
                     -e "s/PetscFunctionReturnVoid\(\)/return/g" \
                     -e "s/ierr\s+=\s+//g" \
                     -e "s/PETSC[A-Z]*_DLLEXPORT//g" $$i | ${C2HTML} -n | \
              awk '{ sub(/<pre width="80">/,"<pre width=\"80\">\n"); print }' | ${PYTHON} ${PETSC_DIR}/lib/petsc/bin/maint/fixinclude.py $$i $${SLEPC_DIR} | \
              grep -E -v '(PetscValid|PetscFunctionBegin|PetscCheck|PetscErrorCode ierr;|#if !defined\(__|#define __|#undef __|EXTERN_C )' | \
              ${MAPNAMES} -map $$htmlmap_tmp -inhtml | sed -e s?ROOT?$${IROOT}?g >> $${loc}/$$i.html ; \
            fi; \
          done ;\
          ROOT=`echo ${LOCDIR} | sed -e s?/[a-z]*?/..?g -e s?src/??g -e s?include/??g` ;\
          loc=`pwd | sed -e s?\$${SLEPC_DIR}?$${LOC}/?g -e s?/disks??g`; ${RM} $${loc}/index.html; \
          cat ${SLEPC_DIR}/src/docs/manualpages-sec/header_${MANSEC} | sed -e "s?<A HREF=\"SLEPC_DIR[a-z/]*\">Examples</A>?<A HREF=\"$${ROOT}/docs/manualpages/${MANSEC}\">Manual pages</A>?g" -e "s?SLEPC_DIR?$${ROOT}/?g"> $${loc}/index.html; \
          echo "<p>" >> $${loc}/index.html ;\
          loc=`pwd | sed -e s?\$${SLEPC_DIR}?$${LOC}/?g -e s?/disks??g`;\
          base=`basename ${LOCDIR}`; \
          if [ "$${base}" = "tutorials" -o "$${base}" = "nlevp" ] ; then \
            for file in ${EXAMPLESALL} foo ; do \
              if [ -f $$file ]; then \
                if [ "$${file%.F}.F" = "$${file}" -o "$${file%.F90}.F90" = "$${file}" ]; then \
                  cmess=`grep "Description:" $${file} | cut -d: -f2`; \
                else \
                  cmess=`grep "static\( const\)\? char help" $${file} | cut -d\" -f2 | cut -d\. -f1`; \
                fi; \
                echo "<a href=\"$${file}.html\">$${file}: $${cmess}</a><br>" >> $${loc}/index.html;\
                ${PYTHON} ${PETSC_DIR}/lib/petsc/bin/maint/latexinexamples.py $${loc}/$${file}.html;\
              fi; \
            done ;\
          else \
            for file in ${DIRS} foo; do \
              if [ -d $$file ]; then \
                echo "<a href=\"$${file}/\">$${file}/</a><br>" >> $${loc}/index.html; \
              fi; \
            done; \
            echo " " >> $${loc}/index.html; \
            for file in ${SOURCEALL} foo ; do \
              if [ -f $$file ]; then \
                echo "<a href=\"$${file}.html\">$${file}</a><br>" >> $${loc}/index.html; \
              fi; \
            done; \
            echo " " >> $${loc}/index.html; \
            echo "<a href=\"makefile.html\">makefile</a><br>" >> $${loc}/index.html; \
          fi ;\
          loc=`pwd | sed -e s?\$${SLEPC_DIR}?$${LOC}/?g -e s?/disks??g`; \
          cat makefile | ${C2HTML} | ${MAPNAMES} -map $$htmlmap_tmp -inhtml > $${loc}/makefile.html; \
          ${RM} $$htmlmap_tmp

${SLEPC_DIR}/${PETSC_ARCH}/lib/slepc/conf/files:
	@touch -t 197102020000 ${SLEPC_DIR}/${PETSC_ARCH}/lib/slepc/conf/files

${SLEPC_DIR}/${PETSC_ARCH}/tests/testfiles:
	@${MKDIR} -p ${SLEPC_DIR}/${PETSC_ARCH}/tests && touch -t 197102020000 ${SLEPC_DIR}/${PETSC_ARCH}/tests/testfiles

slepc_libs: ${SLEPC_DIR}/${PETSC_ARCH}/lib/slepc/conf/files ${SLEPC_DIR}/${PETSC_ARCH}/tests/testfiles
	+@cd ${SLEPC_DIR} && MAKEFLAGS="-j$(MAKE_NP) -l$(MAKE_LOAD) $(MAKEFLAGS)" ${OMAKE_PRINTDIR} -f gmakefile ${MAKE_PAR_OUT_FLG} V=${V} slepc_libs

getlinklibs_slepc:
	-@echo ${SLEPC_LIB}

getincludedirs_slepc:
	-@echo ${SLEPC_CC_INCLUDES}

vermin_slepc:
	@vermin -vvv -t=3.4- ${VERMIN_OPTIONS} ${SLEPC_DIR}/config

lint_slepc:
	${PYTHON3} ${SLEPC_DIR}/lib/slepc/bin/maint/slepcClangLinter.py $(LINTER_OPTIONS)

help-lint_slepc:
	@${PYTHON3} ${SLEPC_DIR}/lib/slepc/bin/maint/slepcClangLinter.py --help
	-@echo "Basic usage:"
	-@echo "   make lint_slepc <options>"
	-@echo
	-@echo "Options:"
	-@echo "  LINTER_OPTIONS=\"--linter_options ...\"  See above for available options"
	-@echo

include ${PETSC_DIR}/lib/petsc/conf/rules
include ${SLEPC_DIR}/${PETSC_ARCH}/lib/slepc/conf/slepcrules
