include ../../../Rules.mk


DEPSDIR  = deps.$(PLATFORM)
INSTDIR  = $(CURDIR)/$(DEPSDIR)/usr

DEPENDS  += $(DEPSDIR)

CPPFLAGS += -I$(INSTDIR)/include -Wno-missing-braces
LDFLAGS  += -L$(INSTDIR)/lib -pthread
LDLIBS   += -lbluetooth -lncurses

TYRES_EXE = tyres
TYRES_SRC = tyres.c

$(eval $(call build-program, $(TYRES_EXE), $(TYRES_SRC)))

$(DEPSDIR):
	@mkdir $(DEPSDIR)
	@echo "Downloading dependencies sources"
	@wget -P $@ https://github.com/libexpat/libexpat/releases/download/R_2_2_10/expat-2.2.10.tar.bz2
	@wget -P $@ https://dbus.freedesktop.org/releases/dbus/dbus-1.13.18.tar.xz
	@wget -P $@ ftp://ftp.cwru.edu/pub/bash/readline-8.1.tar.gz
	@wget -P $@ ftp://ftp.invisible-island.net/ncurses/ncurses-6.2.tar.gz
	@wget -P $@ ftp://sourceware.org/pub/libffi/libffi-3.3.tar.gz
	@wget -P $@ https://zlib.net/zlib-1.2.11.tar.xz
	@wget -P $@ https://download.gnome.org/sources/glib/2.56/glib-2.56.4.tar.xz
	@wget -P $@ http://www.kernel.org/pub/linux/bluetooth/bluez-5.55.tar.xz
	@echo "Extractings dependencies"
	@tar -x -C $@ -f $@/expat-2.2.10.tar.bz2; mv $@/expat-2.2.10 $@/expat
	@tar -x -C $@ -f $@/dbus-1.13.18.tar.xz; mv $@/dbus-1.13.18 $@/dbus
	@tar -x -C $@ -f $@/readline-8.1.tar.gz; mv $@/readline-8.1 $@/readline
	@tar -x -C $@ -f $@/ncurses-6.2.tar.gz; mv $@/ncurses-6.2 $@/ncurses
	@tar -x -C $@ -f $@/libffi-3.3.tar.gz; mv $@/libffi-3.3 $@/libffi
	@tar -x -C $@ -f $@/zlib-1.2.11.tar.xz; mv $@/zlib-1.2.11 $@/zlib
	@tar -x -C $@ -f $@/glib-2.56.4.tar.xz; mv $@/glib-2.56.4 $@/glib
	@tar -x -C $@ -f $@/bluez-5.55.tar.xz; mv $@/bluez-5.55 $@/bluez
	@echo "Building dependencies"
	@cd $@/expat; ./configure --host=$(HOST) --prefix=$(INSTDIR) --disable-shared CC="$(CC)" CFLAG="$(CFLAGS)"; make; make install
	@cd $@/dbus; ./configure --host=$(HOST) --prefix=$(INSTDIR) --disable-shared --enable-static --disable-systemd --disable-selinux --disable-tests CC="$(CC)" CPPFLAGS="-I$(INSTDIR)/include" CFLAG="$(CFLAGS)" LDFLAGS="-L$(INSTDIR)/lib"; make; make install
	@cd $@/readline; ./configure --host=$(HOST) --prefix=$(INSTDIR) --disable-shared --enable-static --disable-install-examples CC="$(CC)" CFLAG="$(CFLAGS)"; make; make install
	@cd $@/ncurses; ./configure --host=$(HOST) --prefix=$(INSTDIR) --with-shared=no --without-progs --without-tests --without-manpages --disable-database --with-fallbacks=xterm-256color CC="$(CC)" CFLAG="$(CFLAGS)"; make; make install
	@cd $@/libffi; ./configure --host=$(HOST) --prefix=$(INSTDIR) --disable-shared --disable-builddir CC="$(CC)" CFLAG="$(CFLAGS)"; make; make install
	@cd $@/zlib; ./configure --prefix=$(INSTDIR) --static; make CC="$(CC)" CFLAG="$(CFLAGS)"; make install
	@cd $@/glib; ./configure --host=$(HOST) --prefix=$(INSTDIR) --disable-shared --enable-static --with-pcre=internal --disable-fam --disable-libelf --disable-libmount --disable-selinux --disable-man --disable-debug glib_cv_stack_grows=no glib_cv_uscore=no CC="$(CC)" CFLAG="$(CFLAGS)" LIBFFI_CFLAGS="-I$(INSTDIR)/include" LIBFFI_LIBS="-L$(INSTDIR)/lib -lffi" ZLIB_CFLAGS="-I$(INSTDIR)/include" ZLIB_LIBS="-L$(INSTDIR)/lib -lz"; make; make install
	@cd $@/bluez; ./configure --host=$(HOST) --prefix=$(INSTDIR) --enable-library --disable-shared --enable-static --disable-tools --disable-datafiles --disable-client --disable-obex --disable-monitor --disable-mesh --disable-cups --disable-systemd CC="$(CC)" CPPFLAGS="-I$(INSTDIR)/include" CFLAG="$(CFLAGS)" LIBS="-L$(INSTDIR)/lib -lncurses" GLIB_CFLAGS="-I$(INSTDIR)/include/glib-2.0 -I$(INSTDIR)/lib/glib-2.0/include" GLIB_LIBS="-L$(INSTDIR)/lib -lglib-2.0 -lgobject-2.0 -lgio-2.0 -lgmodule-2.0"; make; make install

install:
	@install -d $(DESTDIR)/bin
	@install -m 755 $(OBJDIR)/$(TYRES_EXE) $(DESTDIR)/bin/

clean:
	rm -rf $(OBJDIR) $(DEPSDIR)
