CC = gcc
###############################################################################################
# Use the following symbols for normal operation (using gpio daemon library)
# Note - the pigpiod daemon needs to be started using -x to enable GPIO0 and GPIO1
LDFLAGS = -lpthread -lpigpiod_if2 -lrt -lm -lmpv					# FOR NORMAL USE

# NOTE: The 32 bit Bullseye Pi operating system contains the Y2038 time bug, and will cause problems
# after 03:14:07 UTC on 19 January 2038. Refer to https://en.wikipedia.org/wiki/Year_2038_problem
# The bug has been fixed in 32 bit Bookworm. The 64 bit versions of the OS don't have the bug.
CFLAGS = -O3 -Wall -Wextra -DUSE_GPIO_DAEMON -D_FILE_OFFSET_BITS=64 -D_TIME_BITS=64	# FOR NORMAL USE

# Use the following symbols for normal operation (using direct calling gpio library)
#LDFLAGS = -lpthread -lpigpio -lrt -lm -lmpv
#CFLAGS = -O3 -Wall -Wextra -D_FILE_OFFSET_BITS=64 -D_TIME_BITS=64
#---------------
# Using the GDB debugger:
#
# Add the following to /etc/sysctl.d/coredumps.conf to save core dumps to /tmp/cores
# kernel.core_pattern = /tmp/cores/core-%e-sig%s-user%u-group%g-pid%p-time%t
# kernel.core_uses_pid = 1
# fs.suid_dumpable = 2
#
# Add the following to /etc/tmpfiles.d/coredumps.conf to clear cores after three days
# D /tmp/cores 0777 root root 3d
#
# Run the following command to enable core dumps on this Pi
# sed -i 's/^.*soft[[:space:]]*core.*$/*                soft    core            unlimited/g' /etc/security/limits.conf
#
# Use the following when debugging using gdb, pigpiod daemon
#LDFLAGS = -lpthread -lpigpiod_if2 -lrt -lm -lmpv -rdynamic				# USE FOR DEBUGGING WITH GDB ONLY
#CFLAGS = -Og -g -Wall -Wextra -funwind-tables -DDEBUG -DUSE_GPIO_DAEMON -D_FILE_OFFSET_BITS=64 -D_TIME_BITS=64	# USE FOR DEBUGGING WITH GDB ONLY

# Uset the following when debugging using gdb, direct calling gpio library
#LDFLAGS = -lpthread -lpigpio -lrt -lm -lmpv -rdynamic
#CFLAGS = -Og -g -Wall -Wextra -funwind-tables -DDEBUG -D_FILE_OFFSET_BITS=64 -D_TIME_BITS=64
###############################################################################################
###############################################################################################
all: alarm-clock
###############################################################################################
alarm-clock.o : alarm-clock.c
	-$(CC) -c alarm-clock.c -o $@ $(CFLAGS)
###############################################################################################
alarm-clock : alarm-clock.o
	-$(CC) alarm-clock.o -o $@ $(LDFLAGS)
	-chmod 755 alarm-clock
	-sudo chown root:gpio alarm-clock
###############################################################################################
###############################################################################################
install-web:
	-sudo chmod 644 html/*
	-sudo chmod 755 cgi-bin/*
	-sudo cp html/* /var/www/html
	-sudo cp cgi-bin/* /usr/lib/cgi-bin

install-no-reboot: all install-web
# If the configuration directories don't exist, create them
	-sudo mkdir -p /etc/alarm-clock
	-sudo mkdir -p /etc/alarm-clock/playlists
	-sudo chmod 775 /etc/alarm-clock /etc/alarm-clock/playlists

# The following ifeq sections test for the existance of the setup and alarm files, and if
# they are missing, will 'touch' the files to create blanks.
ifeq (,$(wildcard /etc/alarm-clock/setup.conf))
	-sudo touch /etc/alarm-clock/setup.conf
endif

ifeq (,$(wildcard /etc/alarm-clock/alarms.csv))
	-sudo touch /etc/alarm-clock/alarms.csv
endif

# Install the clock software and ensure runnable and owned by correct account
	-sudo mv alarm-clock /usr/local/bin
	-sudo chown -R www-data:www-data /etc/alarm-clock
	-sudo chmod 664 /etc/alarm-clock/setup.conf /etc/alarm-clock/alarms.csv

install: install-no-reboot
	-sudo reboot-seconds 5 'Make completed and installed. System will reboot shortly.'
###############################################################################################
clean:
	-rm -f *.o *~ alarm-clock
