#
# Makefile
#
# This file is part of SeLinux ALTLinux Policy
# Copyright (C) 2013 Andrew V. Stepanov <stanv@altlinux.ru> 
#
# This file is covered by the GNU General Public License,
# which should be included with mkimage as the file COPYING.
#

MAKEFLAGS=-r

NAME = altlinux
num_sens = 6
num_cats = 16

M4PARAM = -s -D mls_num_sens=$(num_sens) -D mls_num_cats=$(num_cats)

# installation paths
QUIET ?= n
SHAREDIR ?= /usr/share/selinux
HEADERDIR ?= $(SHAREDIR)/$(NAME)
SUPPORTDIR ?= $(SHAREDIR)/$(NAME)/support

ifeq ($(QUIET),y)
        verbose := @
endif

detected_mods := $(wildcard *.te)
all_packages := $(notdir $(detected_mods:.te=.pp))
detected_ifs := $(wildcard *.if)

#
# Interfaces
#
header_interfaces := $(wildcard $(HEADERDIR)/*.if)

#
# Policy headers
#
m4support = $(wildcard $(SUPPORTDIR)/*.spt) $(wildcard $(HEADERDIR)/*.spt)

#
# Main targets
#
all: $(all_packages)

$(info Policy: $(NAME))

#
# Build module packages
#
tmp/all_interfaces.conf: $(m4support) $(header_interfaces) $(detected_ifs)
	@echo "Create: $@"
	@test -d $(@D) || mkdir -p $(@D)
	@echo "ifdef(\`__if_error',\`m4exit(1)')" > tmp/iferror.m4
	@echo "divert(-1)" > $@
	$(verbose) m4 $^ tmp/iferror.m4 | sed -e s/dollarsstar/\$$\*/g >> $@
	@echo "divert" >> $@

.PRECIOUS: tmp/%.mod tmp/%.fc.out tmp/%.fc.in

tmp/%.fc.in:
	@echo "Create IN file context: $@"
	@if [ -f "$*.fc" ]; then \
			echo "Cp $*.fc to $@"; \
			cp -f $*.fc $@; \
			else \
			echo "Create new empty file $@"; \
			touch $@; \
			fi

# Expand macroses in .fc
tmp/%.fc.out: $(m4support) tmp/%.fc.in
	@echo "Create file context: $@"
	$(verbose) m4 $(M4PARAM) $^ > $@

tmp/%.mod: $(m4support) tmp/all_interfaces.conf %.te
	@echo "Compiling: $@"
	@test -d $(@D) || mkdir -p $(@D)
	$(verbose) m4 $(M4PARAM) $^ > $(@:.mod=.te.out)
	$(verbose) checkmodule -M -m $(@:.mod=.te.out) -o $@

%.pp: tmp/%.mod tmp/%.fc.out
	@echo "Creating package: $@"
	$(verbose) semodule_package -o $@ -m $< -f tmp/$*.fc.out


.PHONY: load

#
# Load module packages
#
load: $(all_packages)
	@echo "Loading $(NAME) modules: $(basename $(notdir $?))"
	$(verbose) semodule $(foreach mod,$?,-i $(mod))


#
# Clean the environment
#
clean:
	rm -fR tmp
	rm -f $(all_packages)

# vi:set ts=4 sw=4 noet ai nocindent syntax=make:
