FONTS  = $(shell find fonts -type f)
CSS    = base.css index.css where.css comms.css ANGELF~1.CSS id.css
PAGES  = index.html where.html comms.html ANGELF~1.HTM pubkey.txt id.html
MEDIA  = $(patsubst %,media/%, \
    bg.png bg_2x.png bg_dim.png bg_2x_dim.png kesi.png quoxtrans.png \
    mlem.gif mlem_static.png chitter.png lockpen.png \
    quobl.png quobl2.png \
  ) \
  $(wildcard media/*.svg) $(wildcard media/flags/*)
AFMEDIA = $(patsubst %,NEWFOL~1/%, \
    GEC.JPG PBRUSH.GIF PROGRAM.GIF QUESTION.GIF MAIL.GIF LOWBATT.GIF \
    W95FA.otf w95fa.woff w95fa.woff2 \
  )

BUILDDIR ?= _build
TMPDIR   ?= _tmp

HOST ?= yummy.cricket
REMOTE_USER ?= www-data
IDFILE ?= ~/.ssh/xyz
REMOTE_DIR ?= yummy

all: build

build: $(patsubst %,$(BUILDDIR)/%,$(FONTS) $(CSS) $(PAGES) $(MEDIA) $(AFMEDIA))

upload: build
	@rsync --recursive --partial --progress --copy-links \
	       --compress --human-readable --hard-links --size-only \
	       --delete --delete-after \
	       --rsh='ssh -l $(REMOTE_USER) -i $(IDFILE)' \
	       $(BUILDDIR)/ $(HOST):$(REMOTE_DIR)/

$(BUILDDIR)/%: %
	@echo $*
	@mkdir -p $(dir $@)
	@cp $< $@

$(BUILDDIR)/%.gif: %_bg.gif
	@echo $(notdir $@)
	@mkdir -p $(dir $(BUILDDIR)/$* $(TMPDIR)/$*)
	@gifsicle -U $*_bg.gif -o $(TMPDIR)/$*_bg_u.gif
	@convert -transparent '#ff9bc7' $(TMPDIR)/$*_bg_u.gif $(TMPDIR)/$*_t.gif
	@gifsicle --disposal=previous $(TMPDIR)/$*_t.gif -o $(BUILDDIR)/$*.gif

$(BUILDDIR)/%_small.png: %.svg
	@echo $(notdir $@)
	@mkdir -p $(dir $@)
	@inkscape -e $@ -h 16 $< >/dev/null

$(BUILDDIR)/%_large.png: %.svg
	@echo $(notdir $@)
	@mkdir -p $(dir $@)
	@inkscape -e $@ -h 30 $< >/dev/null

$(BUILDDIR)/%_dim.png: %.png
	@echo $(notdir $@)
	@convert -channel A -evaluate Multiply 0.75 $< $@

$(BUILDDIR)/%.GIF: %.ICO
	@echo $*.GIF
	@mkdir -p $(dir $@)
	@convert '$<[0]' $@

clean:
	$(RM) -r $(TMPDIR) $(BUILDDIR)

.PHONY: clean all build upload
