#
#  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#  SLEPc - Scalable Library for Eigenvalue Problem Computations
#  Copyright (c) 2002-2015, Universitat Politecnica de Valencia, Spain
#
#  This file is part of SLEPc.
#
#  SLEPc is free software: you can redistribute it and/or modify it under  the
#  terms of version 3 of the GNU Lesser General Public License as published by
#  the Free Software Foundation.
#
#  SLEPc  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 Lesser General Public  License  for
#  more details.
#
#  You  should have received a copy of the GNU Lesser General  Public  License
#  along with SLEPc. If not, see <http://www.gnu.org/licenses/>.
#  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#

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 \
	  DOCTEXT_PATH=${PETSC_DIR}/src/docs/tex/doctext; export DOCTEXT_PATH; \
	  ${DOCTEXT} -html \
		-mpath ${LOC}/docs/manualpages/${MANSEC} -heading SLEPc \
		-defn ${SLEPC_DIR}/src/docs/tex/doctext/html.def \
		-locdir ${LOCDIR} -mapref ${LOC}/docs/manualpages/manualpages.cit \
		${SOURCEC} ${SOURCEH}; \
		chmod -f g+w ${LOC}/docs/manualpages/${MANSEC}/* ; fi

slepc_manexamples:
	-@base=`basename ${LOCDIR}`; \
        if [ "$${base}" = "tutorials" ] ; 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 -o /dev/null | sort | uniq` ;  \
              for j in $$a ; do \
                b=`ls ${LOC}/docs/manualpages/*/$${j}.html | cut -f9` ; \
                l=`grep tutorials $${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 ${SOURCEC} ${SOURCEF} ${SOURCEH} ${EXAMPLESC} ${EXAMPLESF} ${EXAMPLESCH} ${EXAMPLESFH} ${EXAMPLESMATLAB} foo ; do\
	    if [ -f $$i ]; then \
              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"  $$i | ${C2HTML} -n  | ${PETSC_DIR}/bin/maint/fixinclude $$i $${ROOT} | \
              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 char help" $${file} | cut -d\" -f2 | cut -d\. -f1`; \
                   echo "<a href=\"$${file}.html\">$${file}: $${cmess}</a><br>" >> $${loc}/index.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 ${SOURCEH} ${SOURCEC} ${SOURCEF} 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 tmp

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 \
              if [ "${INSTALLED_PETSC}" != "" ]; then \
                PETSCCONF="${PETSC_DIR}/include/petscconf.h" ; \
              else \
                PETSCCONF="${PETSC_DIR}/${PETSC_ARCH}/include/petscconf.h" ; \
              fi; \
	      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 [ ${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} NO_PRINT_DIR=${NO_PRINT_DIR});\
	  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_KSP_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} V=${V}
	@echo "========================================="

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