# CONFIGURE at least ROOT_DIR and INSTALL_DIR
# All paths without trailing slashes

# Absolute path to the directory containing all image directories as well as the staging directory
# eg: ROOT_DIR = /data/images
# ROOT_DIR		= 

# The URI at which the ROOT_DIR will be served
ROOT_PATH		= images

# Path of the directory containing all the files to be sorted, relative to ROOT_DIR
# eg: STAGING_DIR = .sort
STAGING_DIR 	= .sort

# Absolute path to the directory into which the application files will be installed
# eg: INSTALL_DIR = /www/imgsort2
# INSTALL_DIR		= 




# DO NOT CHANGE ANYTHING HERE
default: css
.PHONY: clean install stop test php css

# Installation
ifndef ROOT_DIR
$(error Edit this Makefile and set the ROOT_DIR variable)
endif
ifndef INSTALL_DIR
$(error Edit this Makefile and set the INSTALL_DIR variable)
endif
ifndef STAGING_DIR
$(error Edit this Makefile and set the STAGING_DIR variable)
endif
ifndef ROOT_PATH
$(error Edit this Makefile and set the ROOT_PATH variable)
endif

SRC_DIR			= src
RES_DIR			= resources
_SRC_FLS 		= config.html config.js index.html index.js imgsort.php style.css
SRC_FLS    		= $(foreach f,$(_SRC_FLS),$(SRC_DIR)/$(f))
INSTALL_FLS 	= $(foreach f,$(_SRC_FLS),$(INSTALL_DIR)/$(f)) $(INSTALL_DIR)/favicon.png
SASS_CMD 		= sass --color --load-path=src/sass



install: $(INSTALL_FLS) imgsort2-nginx.conf imgsort2-php-fpm.conf 


$(INSTALL_DIR)/%.php: $(SRC_DIR)/%.php
	sed 's|$$rootDir = .*|$$rootDir = "$(ROOT_DIR)/";|; s|$$rootPath = .*|$$rootPath = "$(ROOT_PATH)/";|; s|$$stagingDirName = .*|$$stagingDirName = "$(STAGING_DIR)/";|' $< > $@
	chmod 0744 $@

$(INSTALL_DIR)/%.png: $(RES_DIR)/%.png
	install -m 0744 $< $@

$(INSTALL_DIR)/%: $(SRC_DIR)/%
	install -m 0744 $< $@

%.conf: $(RES_DIR)/%.conf
	sed 's|/www/imgsort2|$(INSTALL_DIR)|; s|/data/images|$(ROOT_DIR)|; s|/images|/$(ROOT_PATH)|;' $< > $@
	chmod 0740 $@

clean:
	rm $(INSTALL_FLS)

# Compile the sass
css:
	$(SASS_CMD) --no-indented --no-source-map src/sass/main.sass src/style.css
# Testing
php:
	php -S localhost:8000 -t . &
	firefox http://localhost:8000/src/index.html
stop:
	-killall php


TESTDIR = images
# Create a image directory for testing.
# This should work with the default configuration
test:
	-@rm -r $(TESTDIR)
	-@mkdir -p $(TESTDIR) $(TESTDIR)/.sort
	-@cp resources/* $(TESTDIR)/.sort

