# ==============================================================================
# Top level Makefile
#
# Switch 'make' into POSIX compliant mode (must be the first non-comment line!)
.POSIX:

# Clear suffix list for inference rules
.SUFFIXES:


# ==============================================================================
# Main rules

# If nothing is specified on the command line, make will use the first rule
# Build anything except the documentation
all: config
	@printf "\n"
	@printf "Building binary ...\n"
	cd src ; $(MAKE) $@

# Build configuration
config: cfg/.cfg_valid

# Run tests
test: config
	@printf "\n"
	@printf "Running tests ...\n"
	cd src ; $(MAKE) $@

# Clean source tree
clean: config
	@printf "\n"
	@printf "Cleaning source tree ...\n"
	cd src ; $(MAKE) $@

# Clean source tree and configuration
distclean: clean
	@printf "\n"
	@printf "Deleting configuration ...\n"
	if test -f ./CONFIG ; then . ./CONFIG ; $$UTIL_RM -rf ./cfg ; fi
	if test -f ./CONFIG ; then . ./CONFIG ; $$UTIL_RM -f config.log ; fi

# Clean source tree, configuration and list with installed files
allclean: distclean
	@printf "\n"
	@printf "Deleting list with installed files ...\n"
	if test -f ./CONFIG ; then \
           . ./CONFIG ; $$UTIL_RM -f src/installed_files ; \
        fi
	@printf "\nSource tree should now be in pristine state.\n"

# For compatibility with GNU autotools
maintainer-clean: allclean

# Install files
install: config
	@printf "\n"
	@printf "Install files to system ...\n"
	cd src ; $(MAKE) $@

# Install files and strip binaries
install-strip: config
	@printf "\n"
	@printf "Install files to system and strip executables ...\n"
	cd src ; $(MAKE) $@

# Uninstall files
uninstall:
	@printf "\n"
	@printf "Uninstall files from system ...\n"
	@printf "Using file list: ./src/installed_files\n"
	@if test ! -f src/installed_files ; \
        then \
           printf "Error: No list with installed files found\n" ; \
           exit 1 ; \
        fi
	@if test ! -f ./CONFIG ; \
        then \
           printf "Error: Configuration file not found\n" ; \
           exit 1 ; \
        fi
	@LC_ALL=POSIX ; export LC_ALL ; \
        while read pathname ; \
        do \
           printf "%s" "Deleting file $$pathname ... " ; \
           if test ! -f "$$pathname" ; \
           then \
              printf "Already deleted\n" ; \
           else \
              . ./CONFIG ; $$UTIL_RM -f "$$pathname" && printf "OK\n" ; \
           fi ; \
        done \
        <src/installed_files
	@. ./CONFIG ; $$UTIL_RM -f src/installed_files
	@printf "Uninstallation successfully completed.\n"

# Build documentation
documentation: cfg/.cfg_valid clean
	@printf "\n"
	@printf "Building documentation ...\n"
	cd src ; $(MAKE) $@

# Unicode joke to test terminal capabilities
love:
	@if test ! -f ./CONFIG ; \
        then \
           printf "Error: Configuration file not found\n" ; \
           exit 1 ; \
        fi
	-@. ./CONFIG ; \
        locale | $$UTIL_GREP LC_CTYPE \
           | $$UTIL_GREP -i "UTF-\{0,1\}8" >/dev/null ; \
        if test $$? -ne 0 ; \
        then \
           printf "Your locale doesn't allow this :-(\n" ; \
        else \
           printf "OK, lets try ... " ; \
           $$UTIL_SLEEP 1 ; \
           printf "\342\231\245" ; \
           $$UTIL_SLEEP 1 ; \
           printf "\342\235\244" ; \
           $$UTIL_SLEEP 1 ; \
           printf "\342\231\245\n" ; \
        fi


# ==============================================================================
# Sub rules

# Create new configuration
cfg/.cfg_valid: CONFIG mkconfig mksyslibs
	@printf "\n"
	@printf "Create configuration ...\n"
	. ./CONFIG ; CFG_ENABLE_SWITCH=1 ; export CFG_ENABLE_SWITCH ; \
           $$UTIL_SH ./mkconfig


# EOF
