#!/usr/bin/make -f
##
## Dovecot: Makefile to compile system-wide Sieve scripts
##
## Template by OSS Technology Corp. <http://www.OSSTech.co.jp/>
## Date: 2014-04-09, since 2014-04-09

DOVECOT_SYSCONF_DIR=	/opt/osstech/etc/dovecot
DOVECOT_CACHE_DIR=	/opt/osstech/var/cache/dovecot
DOVECOT_SIEVEC=		/opt/osstech/bin/sievec

default:
	@cd $(DOVECOT_SYSCONF_DIR) && $(MAKE) all

all: sieve

sieve:
	@set -e; \
	cd $(DOVECOT_SYSCONF_DIR); \
	umask 0022; \
	for sieve in sieve-*.d/*.sieve; do \
	  [ -f "$$sieve" ] || continue; \
	  svbin="$(DOVECOT_CACHE_DIR)/$${sieve%.sieve}.svbin"; \
	  [ "$$sieve" -nt "$$svbin" ] || continue; \
	  echo "Compiling $$sieve into $$svbin ..."; \
	  $(DOVECOT_SIEVEC) "$$sieve" "$$svbin.$$.tmp"; \
	  chmod 0644 "$$svbin.$$.tmp"; \
	  mv "$$svbin.$$.tmp" "$$svbin"; \
	  cp -p "$$sieve" "$(DOVECOT_CACHE_DIR)/$$sieve"; \
	done
	@set -e; \
	cd $(DOVECOT_CACHE_DIR); \
	for svbin in sieve-*.d/*.svbin; do \
	  [ -f "$$svbin" ] || continue; \
	  sieve="$${svbin%.svbin}.sieve"; \
	  [ -f "$(DOVECOT_SYSCONF_DIR)/$$sieve" ] && continue; \
	  echo "Removing $$sieve and $$svbin ..."; \
	  rm "$$sieve" "$$svbin"; \
	done

.PHONY: default all sieve

