#
# Nice make. Use make VERBOSE=1 to verbose compilation.
#
ifneq ($(VERBOSE), 1)
.SILENT:
E = @echo
else
E = @:
endif

all: 
	@echo "Welcome to RSBAC!"
	@echo "To compile all packages, type:"
	@echo "\`\`make build'"
	@echo 
	@echo "To install all packages, as administrator user, type:"
	@echo "\`\`make install' or \`\`make install-strip'"
	@echo 
	@echo "To uninstall all packages, as administrator user, type:"
	@echo "\`\`make uninstall'"
	@echo 
	@echo "To compile or install per package, type:"
	@echo "\`\`make <package>' or \`\`make <package>-install'"
	@echo 
	@echo "with package one of:"
	@echo 
	@echo " ---------------------------------------"
	@echo "|   headers libs nss pam tools   |"
	@echo " ---------------------------------------"
	@echo 
	@echo "Or change to the right directory and use the traditional"
	@echo "make; su; make install"
	@echo
	@echo "To disable User Management (UM) related packages use:"
	@echo "\`\`make build-no-um' then \`\`make inst-no-um' as admininistrator"
	@echo
	@echo "Type make <target> VERBOSE=1 to display compilation lines."
	@echo
	@echo "Be sure to read the \`\`INSTALL' and \`\`README' files for"
	@echo "more information."
	@echo 
	@echo "The RSBAC team - http://www.rsbac.org"


inst-no-um: headers-install libs-install tools-install

build-no-um: libs tools

build: libs pam tools nss
	
install: headers-install libs-install pam-install \
tools-install nss-install

install-strip: headers-install libs-install pam-install \
tools-install-strip nss-install

uninstall: pam-uninstall tools-uninstall nss-uninstall \
libs-uninstall headers-uninstall

clean: libs-clean pam-clean tools-clean nss-clean

distclean : clean

# Display function
E = @echo;echo "$(1)";echo " -------------------------------------"

#
# Building
#

libs:
	$(call E, "Building RSBAC Libraries...")
	@$(MAKE) -C main/libs

nss: libs
	$(call E, "Building RSBAC NSS...")
	@$(MAKE) -C main/nss

pam: libs
	$(call E, "Building RSBAC PAM...")
	@$(MAKE) -C main/pam

tools: libs
	$(call E, "Building RSBAC tools...")
	@$(MAKE) -C main/tools

#
# Installations
#

headers-install:
	$(call E, "Installing RSBAC headers...")
	@$(MAKE) install -C main/headers

libs-install: headers-install libs
	$(call E, "Installing RSBAC libraries...")
	@$(MAKE) install -C main/libs

nss-install: headers-install libs-install nss
	$(call E, "Installing RSBAC NSS...")
	@$(MAKE) install -C main/nss

pam-install: headers-install libs-install pam
	$(call E, "Installing RSBAC PAM...")
	@$(MAKE) install -C main/pam

tools-install: headers-install libs-install tools
	$(call E, "Installing RSBAC tools...")
	@$(MAKE) install -C main/tools

tools-install-strip: headers-install libs-install tools
	$(call E, "Installing RSBAC tools \(stripped\)...")
	@$(MAKE) install-strip -C main/tools

#
# Uninstallations
#

headers-uninstall: nss-uninstall pam-uninstall tools-uninstall libs-uninstall
	$(call E, "Uninstalling RSBAC headers...")
	@-make uninstall -C main/headers

libs-uninstall: nss-uninstall pam-uninstall tools-uninstall
	$(call E, "Uninstalling RSBAC libraries...")
	@-make uninstall clean -C main/libs

nss-uninstall:
	$(call E, "Uninstalling RSBAC NSS...")
	@-make uninstall clean -C main/nss

pam-uninstall:
	$(call E, "Uninstalling RSBAC PAM...")
	@-make uninstall clean -C main/pam

tools-uninstall: libs-uninstall
	$(call E, "Uninstalling RSBAC tools...")
	@-make uninstall clean -C main/tools

#
# Cleaning
#

libs-clean:
	$(call E, "Cleaning RSBAC Libraries...")
	@$(MAKE) distclean -C main/libs

nss-clean:
	$(call E, "Cleaning RSBAC NSS...")
	@$(MAKE) distclean -C main/nss
	
pam-clean:
	$(call E, "Cleaning RSBAC PAM...")
	@$(MAKE) distclean -C main/pam
	
tools-clean:
	$(call E, "Cleaning RSBAC tools...")
	@$(MAKE) distclean -C main/tools

#
# Maintenance
#
OLD_MAJOR_VERSION := 1
OLD_MID_VERSION   := 5
OLD_MINOR_VERSION := 5

NEW_MAJOR_VERSION := 1
NEW_MID_VERSION   := 5
NEW_MINOR_VERSION := 6

OLDVERSION	:= ${OLD_MAJOR_VERSION}.${OLD_MID_VERSION}.${OLD_MINOR_VERSION}
NEWVERSION	:= ${NEW_MAJOR_VERSION}.${NEW_MID_VERSION}.${NEW_MINOR_VERSION}

bump-version:
	@echo "Upgrading version strings ${OLDVERSION} to ${NEWVERSION}"
	@sed -i s/RSBAC\ Administration\ Tools\ ${OLDVERSION}/RSBAC\ Administration\ Tools\ ${NEWVERSION}/ main/tools/src/scripts/*
	@sed -i s/:=\ ${OLDVERSION}/:=\ ${NEWVERSION}/ main/libs/Makefile
	@sed -i s/:=\ ${OLDVERSION}/:=\ ${NEWVERSION}/ main/tools/Makefile
	@sed -i s/define\ RSBAC_VERSION\ \"${OLDVERSION}\"/define\ RSBAC_VERSION\ \"${NEWVERSION}\"/ main/headers/rsbac/types.h
	@sed -i s/define\ RSBAC_VERSION_MINOR\ ${OLD_MINOR_VERSION}/define\ RSBAC_VERSION_MINOR\ ${NEW_MINOR_VERSION}/ main/headers/rsbac/types.h
	@sed -i s/define\ RSBAC_VERSION_MID\ ${OLD_MID_VERSION}/define\ RSBAC_VERSION_MID\ ${NEW_MID_VERSION}/ main/headers/rsbac/types.h
	@sed -i s/define\ RSBAC_VERSION_MAJOR\ ${OLD_MAJOR_VERSION}/define\ RSBAC_VERSION_MAJOR\ ${NEW_MAJOR_VERSION}/ main/headers/rsbac/types.h
	@echo "Don't forget to update main/tools/Changes and debian/changelog"
.PHONY: all build clean distclean
