# Makefile for simp_httpd_m

# 编译器
CC = gcc

# 目标可执行文件
TARGET = sim_httpd_m

# 源文件
SRC = simp_httpd_m.c

# 配置文件
CONFIG = config.json

# 检测操作系统
UNAME_S := $(shell uname -s)

# 根据操作系统设置编译标志和安装路径
ifeq ($(UNAME_S),Darwin)
    # macOS
    CFLAGS = -Wall -Wextra
    INSTALL_BIN = /usr/local/opt/simp_httpd
    INSTALL_CONFIG = /usr/local/opt/simp_httpd
    INSTALL_CMD = install
else
    # 假设其他系统是 Linux
    CFLAGS = -Wall -Wextra -lpthread
    INSTALL_BIN = /opt/simp_httpd
    INSTALL_CONFIG = /opt/simp_httpd
    INSTALL_CMD = install
endif

# 默认目标
all: $(TARGET)

# 编译规则
$(TARGET): $(SRC)
	$(CC) $(CFLAGS) -o $@ $^

# 安装规则
install: $(TARGET)
	@echo "Installing $(TARGET) to $(INSTALL_BIN)"
	@$(INSTALL_CMD) -d $(INSTALL_BIN)
	@$(INSTALL_CMD) -m 755 $(TARGET) $(INSTALL_BIN)
	@echo "Installing $(CONFIG) to $(INSTALL_CONFIG)"
	@$(INSTALL_CMD) -d $(INSTALL_CONFIG)
	@$(INSTALL_CMD) -m 644 $(CONFIG) $(INSTALL_CONFIG)
	@echo "Installation complete."

# 卸载规则
uninstall:
	@echo "Removing $(TARGET) from $(INSTALL_BIN)"
	@rm -f $(INSTALL_BIN)/$(TARGET)
	@echo "Removing $(CONFIG) from $(INSTALL_CONFIG)"
	@rm -f $(INSTALL_CONFIG)/$(CONFIG)
	@echo "Uninstallation complete."

# 清理规则
clean:
	rm -f $(TARGET)

# 伪目标
.PHONY: all clean install uninstall