# This file is part of telegram-purple
#
# This program 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 2 of the License, or
# (at your option) any later version.
#
# This program 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
#
# Copyright Matthias Jentsch, Vitaly Valtman, Christopher Althaus, Markus Endres 2014-2015

srcdir=.
prefix=/usr
datarootdir=${prefix}/share
localedir=${datarootdir}/locale
gettext_package=telegram-purple

CFLAGS=-Wall -g -O2 -I/usr/include/libpurple -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include   -DHAVE_CONFIG_H -DENABLE_NLS -DLOCALEDIR='"$(localedir)"' -Wall -Wextra -Wno-deprecated-declarations -Wno-unused-parameter -I${srcdir} -I. -fno-strict-aliasing -fPIC
LDFLAGS= -L/usr/lib -L/usr/lib -lssl -lcrypto   -lpurple -lglib-2.0   -lwebp -lz -lrt  -rdynamic -ggdb
DIR_PERM=0755
FILE_PERM=0644

CC=arm-linux-gnueabi-gcc
PKG_CONFIG=/scratchbox/devkits/autotools-legacy/bin/pkg-config
MSGFMT_PATH=/scratchbox/devkits/autotools-legacy/bin/msgfmt

DEP=dep
EXE=bin
OBJ=objs
LIB=libs
DIR_LIST=${DEP} ${EXE} ${OBJ} ${LIB}

PLUGIN_OBJECTS=${OBJ}/tgp-net.o ${OBJ}/tgp-timers.o ${OBJ}/msglog.o ${OBJ}/telegram-base.o ${OBJ}/telegram-purple.o ${OBJ}/tgp-2prpl.o ${OBJ}/tgp-structs.o ${OBJ}/tgp-utils.o ${OBJ}/tgp-chat.o ${OBJ}/tgp-ft.o ${OBJ}/tgp-msg.o ${OBJ}/tgp-request.o ${OBJ}/tgp-blist.o
ALL_OBJS=${PLUGIN_OBJECTS}

ifdef MSGFMT_PATH
LOCALES=$(shell cat po/LINGUAS)
LOCALE_MOS=$(addprefix po/,$(addsuffix .mo,$(LOCALES)))
endif

.SUFFIXES:

.SUFFIXES: .c .h .o

PRPL_NAME=telegram-purple.so
PRPL_LIBNAME=${EXE}/${PRPL_NAME}
all: ${PRPL_LIBNAME} $(LOCALE_MOS)

PLUGIN_DIR_PURPLE=$(shell ${PKG_CONFIG} --variable=plugindir purple)
DATA_ROOT_DIR_PURPLE=$(shell ${PKG_CONFIG} --variable=datarootdir purple)
COPY_ICONS=yes
CRYPTO_FLAG=

create_dirs: ${DIR_LIST}

${DIR_LIST}:
	@test -d $@ || mkdir -p $@

DEPENDENCE=$(subst ${OBJ}/,${DEP}/,$(patsubst %.o,%.d,${ALL_OBJS}))
DEPENDENCE_LIST=${DEPENDENCE}
-include ${DEPENDENCE_LIST}


# The dependency on tgl/libs/libtgl.a ensures that "make -C tgl" was called,
# and most importantly: It ensures that all automatic headers exist.
${PLUGIN_OBJECTS}: ${OBJ}/%.o: ${srcdir}/%.c ${LIB}/libtgl.a | create_dirs
	echo $@ && ${CC} ${CFLAGS} ${CPPFLAGS} -I ${srcdir}/tgl -c -MP -MD -MF ${DEP}/$*.d -MQ ${OBJ}/$*.o -o $@ $<

tgl/Makefile.in:
	@echo "tgl/Makefile.in not found. Maybe you need to 'git submodule update --init --recursive' ?" && exit 1

tgl/Makefile: tgl/Makefile.in
	cd tgl && ./configure -q ${CRYPTO_FLAG} --disable-extf CFLAGS="-Wall -g -O2" LDFLAGS=" -L/usr/lib -L/usr/lib"

tgl/libs/libtgl.a: tgl/Makefile
	+make -C tgl

po/%.mo: po/%.po
	${MSGFMT_PATH} -cf -o $@ $<

${LIB}/libtgl.a: tgl/libs/libtgl.a | create_dirs
	cp $< $@

${PRPL_LIBNAME}: ${PLUGIN_OBJECTS} ${LIB}/libtgl.a | create_dirs
	${CC} -shared -o $@ $^ ${LDFLAGS}

.PHONY: plugin
plugin: $(PRPL_LIBNAME)


.PHONY: strip
strip: $(PRPL_LIBNAME)
	$(STRIP) --strip-unneeded $(PRPL_LIBNAME)

.PHONY: install
install: $(PRPL_LIBNAME) $(LOCALE_MOS)
	mkdir -m $(DIR_PERM) -p $(DESTDIR)$(PLUGIN_DIR_PURPLE)
	install -m $(FILE_PERM) $(PRPL_LIBNAME) $(DESTDIR)$(PLUGIN_DIR_PURPLE)/$(PRPL_NAME)
	mkdir -m $(DIR_PERM) -p $(DESTDIR)/etc/telegram-purple
	install -m $(FILE_PERM) tg-server.tglpub $(DESTDIR)/etc/telegram-purple/server.tglpub
	mkdir -m $(DIR_PERM) -p $(DESTDIR)$(datarootdir)/appdata
	install -m $(FILE_PERM) telegram-purple.metainfo.xml $(DESTDIR)$(datarootdir)/appdata/telegram-purple.metainfo.xml
ifeq ($(COPY_ICONS),no)
	@echo "Not copying icons due to configure-option --disable-icons."
	@echo " If you're using Finch, Empathy, or Telepathy, then this is a good idea."
	@echo " If you're using Pidgin, then this is a bad idea."
	@echo " If you're using Adium, read the README.md: You're using the wrong build system!"
else
	mkdir -m $(DIR_PERM) -p $(DESTDIR)$(DATA_ROOT_DIR_PURPLE)/pixmaps/pidgin/protocols/16
	install -m $(FILE_PERM) imgs/telegram16.png $(DESTDIR)$(DATA_ROOT_DIR_PURPLE)/pixmaps/pidgin/protocols/16/telegram.png
	mkdir -m $(DIR_PERM) -p $(DESTDIR)$(DATA_ROOT_DIR_PURPLE)/pixmaps/pidgin/protocols/22
	install -m $(FILE_PERM) imgs/telegram22.png $(DESTDIR)$(DATA_ROOT_DIR_PURPLE)/pixmaps/pidgin/protocols/22/telegram.png
	mkdir -m $(DIR_PERM) -p $(DESTDIR)$(DATA_ROOT_DIR_PURPLE)/pixmaps/pidgin/protocols/48
	install -m $(FILE_PERM) imgs/telegram48.png $(DESTDIR)$(DATA_ROOT_DIR_PURPLE)/pixmaps/pidgin/protocols/48/telegram.png
endif
	@for lang in $(LOCALES); do \
		mkdir -p $(DESTDIR)$(localedir)/$$lang/LC_MESSAGES; \
		echo "installing po/$$lang.mo to $(DESTDIR)$(localedir)/$$lang/LC_MESSAGES/$(gettext_package).mo"; \
		install -m $(FILE_PERM) po/$$lang.mo $(DESTDIR)$(localedir)/$$lang/LC_MESSAGES/$(gettext_package).mo; \
	done

.PHONY: uninstall
uninstall:
	rm -f $(DESTDIR)$(PLUGIN_DIR_PURPLE)/$(PRPL_NAME)
	rm -f $(DESTDIR)/etc/telegram-purple/server.pub # TODO: Remove this in later versions
	rm -f $(DESTDIR)/etc/telegram-purple/server.tglpub
	rm -f $(DESTDIR)$(DATA_ROOT_DIR_PURPLE)/pixmaps/pidgin/protocols/16/telegram.png
	rm -f $(DESTDIR)$(DATA_ROOT_DIR_PURPLE)/pixmaps/pidgin/protocols/22/telegram.png
	rm -f $(DESTDIR)$(DATA_ROOT_DIR_PURPLE)/pixmaps/pidgin/protocols/48/telegram.png
	rm -f $(DESTDIR)$(datarootdir)/appdata/telegram-purple.metainfo.xml
# TODO: Delete all installed .mo's, no matter what LINGUAS says.
	@for lang in $(LOCALES); do \
		echo "removing $(localedir)/$$lang/LC_MESSAGES/$(gettext_package).mo"; \
		rm -f $(localedir)/$$lang/LC_MESSAGES/$(gettext_package).mo; \
	done

.PHONY: local_install
local_install:
	mkdir -m $(DIR_PERM) -p $(DESTDIR)$(HOME)/.purple/plugins
	install -m $(FILE_PERM) $(PRPL_LIBNAME) $(DESTDIR)$(HOME)/.purple/plugins/$(PRPL_NAME)
# See telegram-base.c, function get_user_pk_path for justification:
	mkdir -m $(DIR_PERM) -p $(DESTDIR)$(HOME)/.purple/telegram-purple
	install -m $(FILE_PERM) tg-server.tglpub $(DESTDIR)$(HOME)/.purple/telegram-purple/server.tglpub

.PHONY: local_uninstall
local_uninstall:
	rm -f $(DESTDIR)$(HOME)/.purple/plugins/$(PRPL_NAME)
	rm -f $(DESTDIR)$(HOME)/.purple/telegram-purple/server.tglpub

.PHONY: run
run:
	pidgin -d | grep 'telegram\|plugin\|proxy'

.PHONY: debug
debug: install
	ddd pidgin

.PHONY: clean-here
clean-here:
	rm -rf ${DIR_LIST} config.log config.status $(PRPL_C_OBJS) $(PRPL_LIBNAME) po/*.mo

.PHONY: clean
clean: clean-here tgl/Makefile
	+make -C tgl clean
