KDIR := /lib/modules/$(shell uname -r)/build


ifeq ($(KERNELRELEASE),)
  SUBDIR = $(shell pwd)

else
  SUBDIR = $(SUBDIRS)
  
  #search .INCLUDE_DIRS so it works on Debian
  # but this doesn't seem to work on v5.0.0+
  have_include = $(firstword $(foreach dir,$(.INCLUDE_DIRS),$(wildcard $(dir)/include/$(1))))
  
# detect presence of include files
ifneq ($(call have_include,media/v4l2-common.h),)
EXTRA_CFLAGS += -DHAVE_V4L2_COMMON
endif
v4l2-dev := $(call have_include,media/v4l2-dev.h)
ifneq ($(v4l2-dev),)
EXTRA_CFLAGS += -DHAVE_V4L2_DEV
ifneq ($(shell grep video_device_node_name $(v4l2-dev)),)
EXTRA_CFLAGS += -DHAVE_VIDEO_DEVICE_NODE_NAME
endif
endif
ifneq ($(call have_include,media/v4l2-device.h),)
EXTRA_CFLAGS += -DHAVE_V4L2_DEVICE
endif
ifneq ($(call have_include,media/v4l2-event.h),)
EXTRA_CFLAGS += -DHAVE_V4L2_EVENT
endif
ifneq ($(call have_include,media/v4l2-ioctl.h),)
EXTRA_CFLAGS += -DHAVE_V4L2_IOCTL
endif
videobuf-vmalloc := $(call have_include,media/videobuf-vmalloc.h)
ifneq ($(videobuf-vmalloc),)
ifneq ($(shell grep ext_lock $(videobuf-vmalloc)),)
EXTRA_CFLAGS += -DHAVE_VIDEOBUF_VMALLOC_EXT_LOCK
endif
endif
linux-firmware := $(call have_include,linux/firmware.h)
ifneq ($(linux-firmware),)
ifneq ($(shell grep "gfp_t gfp" $(linux-firmware)),)
EXTRA_CFLAGS += -DHAVE_REQUEST_FIRMWARE_GFP
endif
endif

endif

all: modules

obj-m := s2253.o

ifeq ($(shell id -u),0)
  SUDO =
  pulseaudio = su -c "pulseaudio $(1)" $(shell ps -o user -C pulseaudio | tail -1)
else
  SUDO = sudo 
  pulseaudio = pulseaudio $(1)
endif


#EXTRA_CFLAGS += -Wall -Wextra -Wsign-compare -Wno-unused -Wno-unused-parameter 
#EXTRA_CFLAGS += -Wno-unused -g -O0

# uncomment the following lines to include the firmware inside the module
#EXTRA_CFLAGS += -DINCLUDE_S2253_FW
#s2253fw.h: s2253.fw makefw_h
#	./makefw_h s2253_fw s2253.fw s2253fw.h
#modules: s2253fw.h

KBUILD_CFLAGS += -fno-pie -Wno-pointer-sign

modules:
	echo $(.INCLUDE_DIRS)
	$(MAKE) -C $(KDIR) M=$(SUBDIR) $@

clean:
	$(MAKE) -C $(KDIR) M=$(SUBDIR) $@

install: root_check unload
	$(MAKE) -C $(KDIR) M=$(SUBDIR) modules_install
	/sbin/depmod $(shell uname -r)
	install s2253.fw /lib/firmware/
	/sbin/modprobe s2253

root_check:
	@id -u | grep "^0" || { \
		echo "Try again as root or use 'sudo make install'"; false; \
	}


uninstall: root_check
	-/sbin/modprobe -q -r s2253
	rm -vf /lib/modules/$(shell uname -r)/extra/s2253.ko
	/sbin/depmod $(shell uname -r)
	rm -vf /lib/firmware/s2253.fw

load:
	#$(SUDO)/sbin/modprobe v4l1_compat
	$(SUDO)/sbin/modprobe videodev
	$(SUDO)/sbin/modprobe v4l2_common || true
	$(SUDO)/sbin/modprobe videobuf_core debug=0 || $(SUDO)/sbin/modprobe video-buf
	$(SUDO)/sbin/modprobe videobuf_vmalloc debug=0 || true
	$(SUDO)/sbin/modprobe videobuf2_vmalloc || true
	$(SUDO)/sbin/modprobe videobuf2_core || true
	$(SUDO)/sbin/modprobe videobuf2_common || true
	$(SUDO)/sbin/modprobe videobuf2_v4l2 || true
	$(SUDO)/sbin/insmod ./s2253.ko debug=0

unload:
	@if /sbin/lsmod | grep -q "^s2253 "; then			\
		echo "Unloading s2253 module."			;\
		$(SUDO)/sbin/rmmod s2253 || if $(call pulseaudio,--check); then \
			echo "Stopping pulseaudio."		;\
			$(call pulseaudio,-k)			;\
			echo "Retrying unload s2253 module."	;\
			$(SUDO)/sbin/rmmod  s2253		;\
			echo "Starting pulseaudio."		;\
			$(call pulseaudio,--start)		;\
		else						\
			echo "Failed to unload. Please check if any programs are using the device."	;\
			false					;\
		fi						;\
	fi



arm:
	PATH=$(PATH):/opt/arm-2013.05/bin/ $(MAKE) -C /tmp/linux-cm-t33/ SUBDIRS=$(SUBDIR) modules ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi-
