-include ../arch.mk
-include ../../path.mk
-include ../../.config

ifeq ($(ELBOX_PROGS_PRIV_WPA_SUPPLICANT_WPS),y)
CONFIG_EAP_WPS:=y
CONFIG_WPS_IE:=y
endif

#ifeq ($(ELBOX_PROGS_GPL_SYSLOGD_AP),y)
#CFLAGS += -DSYSLOGD_AP
#endif

#ifeq ($(ELBOX_PROGS_PRIV_WPA_SUPPLICANT_STATUS),y)
#CFLAGS += -DWPA_SUPPLICANT_STATUS
#endif

APPLET		:= wpa_supplicant
LOCAL_SRCS	:= config.c eloop.c common.c md5.c rc4.c sha1.c
CMLIB_SRCS	:= dtrace.c libxmldbc.c unixsock.c
CPLUS_SRCS	:=

ifndef CFLAGS
CFLAGS = -MMD -O2 -Wall -g
endif

# Include directories for CVS version
#CFLAGS += -I. -I../utils -I../hostapd
CFLAGS += -I $(TOPDIR)/include
CFLAGS += -I$(OPENSSLDIR)/include
ifeq ($(strip $(ELBOX_PROGS_PRIV_WPA_SUPPLICANT_USE_STATIC_LINK_OPENSSL)),y)
LIBS += -L$(OPENSSLDIR) -static
else
LIBS += -L$(OPENSSLDIR)
endif

ifeq ($(strip $(ELBOX_PROGS_PRIV_MADWIFI_AP83)),y)
CONFIG_DRIVER_MADWIFI:=y
CFLAGS += -D_BYTE_ORDER=_BIG_ENDIAN
CFLAGS += -I$(TOPDIR)/progs.priv/madwifi_ap83/_build/wlan
CFLAGS += -I$(TOPDIR)/progs.priv/madwifi_ap83/_build/wlan/linux
endif

ifeq ($(strip $(ELBOX_PROGS_PRIV_MADWIFI_CAVIUM)),y)
CONFIG_DRIVER_MADWIFI:=y
CFLAGS += -D_BYTE_ORDER=_BIG_ENDIAN
CFLAGS += -I$(TOPDIR)/progs.priv/madwifi_cavium/_build/wlan/madwifi/madwifi
endif

ifeq ($(strip $(ELBOX_PROGS_PRIV_MADWIFI_FUSION_CAVIUM)),y)
CONFIG_DRIVER_MADWIFI:=y
CFLAGS += -D_BYTE_ORDER=_BIG_ENDIAN
CFLAGS += -I$(TOPDIR)/progs.priv/madwifi_fusion_cavium/_build/wlan/linux
CFLAGS += -I$(TOPDIR)/progs.priv/madwifi_fusion_cavium/_build/wlan
endif

ifeq ($(strip $(ELBOX_PROGS_PRIV_RT2860V2_STA)),y)
CONFIG_DRIVER_RALINK:=y
endif

ifeq ($(strip $(ELBOX_PROGS_PRIV_WPA_SUPPLICANT_8021X_EAPOL)),y)
#CONFIG_DRIVER_WIRED:=y
#CONFIG_IEEE8021X_EAPOL:=y
CONFIG_EAP_MD5:=y
CONFIG_EAP_MSCHAPV2:=y
CONFIG_EAP_TLS:=y
CONFIG_EAP_PEAP:=y
CONFIG_EAP_TTLS:=y
#CONFIG_EAP_LEAP:=y
endif

ifndef CONFIG_BACKEND
CONFIG_BACKEND:=file
endif

ifeq ($(CONFIG_BACKEND), file)
LOCAL_SRCS += config_file.c base64.c
endif

ifdef CONFIG_DRIVER_HOSTAP
CFLAGS += -DCONFIG_DRIVER_HOSTAP
LOCAL_SRCS += driver_hostap.c
CONFIG_WIRELESS_EXTENSION:=y
endif

ifdef CONFIG_DRIVER_WEXT
CFLAGS += -DCONFIG_DRIVER_WEXT
CONFIG_WIRELESS_EXTENSION:=y
endif

ifdef CONFIG_DRIVER_MADWIFI
CFLAGS += -DCONFIG_DRIVER_MADWIFI
LOCAL_SRCS += driver_madwifi.c
CONFIG_WIRELESS_EXTENSION:=y
endif

ifdef CONFIG_DRIVER_BROADCOM
CFLAGS += -DCONFIG_DRIVER_BROADCOM
LOCAL_SRCS += driver_broadcom.c
endif

ifdef CONFIG_DRIVER_RALINK
CFLAGS += -DCONFIG_DRIVER_RALINK
LOCAL_SRCS += driver_ralink.c
endif

ifdef CONFIG_DRIVER_WIRED
CFLAGS += -DCONFIG_DRIVER_WIRED
LOCAL_SRCS += driver_wired.c
endif

ifdef CONFIG_DNET_PCAP
CFLAGS += -DUSE_DNET_PCAP
ifdef CONFIG_WINPCAP
LOCAL_SRCS += l2_packet_pcap.c
CFLAGS += -DCONFIG_WINPCAP
LIBS += -lwpcap -lpacket
else
ifdef CONFIG_L2_FREEBSD
LOCAL_SRCS += l2_packet_freebsd.c
LIBS += -lpcap
else
LOCAL_SRCS += l2_packet_pcap.c
LIBS += -ldnet -lpcap
endif
endif
else
LOCAL_SRCS += l2_packet_linux.c
endif

ifdef CONFIG_EAP_TLS
# EAP-TLS
CFLAGS += -DEAP_TLS
LOCAL_SRCS += eap_tls.c
TLS_FUNCS:=y
CONFIG_IEEE8021X_EAPOL:=y
endif

ifdef CONFIG_EAP_PEAP
# EAP-PEAP
CFLAGS += -DEAP_PEAP
LOCAL_SRCS += eap_peap.c
TLS_FUNCS:=y
CONFIG_EAP_MSCHAPV2:=y
CONFIG_IEEE8021X_EAPOL:=y
CONFIG_EAP_TLV:=y
endif

ifdef CONFIG_EAP_TTLS
# EAP-TTLS
CFLAGS += -DEAP_TTLS
LOCAL_SRCS += eap_ttls.c
MS_FUNCS:=y
TLS_FUNCS:=y
CONFIG_EAP_MD5:=y
CONFIG_IEEE8021X_EAPOL:=y
endif

ifdef CONFIG_EAP_MD5
# EAP-MD5 (also used by EAP-TTLS)
CFLAGS += -DEAP_MD5
LOCAL_SRCS += eap_md5.c
CONFIG_IEEE8021X_EAPOL:=y
endif

# backwards compatibility for old spelling
ifdef CONFIG_MSCHAPV2
CONFIG_EAP_MSCHAPV2:=y
endif

ifdef CONFIG_EAP_MSCHAPV2
# EAP-MSCHAPv2 (also used by EAP-PEAP)
CFLAGS += -DEAP_MSCHAPv2
LOCAL_SRCS += eap_mschapv2.c
MS_FUNCS=y
CONFIG_IEEE8021X_EAPOL:=y
endif

ifdef CONFIG_EAP_GTC
# EAP-GTC (also used by EAP-PEAP)
CFLAGS += -DEAP_GTC
LOCAL_SRCS += eap_gtc.c
CONFIG_IEEE8021X_EAPOL:=y
endif

ifdef CONFIG_EAP_OTP
# EAP-OTP
CFLAGS += -DEAP_OTP
LOCAL_SRCS += eap_otp.c
CONFIG_IEEE8021X_EAPOL:=y
endif

ifdef CONFIG_EAP_SIM
# EAP-SIM
CFLAGS += -DEAP_SIM
LOCAL_SRCS += eap_sim.c
CONFIG_IEEE8021X_EAPOL:=y
CONFIG_EAP_SIM_COMMON:=y
endif

ifdef CONFIG_EAP_LEAP
# EAP-LEAP
CFLAGS += -DEAP_LEAP
LOCAL_SRCS += eap_leap.c
MS_FUNCS:=y
CONFIG_IEEE8021X_EAPOL:=y
endif

ifdef CONFIG_EAP_PSK
# EAP-PSK
CFLAGS += -DEAP_PSK
LOCAL_SRCS += eap_psk.c eap_psk_common.c
CONFIG_IEEE8021X_EAPOL:=y
NEED_AES:=y
endif

ifdef CONFIG_EAP_AKA
# EAP-AKA
CFLAGS += -DEAP_AKA
LOCAL_SRCS += eap_aka.c
CONFIG_IEEE8021X_EAPOL:=y
CONFIG_EAP_SIM_COMMON:=y
endif

ifdef CONFIG_EAP_SIM_COMMON
LOCAL_SRCS += eap_sim_common.c
NEED_AES:=y
endif

ifdef CONFIG_EAP_TLV
# EAP-TLV
CFLAGS += -DEAP_TLV
LOCAL_SRCS += eap_tlv.c
endif

ifdef CONFIG_EAP_FAST
# EAP-FAST
CFLAGS += -DEAP_FAST
LOCAL_SRCS += eap_fast.c
TLS_FUNCS:=y
endif

ifdef CONFIG_EAP_PAX
# EAP-PAX
CFLAGS += -DEAP_PAX
LOCAL_SRCS += eap_pax.c eap_pax_common.c
CONFIG_IEEE8021X_EAPOL:=y
endif

ifdef CONFIG_EAP_WPS
# EAP-WPS Wifi Protected Setup
CFLAGS += -DEAP_WPS
LOCAL_SRCS += eap_wps.c
LIBS_p += -lssl -lcrypto
LIBS += -lssl -lcrypto
CONFIG_IEEE8021X_EAPOL:=y
endif

ifdef CONFIG_WPS_IE
# Support for new WPS IEs
CFLAGS += -DWPS_NEW_IE
LOCAL_SRCS += wps_ie.c
endif

ifdef CONFIG_IEEE8021X_EAPOL
# IEEE 802.1X/EAPOL state machines (e.g., for RADIUS authentication)
CFLAGS += -DIEEE8021X_EAPOL
LOCAL_SRCS += eapol_sm.c eap.c
endif

ifdef CONFIG_PCSC
# PC/SC interface for smartcards (USIM, GSM SIM)
CFLAGS += -DPCSC_FUNCS -I/usr/include/PCSC
LOCAL_SRCS += pcsc_funcs.c
# -lpthread may not be needed depending on how pcsc-lite was configured
LIBS += -lpcsclite -lpthread
endif

ifndef CONFIG_TLS
CONFIG_TLS:=openssl
endif

ifdef TLS_FUNCS
# Shared TLS functions (needed for EAP_TLS, EAP_PEAP, EAP_TTLS, and EAP_FAST)
CFLAGS += -DEAP_TLS_FUNCS
LOCAL_SRCS += eap_tls_common.c
ifeq ($(CONFIG_TLS), openssl)
LOCAL_SRCS += tls_openssl.c
LIBS += -lssl -lcrypto
endif
ifeq ($(CONFIG_TLS), gnutls)
LOCAL_SRCS += tls_gnutls.c
LIBS += -lgnutls -lgcrypt -lgpg-error
endif
ifeq ($(CONFIG_TLS), schannel)
LOCAL_SRCS += tls_schannel.c
# Using OpenSSL for crypto at the moment; to be replaced
LIBS += -lcrypto
endif
ifdef CONFIG_SMARTCARD
ifndef CONFIG_NATIVE_WINDOWS
ifndef CONFIG_L2_FREEBSD
LIBS += -ldl
endif
endif
endif
NEED_CRYPTO:=y
else
LOCAL_SRCS += tls_none.c
endif

ifdef CONFIG_PKCS12
CFLAGS += -DPKCS12_FUNCS
endif

ifdef CONFIG_SMARTCARD
CFLAGS += -DCONFIG_SMARTCARD
endif

ifdef MS_FUNCS
LOCAL_SRCS += ms_funcs.c
NEED_CRYPTO:=y
endif

ifdef NEED_CRYPTO
ifndef TLS_FUNCS
ifeq ($(CONFIG_TLS), openssl)
LIBS += -lcrypto
endif
ifeq ($(CONFIG_TLS), gnutls)
LIBS += -lgcrypt
endif
ifeq ($(CONFIG_TLS), schannel)
# Using OpenSSL for crypto at the moment; to be replaced
LIBS += -lcrypto
endif
endif
ifeq ($(CONFIG_TLS), openssl)
LOCAL_SRCS += crypto.c
endif
ifeq ($(CONFIG_TLS), gnutls)
LOCAL_SRCS += crypto_gnutls.c
endif
ifeq ($(CONFIG_TLS), schannel)
# Using OpenSSL for crypto at the moment; to be replaced
LOCAL_SRCS += crypto.c
endif
endif

ifdef CONFIG_WIRELESS_EXTENSION
CFLAGS += -DCONFIG_WIRELESS_EXTENSION
LOCAL_SRCS += driver_wext.c
endif

ifdef CONFIG_READLINE
CFLAGS += -DCONFIG_READLINE
endif

ifdef CONFIG_NO_STDOUT_DEBUG
CFLAGS += -DCONFIG_NO_STDOUT_DEBUG
endif

ifdef CONFIG_IPV6
# for eapol_test only
CFLAGS += -DCONFIG_IPV6
endif

ifndef CONFIG_NO_WPA
LOCAL_SRCS += wpa.c preauth.c
NEED_AES:=y
else
CFLAGS += -DCONFIG_NO_WPA
endif

ifdef NEED_AES
LOCAL_SRCS += aes_wrap.c
endif

LOCAL_SRCS += wpa_supplicant.c events.c
LOCAL_SRCS += main.c drivers.c

LDFLAGS += $(CFLAGS) $(LIBS)

-include $(TOPDIR)/Rules.mk

clean:
	rm -f core *~ *.o *.d $(APPLET) .depend

install: all
	$(Q)$(STRIP) wpa_supplicant
	$(Q)cp wpa_supplicant $(TARGET)/usr/sbin/.
	#mkdir -p $(DESTDIR)/usr/local/sbin/
	#for i in $(ALL); do cp $$i $(DESTDIR)/usr/local/sbin/$$i; done

.PHONY: clean install

