#
#
# Copyright 2016 Broadcom.
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
#
#

#
# We need to set and export OPEN_ROOT to point to the
# root directory of the OpEN ADK installation, and then
# bring in the common.mak settings.
#
export OPEN_ROOT = ../..

export RUBY_H = ./ruby1.8.7
export SWIGVER=1.3.35

ifeq (1,$(BLD_L7_TARGETOS_LINUX_UB14))
export RUBY_H = ./ruby1.9.1
endif

ifeq (1,$(BLD_L7_TARGETOS_LINUX_UB16))
export RUBY_H = ./ruby2.2.0
endif

ifeq (1,$(BLD_L7_TARGETOS_LINUX_UB18))
export RUBY_H = ./ruby-amd64-2.5.0
endif

ifeq (1,$(BLD_L7_TARGETOS_LINUX_UB20))
export RUBY_H = ./ruby-amd64-2.7.0
endif

ifeq (1,$(BLD_L7_TARGETOS_LINUX_COS7))
export RUBY_H = ./ruby2.0.0
export SWIGVER=3.0.0
endif

ifeq (1,$(BLD_L7_FLEX_EMDEBIAN))
ifeq (buster,$(EMDEBIAN_VERSION))
RUBY_VER=2.5.0
else
RUBY_VER=2.3.0
endif
ifeq (armel,$(DISTRO_ARCH))
export RUBY_H = ./ruby-armel-$(RUBY_VER)
endif
ifeq (amd64,$(DISTRO_ARCH))
export RUBY_H = ./ruby-amd64-$(RUBY_VER)
endif
ifeq (arm64,$(DISTRO_ARCH))
export RUBY_H = ./ruby-arm64-$(RUBY_VER)
endif
endif

include $(OPEN_ROOT)/swig/make/make.swig

#
# Simple rule for compiling our applications, no
# incremental steps.
#
all: OpEN_base.so
	$(NOOP)

openswig: OpEN_base.so
	$(NOOP)

OpEN_base.so: openwrap.i createstub.pl
	$(DBG) swig -DL7_NO_PRINTF_FMT=1 -w801 -ruby -o openwrapbase.c openwrap.i
	$(DBG) swig -DL7_NO_PRINTF_FMT=1 -w801 -ruby -o openwrapbase_qos.c openwrap_qos.i
	$(DBG) swig -DL7_NO_PRINTF_FMT=1 -w801 -ruby -o openwrapbase_switching.c openwrap_switching.i
	$(DBG) swig -DL7_NO_PRINTF_FMT=1 -w801 -ruby -o openwrapbase_routing.c openwrap_routing.i
	$(DBG) swig -DL7_NO_PRINTF_FMT=1 -w801 -ruby -o openwrapbase_security.c openwrap_security.i
	$(DBG) python getwrappers_base.py
	$(DBG) python getwrappers_switching.py
	$(DBG) python getwrappers_qos.py
	$(DBG) python getwrappers_routing.py
	$(DBG) python getwrappers_security.py
	$(DBG) perl createstub.pl
	$(DBG) echo "void AppendWrappedFuncs();" > tmp.txt
	$(DBG) cat tmp.txt openwrapbase.c result_base.txt > openwraptmp.c
	$(DBG) cat tmp.txt openwrapbase_qos.c result_qos.txt > openwraptmp_qos.c
	$(DBG) cat tmp.txt openwrapbase_switching.c result_switching.txt > openwraptmp_switching.c
	$(DBG) cat tmp.txt openwrapbase_routing.c result_routing.txt > openwraptmp_routing.c
	$(DBG) cat tmp.txt openwrapbase_security.c result_security.txt > openwraptmp_security.c	
	$(DBG) sed "s/SWIG_RubyInitializeTrackings();/SWIG_RubyInitializeTrackings(); AppendWrappedFuncs();/" < openwraptmp.c > openwrap.c
	$(DBG) sed "s/SWIG_RubyInitializeTrackings();/SWIG_RubyInitializeTrackings(); AppendWrappedFuncs();/" < openwraptmp_qos.c > openwrap_qos.c
	$(DBG) sed "s/SWIG_RubyInitializeTrackings();/SWIG_RubyInitializeTrackings(); AppendWrappedFuncs();/" < openwraptmp_switching.c > openwrap_switching.c
	$(DBG) sed "s/SWIG_RubyInitializeTrackings();/SWIG_RubyInitializeTrackings(); AppendWrappedFuncs();/" < openwraptmp_routing.c > openwrap_routing.c
	$(DBG) sed "s/SWIG_RubyInitializeTrackings();/SWIG_RubyInitializeTrackings(); AppendWrappedFuncs();/" < openwraptmp_security.c > openwrap_security.c
	$(DBG) $(CROSS_COMPILE)$(CC) -I$(RUBY_H) -fPIC $(CFLAGS) $(CFLAGSEXTRA) -c openwrap.c
	$(DBG) $(CROSS_COMPILE)$(CC) -I$(RUBY_H) -fPIC $(CFLAGS) $(CFLAGSEXTRA) -c openwrap_qos.c
	$(DBG) $(CROSS_COMPILE)$(CC) -I$(RUBY_H) -fPIC $(CFLAGS) $(CFLAGSEXTRA) -c openwrap_switching.c
	$(DBG) $(CROSS_COMPILE)$(CC) -I$(RUBY_H) -fPIC $(CFLAGS) $(CFLAGSEXTRA) -c openwrap_routing.c
	$(DBG) $(CROSS_COMPILE)$(CC) -I$(RUBY_H) -fPIC $(CFLAGS) $(CFLAGSEXTRA) -c openwrap_security.c
	$(DBG) $(CROSS_COMPILE)$(CC) -shared openwrap.o -o $@ $(LDFLAGS) $(LFLAGSEXTRA)
	$(DBG) $(CROSS_COMPILE)$(CC) -shared openwrap_qos.o -o OpEN_qos.so $(LDFLAGS) $(LFLAGSEXTRA)
	$(DBG) $(CROSS_COMPILE)$(CC) -shared openwrap_switching.o -o OpEN_switching.so $(LDFLAGS) $(LFLAGSEXTRA)
	$(DBG) $(CROSS_COMPILE)$(CC) -shared openwrap_routing.o -o OpEN_routing.so $(LDFLAGS) $(LFLAGSEXTRA)
	$(DBG) $(CROSS_COMPILE)$(CC) -shared openwrap_security.o -o OpEN_security.so $(LDFLAGS) $(LFLAGSEXTRA)

openwrap.i: genifile_base.pl genifile_qos.pl genifile_switching.pl genifile_routing.pl
	$(DBG) python rpccom_openapi_enums.py
	$(DBG) perl genifile_base.pl
	$(DBG) perl genifile_qos.pl
	$(DBG) perl genifile_switching.pl
	$(DBG) perl genifile_routing.pl
	$(DBG) perl genifile_security.pl

clean:
	$(DBG) rm -f *.o OpE*.so openwra*.c openwrapbas*.c openwraptm*.c resul*.txt appen*.txt tmp.txt openwra*.i 
