#
#  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#  SLEPc - Scalable Library for Eigenvalue Problem Computations
#  Copyright (c) 2002-2017, 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

slepc_manualpages:
	-@if [ "${MANSEC}" != "" ] ; then \
          if [ "${SUBMANSEC}" = "" ] ; then LMANSEC=${MANSEC}; else LMANSEC=${SUBMANSEC}; fi; \
       	  DOCTEXT_PATH=${PETSC_DIR}/src/docs/tex/doctext; export DOCTEXT_PATH; \
	  ${DOCTEXT} -html \
		-mpath ${LOC}/docs/manualpages/$${LMANSEC} -heading SLEPc \
		-defn ${SLEPC_DIR}/src/docs/tex/doctext/html.def \
		-locdir ${LOCDIR} -mapref ${LOC}/docs/manualpages/manualpages.cit ${SOURCED}; \
		chmod -f g+w ${LOC}/docs/manualpages/$${LMANSEC}/* ; fi

slepc_manexamples:
	-@base=`basename ${LOCDIR}`; \
        if [ "$${base}" = "tutorials" -o "$${base}" = "nlevp" ] ; then \
          echo "Generating manual example links" ; \
          for i in ${EXAMPLESC} ${EXAMPLESF} 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 tutorials -e 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

slepc_html: chk_c2html
	-@sed -e s?man+../?man+ROOT/docs/manualpages/? ${LOC}/docs/manualpages/manualpages.cit > /tmp/$$USER.htmlmap
	-@cat ${PETSC_DIR}/src/docs/mpi.www.index >> /tmp/$$USER.htmlmap
	-@ROOT=`echo ${LOCDIR} | sed -e s?/[a-z]*?/..?g -e s?src/??g -e s?include/??g` ;\
          loc=`pwd | sed -e s?\$${PETSC_DIR}?$${LOC}/?g -e s?/disks??g`;  \
          ${MKDIR} -p $${loc} ;\
          for i in ${SOURCEALL} ${EXAMPLESC} ${EXAMPLESF} ${EXAMPLESCH} ${EXAMPLESFH} ${EXAMPLESMATLAB} 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 "s/CHKERRQ(ierr);//g" -e "s/PetscFunctionReturn(0)/return(0)/g" -e "s/ierr [ ]*= //g"  -e "s/PETSC[A-Z]*_DLLEXPORT//g" $$i | ${C2HTML} -n  | \
              awk '{ sub(/<pre width=\"80\">/,"<pre width=\"80\">\n"); print }'  | ${PETSC_DIR}/bin/maint/fixinclude $$i $${PETSC_DIR} | \
              egrep -v '(PetscValid|PetscFunctionBegin|PetscCheck|PetscErrorCode ierr;|#if !defined\(__|#define __|#undef __|EXTERN_C )' | \
              ${MAPNAMES} -map /tmp/$$USER.htmlmap -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?\$${PETSC_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=\"PETSC_DIR[a-z/]*\">Examples</A>?<A HREF=\"$${ROOT}/docs/manualpages/${MANSEC}\">Manual pages</A>?g" -e "s?PETSC_DIR?$${ROOT}/?g"> $${loc}/index.html; \
          echo "<p>" >> $${loc}/index.html
	-@loc=`pwd | sed -e s?\$${PETSC_DIR}?$${LOC}/?g -e s?/disks??g`;\
            if [ "${EXAMPLESC}" != "" ] ; then \
               for file in ${EXAMPLESC} foo ; do \
		if [ -f $$file ]; then \
                   cmess=`grep "static\( const\)\? char help" $${file} | cut -d\" -f2 | cut -d\. -f1`; \
                   echo "<a href=\"$${file}.html\">$${file}: $${cmess}</a><br>" >> $${loc}/index.html;\
                   ${PYTHON} ${PETSC_DIR}/bin/maint/latexinexamples.py $${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; \
            fi ;\
            echo " " >> $${loc}/index.html; \
            echo "<a href=\"makefile.html\">makefile</a><br>" >> $${loc}/index.html
	-@loc=`pwd | sed -e s?\$${PETSC_DIR}?$${LOC}/?g -e s?/disks??g`;  \
	   cat makefile | ${C2HTML}  | ${MAPNAMES} -map /tmp/$$USER.htmlmap -inhtml > $${loc}/makefile.html
	-@${RM} /tmp/$$USER.htmlmap

buildmod_slepc:
	-@${OMAKE}  clean
	@${OMAKE}  libfastcompile
	@${OMAKE}  modcopy_slepc
	@${OMAKE}  clean

modcopy_slepc:
	@${CP} -f *.mod ${SLEPC_DIR}/${PETSC_ARCH}/include

slepc_tree: ${ACTION}
	-@for dir in ${DIRS} ftn-auto ftn-custom f90-custom; do \
            if [ -d $$dir ]; then \
              PETSCCONF="${PETSCCONF_H}"; \
	      r=`grep -w requirespackage $$dir/makefile`; \
              if [ "$$?" = 0 ]; then \
		PKGFLG=`echo $$r | cut -d \' -f2`;\
                grep -w "#define $${PKGFLG}" $${PETSCCONF} > /dev/null; \
                if [ "$$?" = 1 ]; then \
                  continue; \
                fi; \
              fi; \
	      r=`grep -w requiresfunction $$dir/makefile`; \
              if [ "$$?" = 0 ]; then \
		PKGFLG=`echo $$r | cut -d \' -f2`;\
                grep -w "#define $${PKGFLG}" $${PETSCCONF} > /dev/null; \
                if [ "$$?" = 1 ]; then \
                  continue; \
                fi; \
              fi; \
	      r=`grep -w requiresdefine $$dir/makefile`; \
              if [ "$$?" = 0 ]; then \
		PKGFLG=`echo $$r | cut -d \' -f2`;\
                grep -w "#define $${PKGFLG}" ${SLEPC_DIR}/${PETSC_ARCH}/include/slepcconf.h > /dev/null; \
                if [ "$$?" = 1 ]; then \
                  grep -w "#define $${PKGFLG}" $${PETSCCONF} > /dev/null; \
                  if [ "$$?" = 1 ]; then \
                    continue; \
                  fi; \
                fi; \
              fi; \
              r=`grep -w requireslanguage $$dir/makefile`; \
              if [ "$$?" = 0 ]; then \
                echo $$r | grep -w ${PETSC_LANGUAGE} > /dev/null; \
                if [ "$$?" = 1 ]; then \
                  continue; \
                fi; \
              fi; \
              r=`grep -w requiresscalar $$dir/makefile`; \
              if [ "$$?" = 0 ]; then \
                echo $$r |  grep -w ${PETSC_SCALAR} > /dev/null; \
                if [ "$$?" = 1 ]; then \
                  continue; \
                fi; \
              fi; \
              r=`grep -w requiresprecision $$dir/makefile`; \
              if [ "$$?" = 0 ]; then \
                echo $$r |  grep -w ${PETSC_PRECISION} > /dev/null; \
                if [ "$$?" = 1 ]; then \
                  continue; \
                fi; \
              fi; \
            else \
              continue; \
            fi; \
            (cd $$dir ; \
            if [ ${PRINT_PROGRESS}foo = dotfoo ] ; then printf "."; fi; \
            if [ ${PRINT_PROGRESS}foo = foo -o ${PRINT_PROGRESS}foo = yesfoo -o ${PRINT_PROGRESS}foo = dirfoo ] ; then echo ${ACTION} in: `pwd`; fi; \
            if [ ${NO_PRINT_DIR}foo = foo ] ; then echo ${ACTION} in: `pwd`; fi; \
            ${OMAKE} slepc_tree ACTION=${ACTION}  PETSC_ARCH=${PETSC_ARCH} LOC=${LOC} DATAFILESPATH=${DATAFILESPATH} BASE_DIR=${BASE_DIR} PRINT_PROGRESS=${PRINT_PROGRESS});\
	  done

# builds the SLEPc shared library
slepc_shared: chk_makej
	-@${OMAKE} PETSC_ARCH=${PETSC_ARCH} PETSC_DIR=${PETSC_DIR} SLEPC_DIR=${SLEPC_DIR} OTHERSHAREDLIBS="${PETSC_SNES_LIB} ${SLEPC_EXTERNAL_LIB}" shared_nomesg

# uses the cmake infrastructure to build/rebuild the libraries
slepc_ccmake: chk_makej
	@echo "Building SLEPc using CMake with ${MAKE_NP} build threads"
	@echo "=========================================="
	@cd ${SLEPC_DIR}/${PETSC_ARCH} && ${OMAKE} -j ${MAKE_NP} VERBOSE=${VERBOSE}
	@if [ "${BUILDSHAREDLIB}" = "yes" -a "${DSYMUTIL}" != "true" ]; then \
        echo "Running ${DSYMUTIL} on ${SHLIBS}";\
        for LIBNAME in ${SHLIBS}; do ${DSYMUTIL} ${INSTALL_LIB_DIR}/$$LIBNAME.${SL_LINKER_SUFFIX}; done; fi
	@echo "========================================="

slepc_cmake: chk_makej
	@${OMAKE} slepc_ccmake VERBOSE=1

slepc_gnumake: chk_makej
	@echo "Building SLEPc using GNU Make with ${MAKE_NP} build threads"
	@echo "=========================================="
	@cd ${SLEPC_DIR} && ${OMAKE_PRINTDIR} -f gmakefile -j ${MAKE_NP} ${MAKE_PAR_OUT_FLG} V=${V}
	@echo "========================================="

getlinklibs_slepc:
	-@echo ${SLEPC_LIB}

getincludedirs_slepc:
	-@echo ${SLEPC_CC_INCLUDES}

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