From: Florian Lohoff <f@zz.de>
Date: Wed, 11 Apr 2012 15:29:18 +0200
Subject: Make building easier by using pkg-config

Updated with suggestion from https://bugs.debian.org/929345
on cross compilation.

Last-Update: 2021-02-12
---
 .gitignore       |  1 +
 Makefile         | 17 ++++++++++++-----
 Makefile.defs.sh | 17 +++++++++++++++++
 3 files changed, 30 insertions(+), 5 deletions(-)
 create mode 100644 Makefile.defs.sh

diff --git a/.gitignore b/.gitignore
index 70bce91..37e616c 100644
--- a/.gitignore
+++ b/.gitignore
@@ -8,5 +8,6 @@ core*
 *.rej
 tsdecode
 getstream
+Makefile.defs
 
 !.gitignore
diff --git a/Makefile b/Makefile
index 74a996a..501467f 100644
--- a/Makefile
+++ b/Makefile
@@ -1,11 +1,17 @@
-CC=gcc
-CFLAGS=-Wall  -I. -I/usr/include/glib-2.0/ -I/usr/lib/glib-2.0/include/
+CC ?= gcc
+CFLAGS=-Wall -I. 
+
 ifdef OPT
 CFLAGS+=-O2
 else
 CFLAGS+=-O0 -g
 endif
-LDFLAGS=-levent -lglib-2.0 -lpthread
+
+LDFLAGS=-lpthread
+
+MDEFS := $(shell sh Makefile.defs.sh >Makefile.defs)
+include Makefile.defs
+
 OBJ-getstream=getstream.o fe.o crc32.o \
 	libhttp.o libconf.o config.o util.o logging.o \
 	stream.o input.o \
@@ -20,15 +26,16 @@ OBJ-tsdecode=tsdecode.o psi.o crc32.o
 all: getstream tsdecode
 
 tsdecode: $(OBJ-tsdecode)
-	gcc -o $@ $+ $(LDFLAGS)
+	$(CC) -o $@ $+ $(LDFLAGS)
 
 getstream: $(OBJ-getstream)
-	gcc -o $@ $+ $(LDFLAGS) 
+	$(CC) -o $@ $+ $(LDFLAGS) 
 
 clean:
 	-rm -f $(OBJ-getstream) $(OBJ-tsdecode)
 	-rm -f getstream tsdecode 
 	-rm -f core vgcore.pid* core.* gmon.out
+	-rm -f Makefile.defs
 
 distclean: clean
 	-rm -rf CVS .cvsignore
diff --git a/Makefile.defs.sh b/Makefile.defs.sh
new file mode 100644
index 0000000..35248ba
--- /dev/null
+++ b/Makefile.defs.sh
@@ -0,0 +1,17 @@
+: "${PKG_CONFIG:=pkg-config}"
+echo CFLAGS+=`${PKG_CONFIG} --cflags glib-2.0`
+echo LDFLAGS+=`${PKG_CONFIG} --libs glib-2.0`
+
+libeventlibs=`${PKG_CONFIG} --libs libevent 2>/dev/null`
+libeventcflags=`${PKG_CONFIG} --cflags libevent 2>/dev/null`
+
+if [ -z "${libeventcflags}" ]; then
+	libeventcflags=
+fi
+
+if [ -z "${libeventlibs}" ]; then
+	libeventlibs=-levent
+fi
+
+echo CFLAGS+=${libeventcflags}
+echo LDFLAGS+=${libeventlibs}
