# **************************************************************************** #
#                                                                              #
#                                                         :::      ::::::::    #
#    Makefile                                           :+:      :+:    :+:    #
#                                                     +:+ +:+         +:+      #
#    By: gbaconni@student.42lausanne.ch             +#+  +:+       +#+         #
#                                                 +#+#+#+#+#+   +#+            #
#    Created: 2022/01/18 15:11:16 by gbaconni          #+#    #+#              #
#    Updated: 2022/04/29 23:22:57 by gbaconni         ###   lausanne.ch        #
#                                                                              #
# **************************************************************************** #
#
# make ft_printf
# make all
# make clean
# make fclean
# make re
# make
#

GIT_REPO = git@vogsphere.42lausanne.ch:vogsphere/intra-uuid-fd8d00fc-8c22-400e-a8d9-dcc364688380-4092952-gbaconni
PDF_FR = https://cdn.intra.42.fr/pdf/pdf/50142/fr.subject.pdf
PDF_EN = https://cdn.intra.42.fr/pdf/pdf/50141/en.subject.pdf

INT_MIN = "-2147483648"
INT_MAX	= "2147483647"
UINT_MAX = "4294967295"
LONG_MIN = "-9223372036854775808"
LONG_MAX = "9223372036854775807"
ULONG_MAX = "18446744073709551615"

NAME = ft_printf

LIBFTPRINTF = libftprintf
LFLAGS = -L. -L$(LIBFTPRINTF) -lftprintf

SRC = main.c

INCLUDE = -I. -I$(LIBFTPRINTF)
HDR = $(LIBFTPRINTF)/libftprintf.h

CC = gcc
CFLAGS = -Wall -Wextra -Werror
ifeq ($(FAST), 1)
	CFLAGS += -pipe -O3 -ffast-math -fomit-frame-pointer -funroll-loops
	#CFLAGS += -pipe -Ofast -ffast-math -funroll-loops -fomit-frame-pointer
endif
ifeq ($(DEBUG), 1)
	CFLAGS += -g -O0 -fsanitize=address -fsanitize=undefined -fsanitize=signed-integer-overflow
endif
ifeq ($(BONUS), 1)
	CFLAGS += -D BONUS=1
endif

VALGRIND = valgrind
VALGRINDFLAGS = --quiet --leak-check=full --show-leak-kinds=all
ifeq ($(LEAK), 1)
	CFLAGS += -g -O0
	PREFIX += $(VALGRIND) $(VALGRINDFLAGS)
endif
KERNEL = $(shell uname -s)
MACHINE = $(shell uname -m)

RM = rm
RMFLAGS = -f

NORMINETTE = norminette
NORMINETTEFLAGS = -o

MAKE = make

all: $(NAME)

$(NAME):
	@$(MAKE) -C $(LIBFTPRINTF) all DEBUG=$(DEBUG) LEAK=$(LEAKS) FAST=$(FAST) >/dev/null 2>&1
ifeq ($(BONUS), 1)
	@$(MAKE) -C $(LIBFTPRINTF) bonus DEBUG=$(DEBUG) LEAK=$(LEAKS) FAST=$(FAST) >/dev/null 2>&1
endif
	@$(CC) $(CFLAGS) $(INCLUDE) $(SRC) $(LFLAGS) -o $@

clean:
	@$(RM) $(RMFLAGS) $(NAME)

fclean: clean
	@$(MAKE) -C $(LIBFTPRINTF) fclean

re: fclean all

bonus:
	@$(MAKE) -C $(LIBFTPRINTF) bonus DEBUG=$(DEBUG) LEAK=$(LEAKS) FAST=$(FAST)
	@$(MAKE) fclean all BONUS=1 DEBUG=$(DEBUG) LEAK=$(LEAKS) FAST=$(FAST)

rebonus:
	@$(MAKE) -C $(LIBFTPRINTF) rebonus DEBUG=$(DEBUG) LEAK=$(LEAKS) FAST=$(FAST)
	@$(MAKE) fclean all BONUS=1 DEBUG=$(DEBUG) LEAK=$(LEAKS) FAST=$(FAST)

check:
	@$(MAKE) -C $(LIBFTPRINTF) check

leak:
	@$(MAKE) re LEAK=1 BONUS=1 DEBUG=$(DEBUG) FAST=$(FAST)

debug:
	@$(MAKE) re DEBUG=1 BONUS=1 LEAK=$(LEAKS) FAST=$(FAST)

fast:
	@$(MAKE) re FAST=1 BONUS=1 DEBUG=$(DEBUG) LEAK=$(LEAKS)

test: bonus
	@$(PREFIX) ./$(NAME) "%c" C
	@$(PREFIX) ./$(NAME) "%s" "42 Lausanne"
	@$(PREFIX) ./$(NAME) "%s" ""
	@$(PREFIX) ./$(NAME) "%d" 42
	@$(PREFIX) ./$(NAME) "%i" $(INT_MIN)
	@$(PREFIX) ./$(NAME) "%i" $(INT_MAX)
	@$(PREFIX) ./$(NAME) "%u" $(UINT_MAX)
	@$(PREFIX) ./$(NAME) "%x" $(LONG_MIN)
	@$(PREFIX) ./$(NAME) "%X" $(LONG_MIN)
	@$(PREFIX) ./$(NAME) "%x" $(LONG_MAX)
	@$(PREFIX) ./$(NAME) "%X" $(LONG_MAX)
	@$(PREFIX) ./$(NAME) "%x" $(ULONG_MAX)
	@$(PREFIX) ./$(NAME) "%X" $(ULONG_MAX)
	@$(PREFIX) ./$(NAME) "%x" 42
	@$(PREFIX) ./$(NAME) "%X" 42
	@$(PREFIX) ./$(NAME) "%%" ""
	@$(PREFIX) ./$(NAME) "%p" ""
	@$(PREFIX) ./$(NAME) "%p" "\n"
	@$(PREFIX) ./$(NAME) "%p" $(LONG_MIN)
	@$(PREFIX) ./$(NAME) "%p" $(LONG_MAX)
	@$(PREFIX) ./$(NAME) "%p" $(ULONG_MAX)
	@$(PREFIX) ./$(NAME) "Forty Two" ""
	@$(PREFIX) ./$(NAME) "\t\r\n" ""
	@$(PREFIX) ./$(NAME) "%1d" 42

ifeq ($(KERNEL),Linux)
test2: leak
	@$(MAKE) test LEAK=1 DEBUG=$(DEBUG) FAST=$(FAST)

test21: leak
	@$(MAKE) test42 LEAK=1 DEBUG=$(DEBUG) FAST=$(FAST)
else
test2: debug
	@$(MAKE) test DEBUG=1 LEAK=$(LEAKS) FAST=$(FAST)

test21: debug
	@$(MAKE) test42 DEBUG=1 LEAK=$(LEAKS) FAST=$(FAST)
endif

test3: fast
	@$(MAKE) test FAST=1

test42: bonus
	@$(PREFIX) ./$(NAME) "Hello World %% %44s %d %c %x %p" "Lausanne" 42 C 66 "C"
	@$(PREFIX) ./$(NAME) "Characters: %c %c \n" 'a' 65
	@$(PREFIX) ./$(NAME) "Decimals: %d\n" 1977
	@$(PREFIX) ./$(NAME) "Preceding with blanks: %10d \n" 1977
	@$(PREFIX) ./$(NAME) "Preceding with zeros: %010d \n" 1977
	@$(PREFIX) ./$(NAME) "Some different radices: %d %x %#x\n" 100 100 100

watch: fclean
	@read -p "cmd: " cmd; while :; do clear; date "+%F %T (every 2.0s)"; echo; sh -c "$${cmd} 2>&1" | tail -n 10 || true; sleep 2; done

doc:
	@curl -s -L -z fr.subject.pdf -o fr.subject.pdf $(PDF_FR)
	@curl -s -L -z en.subject.pdf -o en.subject.pdf $(PDF_EN)

update: sync

sync: online pull
	@git ls-files --others --exclude-standard | xargs diff -Naur '' | less -F
	@git status | grep -q 'nothing to commit' || (git diff; read -p "Comment: " comment; git add -A; git commit -am "$${comment-$$(date '+%F %T')}"; git push)

online:
	@dig +short vogsphere.baco.net A | grep -qF '213.5.156.25' || echo 'vogsphere.baco.net unresolvable'
	@nc -vzw3 vogsphere.baco.net 443 >/dev/null 2>&1 || echo 'vogsphere.baco.net unreachable'

status:
	@git status

pull: config
	@git pull

config:
	@git config user.name "gbaconni"
	@git config user.email "gbaconni@student.42lausanne.ch"
	@git config pull.rebase false

42:
	@git clone $(GIT_REPO) 42 || true

21: 42
	@test -f 42/Makefile && make -C 42 pull
	@mv 42/.git .42.git
	@rsync --verbose --archive --delete --include '*.c' --include '*.h' --include 'Makefile' --include '*/' --exclude '*' libftprintf/ 42/
	@mv .42.git 42/.git
	@make -C 42 check fclean clean re all sync

