############################################################################################
#
#	Makefile For libssh2
#
#	Author: Han Chang
#	Date: 2022.04.13
#	
#	Description:
#		It will be used by xAgent implementation.
#		We will use the version curl-7.36.0 for xAgent.
#
#############################################################################################
include $(GPLTOPDIR)/config.mk
include $(GPLTOPDIR)/config.in

APP=libssh2-1.10.0
SUBDIRS := libssh2-1.10.0

CURL_DIR_PATH=$(shell pwd)

.PHONY:

## Due to new "make distclent" failed after 7.6x, so it created two folder to prepare static command
#all: curl_conf sub_clean_static curl curl_preinstall dist_clean libcurl_conf sub_clean libcurl
all: gen_conf libssh2 #libssh2_preinstall libcurl_conf sub_clean libcurl
	cd $(APP); make



#	@if [ ! -e $(SUBDIR)/Makefile.in ]; then \
    	echo "Untarring original $(APP) source"; \
	    (tar zxvf $(APP).tar.gz 2> /dev/null); \
	    (cd $(APP); ./configure --host=$(TOOLCHAIN_PREFIX) \
        --target=$(TOOLCHAIN_PREFIX) \
        --prefix=$(INSTALL_DIR) \
        --disable-mpers \
        --sbindir=$(INSTALL_DIR)/bin);  \
        fi;

# removed old folder to avoid mis-link.
gen_conf:
	if [ ! -e $(APP)/Makefile.in ]; then \
    	echo "Untarring original $(APP) source"; \
	    (tar zxvf $(APP).tar.gz 2> /dev/null); \
	    (ln -sf $(APP) libssh2); \
	    (cd $(APP); \
	    ./configure --host=$(TARGET_PLATFORM_ARCH)-linux --prefix=$(TARGETDIR) \
						--disable-examples-build \
						--with-ssl=$(LIB_OPENSSLDIR) \
						--with-libssl-prefix=$(LIB_OPENSSLDIR) LDFLAGS=-Wl,-rpath-link,$(LIB_OPENSSLDIR)/lib ); \
        fi
#	rm -rf $(CURL_DIR)
#	ln -sf $(CURL_DIR) libssh2
#	cd libssh2; \
#	./configure --host=$(TARGET_PLATFORM_ARCH)-linux --prefix=$(TARGETDIR) \
						--with-ssl=$(LIB_OPENSSLDIR) \
						--with-libssl-prefix=$(LIB_OPENSSLDIR)  
#	    (cp configure.ac $(APP)/); \
	    (cp configure $(APP)/); \
	    (cp Makefile.in  $(APP)/); \


libssh2:	
	cd $(APP); make

	

clean:
	@if [ -e $(SUBDIRS)/Makefile ]; then \
    cd $(SUBDIRS); $(MAKE) distclean ; \
    fi
	-rm -rf $(SUBDIRS)

curl_conf:
	rm -rf curl-7.57.0
	rm -rf curl-7.36.0
	rm -rf curl-7.23.1
	cp -rf $(CURL_DIR) $(CURL_DIR)_static
	ln -sf $(CURL_DIR) curl
	cd $(CURL_DIR)_static; \
	./configure --host=$(TARGET_PLATFORM_ARCH)-linux --prefix=$(TARGETDIR) \
						--disable-shared --with-ssl=$(LIB_OPENSSLDIR) --disable-manual --disable-proxy --without-zlib \
		 				--disable-ipv6 --disable-dict --disable-file --disable-gopher \
						--disable-imap --disable-pop3 --disable-smtp --disable-telnet --disable-tftp --disable-rtsp CFLAGS="$(CFLAGS) -fPIC" LDFLAGS=-Wl,-rpath-link,$(LIB_OPENSSLDIR)/lib
curl:	
	touch $(CURL_DIR)_static/Makefile
	(cd $(CURL_DIR)_static; make) || exit 1;

curl_preinstall:
	cp $(CURL_DIR_PATH)/$(CURL_DIR)_static/src/curl $(CURL_DIR_PATH)/binary
	
	 
libcurl_conf:
	cd $(CURL_DIR); \
	./configure --host=$(TARGET_PLATFORM_ARCH)-linux CC=$(TOOLCHAIN_PATH_USER)/$(TARGET_PLATFORM_CROSS_COMPILE_USER)gcc --prefix=$(TARGETDIR) \
						--enable-shared --with-ssl=$(LIB_OPENSSLDIR) --disable-manual --disable-proxy --without-zlib \
						--disable-ipv6 --disable-dict --disable-file --disable-ftp --disable-gopher \
						--disable-imap --disable-pop3 --disable-smtp --disable-telnet --disable-tftp --disable-rtsp CFLAGS="$(CFLAGS) -fPIC" LDFLAGS=-Wl,-rpath-link,$(LIB_OPENSSLDIR)/lib
libcurl:
	(cd $(CURL_DIR); make) || exit 1;
	cp $(CURL_DIR_PATH)/$(CURL_DIR)/lib/.libs/libcurl.* $(CURL_DIR_PATH)/binary	

sub_clean:
	for i in ${SUBDIRS}; do \
		(cd $$i; make clean); \
	done

sub_clean_static:
	for i in ${SUBDIRS_static}; do \
		(cd $$i; make clean); \
	done
	
dist_clean:
	for i in ${SUBDIRS}; do \
		(cd $$i; make distclean) || exit 1; \
	done
	
clean: curl_conf sub_clean libcurl_conf sub_clean
	rm -rf $(CURL_DIR_PATH)/binary/*
	rm -rf $(TARGETDIR)/sbin/curl
	rm -rf $(TARGETDIR)/usr/lib/libcurl.*
install:
#	$(TARGET_PLATFORM_CROSS_COMPILE_USER)strip $(CURL_DIR_PATH)/binary/curl
#	rm -rf $(TARGETDIR)/bin/curl
#	rm -f $(TARGETDIR)/lib/public/libcurl.so*
#	install -d $(TARGETDIR)/sbin/
#	install -m 755 $(CURL_DIR_PATH)/binary/curl $(TARGETDIR)/sbin
#	install -m 755 $(CURL_DIR_PATH)/binary/libcurl.*  $(TARGETDIR)/usr/lib

