################################################################################
# File: Makefile                                                               #
################################################################################
# Makefile for the socketcc library.  This will first make all the object      #
# files before linking to create the final library file.  There is also the    #
# option of installing and removing the library to /usr/lib and /usr/include.  #
# Object files are made in the obj sub-directory.                              #
################################################################################
# Written By: Jason But                                                        #
# Copyright:  CTIE - Monash University 2001                                    #
#                                                                              #
# Notes:                                                                       #
#    Version 1.00 - Original Version of module.                                #
################################################################################

################################################################################
# Module compile information.                                                  #
################################################################################
#     SRC_DIR      - Directory containing source and header files.             #
#     OBJ_DIR      - Directory containing obj files and associated Makefile.   #
#     LIB_NAME     - Name of library as used for linking by other applications.#
#     LIB_SONAME   - Name of library as used for dynamic linking (soname).     #
#     LIB_TARGET   - Final target filename of library.                         #
#     LIB_HEADERS  - Header files from SRC_DIR to be copied to /usr/include.   #
################################################################################
SRC_DIR = src
OBJ_DIR = obj
LIB_NAME = libsocketcc.so
LIB_SONAME = $(LIB_NAME).1
LIB_TARGET = $(LIB_SONAME).0
LIB_HEADERS = socketcc.h

################################################################################
# Linker and associated flags.                                                 #
################################################################################
LD = gcc -lpthreadcc -shared -Wl,-soname,$(LIB_SONAME)

################################################################################
# Makeup of introduction message.                                              #
################################################################################
INFO_LINE =  "=======================================================\n"
INFO_TITLE = "  socketcc : C++ Class based socket object libraries.\n"
INFO_VER = " Version      : 1.0\n"
INFO_DEV = " Developed By : CTIE - Monash University\n"

INFO_PROG = $(INFO_LINE)$(INFO_TITLE)$(INFO_LINE)$(INFO_VER)$(INFO_DEV)$(INFO_LINE)

################################################################################
# Descriptions of certain make tasks.                                          #
################################################################################
DESC_ALL =         "Compile and make $(LIB_TARGET).\n"
DESC_APPLICATION = $(DESC_ALL)
DESC_OBJECTS =     "Compile all object files in $(OBJ_DIR) sub-directory.\n"
DESC_INSTALL =     "Install $(LIB_NAME) to /usr/lib and $(LIB_HEADERS) to /usr/include\n"
DESC_REMOVE =      "Remove $(LIB_NAME) from /usr/lib and $(LIB_HEADERS) from /usr/include\n"
DESC_CLEAN =       "Remove any files created for a clean install.\n"

################################################################################
# ALL                                                                          #
# LIBRARY                                                                      #
################################################################################
# make all and make library perform the same task of resolving to making the   #
# target libary file.                                                          #
################################################################################
all:                library
	@echo Run \'make install\' to use this library with other projects...

library:            pre-compile-info $(LIB_TARGET)

################################################################################
# make pre-compile-info echos pre-compilation information to the screen.       #
################################################################################
pre-compile-info:
	@echo -e $(INFO_PROG)

################################################################################
# make LIB_TARGET depends on the on all the substituent object files.  These   #
# are linked to create the final target.                                       #
################################################################################
$(LIB_TARGET):      objects
	@(                                                                          \
        echo Linking Libary...;                                                 \
        echo -e "\tLinking: Object files in $(OBJ_DIR) --> $(LIB_TARGET)";      \
        $(LD) -o $(LIB_TARGET) $(OBJ_DIR)/*.o;                                  \
        echo Library Linked...;                                                 \
        echo                                                                    \
    )

################################################################################
# OBJECTS                                                                      #
################################################################################
# make objects makes all the object files in the object sub-directory.  This   #
# is done via a recursive make call in that directory.                         #
################################################################################
objects:
	@$(MAKE) -s -C $(OBJ_DIR);

################################################################################
# DEPEND                                                                       #
################################################################################
# make dep, fastdepend and depend does make depend in the obj sub-directory.   #
################################################################################
dep fastdep depend:
	@$(MAKE) -s -C $(OBJ_DIR) depend;

################################################################################
# INSTALL                                                                      #
################################################################################
# Installs the compiled library to the system, depends on the library being    #
# compiled.  The target file is copied to the /usr/lib directory, we run       #
# ldconfig to ensure that the dynamic link is created for the give soname.     #
# We also create a symbolic link to the library name for compilation/linking   #
# purposes, finally, the header files must be copied to /usr/include.          #
################################################################################
install:            library
	@(                                                                          \
        echo Installing Libary...;                                              \
        echo -e "\tCopying $(LIB_TARGET) to /usr/lib...";                       \
        cp $(LIB_TARGET) /usr/lib;                                              \
        pushd /usr/lib > /dev/null;                                             \
        echo -e "\tCreating $(LIB_SONAME) for soname dynamic linking...";       \
        /sbin/ldconfig;                                                         \
        echo -e "\tCreating $(LIB_NAME) for linking purposes...";               \
        ln -s $(LIB_SONAME) $(LIB_NAME);                                        \
        popd > /dev/null;                                                       \
        echo -e "\tInstalling header files $(LIB_HEADERS) to /usr/include...";	\
        cp $(SRC_DIR)/$(LIB_HEADERS) /usr/include;                              \
        echo Install Complete...;                                               \
        echo                                                                    \
    )

################################################################################
# REMOVE                                                                       #
################################################################################
# Removes the compiled library from the system, deletes the library file two   #
# symbolic links from the /usr/lib directory and the header files from the     #
# /usr/include directory.                                                      #
################################################################################
remove:             pre-compile-info
	@(                                                                          \
        echo Removing Libary...;                                                \
        echo -e "\tRemoving $(LIB_TARGET) and symbolic links from /usr/lib..."; \
        rm -f /usr/lib/$(LIB_TARGET);                                           \
        rm -f /usr/lib/$(LIB_SONAME);                                           \
        rm -f /usr/lib/$(LIB_NAME);                                             \
        echo -e "\tRemoving header files $(LIB_HEADERS) from /usr/include...";  \
        rm -f /usr/include/$(LIB_HEADERS);                                      \
        echo Remove Complete...;                                                \
        echo                                                                    \
    )

################################################################################
# CLEAN                                                                        #
# MRPROPER                                                                     #
################################################################################
# Remove any files created for a clean install.                                #
################################################################################
clean mrproper::    pre-compile-info
	@(                                                                          \
        echo Removing $(LIB_TARGET)...;                                         \
        rm -f $(LIB_TARGET);                                                    \
        $(MAKE) -s -C $(OBJ_DIR) clean                                          \
    )

################################################################################
# HELP                                                                         #
################################################################################
# Help messages.                                                               #
################################################################################
HLP_HELP =    "help\tthis screen.\n"
HLP_ALL =     "all\t"$(DESC_ALL)
HLP_LIBRARY = "library\t"$(DESC_APPLICATION)
HLP_OBJECTS = "objects\t"$(DESC_OBJECTS)
HLP_INSTALL = "install\t"$(DESC_INSTALL)
HLP_REMOVE =  "remove\t"$(DESC_REMOVE)
HLP_CLEAN =   "clean\t"$(DESC_CLEAN)

HLP_COMMANDS = $(HLP_HELP)$(HLP_ALL)$(HLP_LIBRARY)$(HLP_OBJECTS)$(HLP_INSTALL)$(HLP_REMOVE)$(HLP_CLEAN)

################################################################################
# make help resolves to echoing available make options.                        #
################################################################################
help:               pre-compile-info
	@echo Makefile Help
	@echo
	@echo Usage: make [help all library objects install remove clean]
	@echo
	@echo -e $(HLP_COMMANDS)

################################################################################
# End of File: Makefile                                                        #
################################################################################
