CC ?= cc
CSTD ?= -std=c11
CFLAGS ?= -O2 -Wall -Wextra -Wpedantic $(CSTD)

SDL_CFLAGS := $(shell pkg-config --cflags SDL2 SDL2_ttf 2>/dev/null)
SDL_LIBS := $(shell pkg-config --libs SDL2 SDL2_ttf 2>/dev/null)
FFMPEG_CFLAGS := $(shell pkg-config --cflags libavformat libavcodec libswscale libswresample libavutil 2>/dev/null)
FFMPEG_LIBS := $(shell pkg-config --libs libavformat libavcodec libswscale libswresample libavutil 2>/dev/null)
MULTIARCH_CFLAGS := $(if $(wildcard /usr/include/i386-linux-gnu/SDL2/_real_SDL_config.h),-I/usr/include/i386-linux-gnu,)

ifeq ($(strip $(SDL_CFLAGS)),)
SDL_CFLAGS := $(shell sdl2-config --cflags 2>/dev/null) $(MULTIARCH_CFLAGS)
SDL_LIBS := $(shell sdl2-config --libs 2>/dev/null) -lSDL2_ttf
endif

ifeq ($(strip $(FFMPEG_LIBS)),)
FFMPEG_LIBS := -lavformat -lavcodec -lswscale -lswresample -lavutil
endif

SRC := $(wildcard src/*.c)
OBJ := $(SRC:.c=.o)
BIN := passport-c-media-player

CPPFLAGS += -I./src $(SDL_CFLAGS) $(FFMPEG_CFLAGS)
LDLIBS += $(SDL_LIBS) $(FFMPEG_LIBS) -lm

.PHONY: all run clean appimage

all: $(BIN)

$(BIN): $(OBJ)
	$(CC) $(OBJ) -o $@ $(LDLIBS)

run: $(BIN)
	./$(BIN)

clean:
	rm -f $(OBJ) $(BIN)

appimage: $(BIN)
	./build-appimage.sh
