# ==============================================================================
# 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: bundledlibs config
	@printf "\n%s\n" "Building binary ..."
	(cd src ; $(MAKE) $@)

# Build configuration
config: cfg/.cfg_valid

# Build bundled libraries
bundledlibs:
	@printf "%s\n" "Building libbasexx ..."
	(cd subprojects/libbasexx ; $(MAKE) static)
	@printf "%s\n" "Building libjpiconv ..."
	(cd subprojects/libjpiconv ; $(MAKE) static)
	@printf "%s\n" "Building libssiconv ..."
	(cd subprojects/libssiconv ; $(MAKE) static)
	@printf "%s\n" "Building libuciconv ..."
	(cd subprojects/libuciconv ; \
            export CPPFLAGS="-I../../../../src/include" ; \
            export LDFLAGS="-L../../../../src/lib" ; $(MAKE) static)

# Run tests
test: config
	@printf "%s\n" "Testing libbasexx ..."
	(cd subprojects/libbasexx ; \
            PVERS=$$(command -p getconf _POSIX_VERSION) ; \
            export CPPFLAGS="-D_POSIX_C_SOURCE=$${PVERS}L -I../../../cheat" ; \
            $(MAKE) check)
	@printf "%s\n" "Testing libjpiconv ..."
	(cd subprojects/libjpiconv ; \
            PVERS=$$(command -p getconf _POSIX_VERSION) ; \
            export CPPFLAGS="-D_POSIX_C_SOURCE=$${PVERS}L -I../../../cheat" ; \
            $(MAKE) check)
	@printf "%s\n" "Testing libssiconv ..."
	(cd subprojects/libssiconv ; \
            PVERS=$$(command -p getconf _POSIX_VERSION) ; \
            export CPPFLAGS="-D_POSIX_C_SOURCE=$${PVERS}L -I../../../cheat" ; \
            $(MAKE) check)
	(cd subprojects/libuciconv ; \
            PVERS=$$(command -p getconf _POSIX_VERSION) ; \
            export CPPFLAGS="-D_POSIX_C_SOURCE=$${PVERS}L -I../../../cheat \
                             -I../../../../src/include" ; \
            export LDFLAGS="-L../../../../src/lib" ; $(MAKE) check)
	@printf "\n%s\n" "Running tests ..."
	(cd src ; $(MAKE) $@)

# Clean source tree
clean: config
	@printf "%s\n" "Cleaning libbasexx ..."
	(cd subprojects/libbasexx ; $(MAKE) $@)
	@printf "\n%s\n" "Cleaning libjpiconv ..."
	(cd subprojects/libjpiconv ; $(MAKE) $@)
	@printf "\n%s\n" "Cleaning libssiconv ..."
	(cd subprojects/libssiconv ; $(MAKE) $@)
	@printf "\n%s\n" "Cleaning libuciconv ..."
	(cd subprojects/libuciconv ; $(MAKE) $@)
	@printf "\n%s\n" "Cleaning source tree ..."
	(cd src ; $(MAKE) $@)

# Clean source tree and configuration
distclean: clean
	@printf "%s\n" "Deleting configuration ..."
	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 "%s\n" "Deleting list with installed files ..."
	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 bundledlibs
	@printf "%s\n" "Install files to system ..."
	(cd src ; $(MAKE) $@)

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

# Uninstall files
uninstall:
	@printf "%s\n" "Uninstall files from system ..."
	@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 "%s\n" "Building libbasexx documentation ..."
	(cd subprojects/libbasexx ; $(MAKE) $@)
	@printf "%s\n" "Building libjpiconv documentation ..."
	(cd subprojects/libjpiconv ; $(MAKE) $@)
	@printf "%s\n" "Building libssiconv documentation ..."
	(cd subprojects/libssiconv ; $(MAKE) $@)
	@printf "%s\n" "Building libuciconv documentation ..."
	(cd subprojects/libuciconv ; $(MAKE) $@)
	@printf "\n%s\n" "Building documentation ..."
	(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 "%s\n" "Create configuration ..."
	. ./CONFIG ; CFG_ENABLE_SWITCH=1 ; export CFG_ENABLE_SWITCH ; \
           $$UTIL_SH ./mkconfig


# EOF
