# This file is part of cchttpd.
# Copyright (C) 2012-2014  Guillaume Quintin.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

.POSIX:
.DEFAULT:
.SUFFIXES:




# Installation directories
PREFIX      = /opt/local
BIN_DIR     = $(PREFIX)/bin
TRAFFIC_DIR = $(PREFIX)/bin
MAN_DIR     = $(PREFIX)/share/man/man1




VERSION   = 0.6
CC        = cc
CFLAGS    = -Wall -Wextra -ansi -pedantic-errors -O2
LDFLAGS   = 
BIN       = cchttpd
SOURCES   = cchttpd.c send.c get_method.c logger.c mime.c mycgi.c \
            init.c socket.c signals.c cchttpd.h
FILES     = $(SOURCES) Makefile README COPYING cchttpd.man cchttpd.1 \
            index.htm mycgi_test
TRAFFIC   = traffic
VOMITPAGE = vomitpage

all: $(BIN)

$(BIN): Makefile $(SOURCES)
	$(CC) $(LDFLAGS) $(CFLAGS) $(BIN).c -o $(BIN)

$(BIN).1: Makefile $(BIN).man
	echo ".\\\" This file is part of $(BIN)" > $@
	echo ".\\\" Copyright 2012-2014  Guillaume Quintin" >> $@
	echo ".\\\"" >> $@
	sed -n '296,308 p' COPYING | sed 's/^ */.\\" /g' >> $@
	echo ".TH $(BIN) 1" >> $@
	$(VOMITPAGE) --man $(BIN).man >> $@

install: all
	mkdir -p $(BIN_DIR)
	cp $(BIN) $(BIN_DIR)/$(BIN)
	mkdir -p $(MAN_DIR)
	cp $(BIN).1 $(MAN_DIR)/$(BIN).1

uninstall:
	rm $(BIN_DIR)/$(BIN)
	rm $(MAN_DIR)/$(BIN).1

clean:
	rm -f $(BIN)

maintainer-clean: clean
	rm -rf *.o *~ *.tar.bz2 pre_dist/

prepare-dist:
	mkdir -p pre_dist
	rm -rf pre_dist/*
	cp $(FILES) pre_dist

dist: prepare-dist
	mv pre_dist $(BIN)-$(VERSION) 
	tar cvf $(BIN)-$(VERSION).tar $(BIN)-$(VERSION)/
	bzip2 -f $(BIN)-$(VERSION).tar
	rm -rf $(BIN)-$(VERSION)

###################
#   Misc stuff
###################

install-traffic:
	mkdir -p $(TRAFFIC_DIR)
	cp $(TRAFFIC) $(TRAFFIC_DIR)/$(TRAFFIC)

uninstall-traffic:
	rm -f $(TRAFFIC_DIR)/$(TRAFFIC)
