##############################################################################################
##############################################################################################
###########################                                     ##############################
###########################     GMP & NTL INSTALL MAKEFILE      ##############################
###########################                                     ##############################
##############################################################################################
##############################################################################################
#
# DESCRIPTION: Main Makefile that acts as installer for HElib, Afhel & Pyfhel. With it, there
#                 is no need to travel across directories unless some particular configuration is
#                 desired. Just type the next command and wait for everything to be finished.
#                     > sudo make
#
#                 You can also select which library to build&install (e.g.:sudo make Afhel), and
#                 clean all the subdirectories all at once with 'make clean'. Finally, 'make
#                 uninstall' allows the fast removal of all libraries
#
#
# AUTHOR: Alberto Ibarrondo (github @ibarrond)
# DATE: 17/10/2017
#   
#   
# LICENSE: GNU GPL v3
#  
#  This file is part of Pyfhel project
#
#  Pyfhel 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.
#
#  Pyfhel 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/>.
#


##############################################################################################
#                                   MAKEFILE VARIABLES                                       #
##############################################################################################

#.................................. LIBRARY VERSIONS  ........................................

GMP_VERSION=6.1.2
NTL_VERSION=10.5.0

GMP_DIR=gmp-$(GMP_VERSION)
NTL_DIR=ntl-$(NTL_VERSION)

LIBLINK=$(shell cd /usr/local/lib && ls libntl.so.??)

##############################################################################################
#                                         EASY INSTALL                                       #
##############################################################################################


all: downloads decompress gmp ntl linking

downloads:
	wget "https://gmplib.org/download/gmp/$(GMP_DIR).tar.bz2"
	wget "http://www.shoup.net/ntl/$(NTL_DIR).tar.gz"

decompress:
	bzip2 -df $(GMP_DIR).tar.bz2
	tar xvf $(GMP_DIR).tar
	tar xvf $(NTL_DIR).tar.gz

gmp:
	cd $(GMP_DIR) && ./configure
	$(MAKE) -C $(GMP_DIR)
	sudo $(MAKE) install -C $(GMP_DIR)

ntl:
	cd $(NTL_DIR)/src && ./configure NTL_GMP_LIP=on SHARED=on
	$(MAKE) -C $(NTL_DIR)/src
	sudo $(MAKE) install -C $(NTL_DIR)/src

linking:
	$(eval $LIBLINK:=$(shell cd /usr/local/lib && ls libntl.so.??))
	sudo ln -sf /usr/local/lib/$($LIBLINK) /usr/lib/$($LIBLINK)



$(info ----------------------GMP & NTL INSTALL MAKEFILE-----------------------)
$(info Dependencies= Pyfhel -> Afhel -> HElib -> NTL & GMP)
$(info $(LIBLINK) Automatic installer. GMP VERSION=$(GMP_VERSION) | NTL VERSION=$(NTL_VERSION))


