# ########################################################################
#
#                           Makefile for libY2
#
#   Rules:
#
#       all     -- builds program.
#       install -- install program.
#       clean   -- remove object and other work files.
#
#       If no arguments are givin, the program is built by default.
#

# ########################################################################
# Install Options:
#
PREFIX = /usr


# ########################################################################
# Library Name and Version:
#
#   Note, do not change the *major* version number. That would imply that
#   the entire Y protocol has changed.
#
LIBPFX = libY2
LIBVER = 14


# ########################################################################
# Compiler Flags:
#   
#   These are definations to enable or disable certain compile time
#   options. Omitting a defination turns that option off.
#
#   Each argument is of the format -D<option> where <option> is
#   one of the following:
#
#	* None available *
#
#   Other arguments include:
#
#       -O#                     Specifies the optimization level the
#                               compiler is to compile at. This (attempts)
#                               to improve the efficiency of the generated
#                               program when it runs. Available values for
#                               # are from 0 to 2 (some compilers allow
#                               higher values). When in doubt, set # to 2.
#
#       -g                      Compile with debugging information,
#                               this is useful for determining why this
#                               program (if it did) crash. However this
#                               may hinder performance, so don't use
#                               this option unless you are attempting
#                               to debug the program.
#
#	-shared			Compile this program as a shared library.
#

CFLAGS = -Wall -O2 -g
#CFLAGS = -Wall -O6 -fomit-frame-pointer -funroll-loops \
#         -finline-functions -ffast-math -march=i586

CPPFLAGS = -D__cplusplus -Dc_plusplus


# ########################################################################
# Dependant Libraries:
#
#   These are dynamic (sometimes called shared) libraries that this
#   program is to be `linked' to.
#
#   Each argument is of the format -l<name> where <name> is the name
#   of the library. You may have to add one or more -l<name> arguments
#   to the LIB line depending on what you have set in the CFLAGS line
#   farther above.
#       
LIBS = -lm

# Library Directories:
#
#   All libraries are looked for in the directories specified below.
# 
#   Each argument is of the format -L<dir> where <dir> is the full
#   path to the directory.
#
LIB_DIRS =

# Header File Directories:
#
#   Required header files that are not in the standard locations are
#   searched for in the directories specified below.
#
#   Each argument is of the format -I<dir> where <dir> is the full
#   path to the directory.
#
INC =
INC_DIRS =


# ########################################################################
# Program Source and Header Files:
#
include Makefile.srclist

CC = cc
CPP = c++

# ########################################################################
# Utilities:
#
LS      = ls
LSFLAGS = -a -l

RM      = rm
RMFLAGS = -f

COPY      = cp
COPYFLAGS = -i -v

MKDIR      = mkdir
MKDIRFLAGS = -p

LINK      = ln
LINKFLAGS = -sf

LDCONFIG = ldconfig


# ########################################################################
# Build Rules:
#
LIB     = $(LIBPFX).so.$(LIBVER)
OBJ_C   = $(SRC_C:.c=.o)
OBJ_CPP = $(SRC_CPP:.cpp=.o)
.c.o:
	+$(CC) -c $*.c $(INC_DIRS) $(CFLAGS)
.cpp.o:
	+$(CPP) -c $*.cpp $(INC_DIRS) $(CFLAGS) $(CPPFLAGS)

$(LIB): $(OBJ_C) $(OBJ_CPP)
	$(CC) $(OBJ_C) $(OBJ_CPP) -Wl,-soname=$(LIB) -shared -o $(LIB) $(LIBS) $(LIB_DIRS)
	$(LINK) $(LINKFLAGS) $(LIB) $(LIBPFX).so

all: $(LIB)

# ########################################################################
# Install Rules:
#

INSTALL      = install
INSTBINFLAGS = -m 0755 -s
INSTUIDFLAGS = -m 4755
INSTLIBFLAGS = -m 0755 -s
INSTINCFLAGS = -m 0644
INSTMANFLAGS = -m 0644
INSTCFGFLAGS = -m 0644
INSTDATFLAGS = -m 0644

YLIB_DIR = $(PREFIX)/lib
YINC_DIR = $(PREFIX)/include/Y2
YMAN_DIR = $(PREFIX)/man/man3


install_lib:
	$(MKDIR) $(MKDIRFLAGS) $(YLIB_DIR)
	$(INSTALL) $(INSTLIBFLAGS) $(LIB) $(YLIB_DIR)
	$(LINK) $(LINKFLAGS) $(LIB) $(YLIB_DIR)/$(LIBPFX).so

install:
	$(MKDIR) $(MKDIRFLAGS) $(YLIB_DIR)
	$(INSTALL) $(INSTLIBFLAGS) $(LIB) $(YLIB_DIR)
	$(LINK) $(LINKFLAGS) $(LIB) $(YLIB_DIR)/$(LIBPFX).so

	$(MKDIR) $(MKDIRFLAGS) $(YINC_DIR)
	$(INSTALL) $(INSTINCFLAGS) ../include/Y2/Y.h $(YINC_DIR)
	$(INSTALL) $(INSTINCFLAGS) ../include/Y2/Ylib.h $(YINC_DIR)
	$(INSTALL) $(INSTINCFLAGS) ../include/Y2/Yclientnet.h $(YINC_DIR)
	$(INSTALL) $(INSTINCFLAGS) ../include/Y2/Ymixercodes.h $(YINC_DIR)

	@$(RM) $(RMFLAGS) $(YMAN_DIR)/YAddHost.3
	@$(RM) $(RMFLAGS) $(YMAN_DIR)/YAudioCDTrackStruct.3
	@$(RM) $(RMFLAGS) $(YMAN_DIR)/YCalculateCycle.3
	@$(RM) $(RMFLAGS) $(YMAN_DIR)/YChangeAudioModePreset.3
	@$(RM) $(RMFLAGS) $(YMAN_DIR)/YCloseConnection.3
	@$(RM) $(RMFLAGS) $(YMAN_DIR)/YDestroyPlaySoundObject.3
	@$(RM) $(RMFLAGS) $(YMAN_DIR)/YEjectAudioCD.3
	@$(RM) $(RMFLAGS) $(YMAN_DIR)/YEvent.3
	@$(RM) $(RMFLAGS) $(YMAN_DIR)/YEventAudioCDStats.3
	@$(RM) $(RMFLAGS) $(YMAN_DIR)/YEventAudioChange.3
	@$(RM) $(RMFLAGS) $(YMAN_DIR)/YEventAudioStats.3
	@$(RM) $(RMFLAGS) $(YMAN_DIR)/YEventClientMessage.3
	@$(RM) $(RMFLAGS) $(YMAN_DIR)/YEventCycleChange.3
	@$(RM) $(RMFLAGS) $(YMAN_DIR)/YEventDisconnect.3
	@$(RM) $(RMFLAGS) $(YMAN_DIR)/YEventHost.3
	@$(RM) $(RMFLAGS) $(YMAN_DIR)/YEventMixer.3
	@$(RM) $(RMFLAGS) $(YMAN_DIR)/YEventServerStats.3
	@$(RM) $(RMFLAGS) $(YMAN_DIR)/YEventShutdown.3
	@$(RM) $(RMFLAGS) $(YMAN_DIR)/YEventSoundKill.3
	@$(RM) $(RMFLAGS) $(YMAN_DIR)/YEventSoundObjectAttributes.3
	@$(RM) $(RMFLAGS) $(YMAN_DIR)/YEventSoundPlay.3
	@$(RM) $(RMFLAGS) $(YMAN_DIR)/YEventSync.3
	@$(RM) $(RMFLAGS) $(YMAN_DIR)/YEventYSHMSound.3
	@$(RM) $(RMFLAGS) $(YMAN_DIR)/YFreeAudioCDTracksList.3
	@$(RM) $(RMFLAGS) $(YMAN_DIR)/YFreeAudioModesList.3
	@$(RM) $(RMFLAGS) $(YMAN_DIR)/YGetAudioCDTracks.3
	@$(RM) $(RMFLAGS) $(YMAN_DIR)/YGetAudioModes.3
	@$(RM) $(RMFLAGS) $(YMAN_DIR)/YGetAudioStats.3
	@$(RM) $(RMFLAGS) $(YMAN_DIR)/YGetMixerChannel.3
	@$(RM) $(RMFLAGS) $(YMAN_DIR)/YGetMixerChannelQuad.3
	@$(RM) $(RMFLAGS) $(YMAN_DIR)/YGetNextEvent.3
	@$(RM) $(RMFLAGS) $(YMAN_DIR)/YGetPlaySoundObjectValues.3
	@$(RM) $(RMFLAGS) $(YMAN_DIR)/YGetServerStats.3
	@$(RM) $(RMFLAGS) $(YMAN_DIR)/YGetSoundObjectAttributes.3
	@$(RM) $(RMFLAGS) $(YMAN_DIR)/YIPUnion.3
	@$(RM) $(RMFLAGS) $(YMAN_DIR)/YIntro.3
	@$(RM) $(RMFLAGS) $(YMAN_DIR)/YMatchAudioModeValues.3
	@$(RM) $(RMFLAGS) $(YMAN_DIR)/YOpenConnection.3
	@$(RM) $(RMFLAGS) $(YMAN_DIR)/YPlayAudioCDTrack.3
	@$(RM) $(RMFLAGS) $(YMAN_DIR)/YPutBackEvent.3
	@$(RM) $(RMFLAGS) $(YMAN_DIR)/YRemoveHost.3
	@$(RM) $(RMFLAGS) $(YMAN_DIR)/YSHMCloseSound.3
	@$(RM) $(RMFLAGS) $(YMAN_DIR)/YSHMOpenSound.3
	@$(RM) $(RMFLAGS) $(YMAN_DIR)/YSendClientMessage.3
	@$(RM) $(RMFLAGS) $(YMAN_DIR)/YSetAudioModeValues.3
	@$(RM) $(RMFLAGS) $(YMAN_DIR)/YSetCycle.3
	@$(RM) $(RMFLAGS) $(YMAN_DIR)/YSetMixerChannel.3
	@$(RM) $(RMFLAGS) $(YMAN_DIR)/YSetMixerChannelQuad.3
	@$(RM) $(RMFLAGS) $(YMAN_DIR)/YSetPlaySoundObjectValues.3
	@$(RM) $(RMFLAGS) $(YMAN_DIR)/YShutdownServer.3
	@$(RM) $(RMFLAGS) $(YMAN_DIR)/YStartPlaySoundObject.3
	@$(RM) $(RMFLAGS) $(YMAN_DIR)/YStartPlaySoundObjectSimple.3
	@$(RM) $(RMFLAGS) $(YMAN_DIR)/YStopAudioCD.3
	@$(RM) $(RMFLAGS) $(YMAN_DIR)/YSyncAll.3
	@$(RM) $(RMFLAGS) $(YMAN_DIR)/YVolumeStruct.3

	$(MKDIR) $(MKDIRFLAGS) $(YMAN_DIR)
	$(INSTALL) $(INSTMANFLAGS) man/* $(YMAN_DIR)

	-$(LDCONFIG)

	@echo "-------------------------------------------------------------------------"
	@echo "Library $(LIB) installed in:"
	@echo " "
	@echo "        $(YLIB_DIR)"
	@echo " "
	@echo "Header files are installed in:"
	@echo " "
	@echo "        $(YINC_DIR)"
	@echo " "
	@echo "Manual pages are installed in:"
	@echo " "
	@echo "        $(YMAN_DIR)"
	@echo " "
	@echo "To link your programs to this library add -lY2 to the compiler"
	@echo "command."
	@echo " "
	@echo "You may also need to recompile some programs which are linked"
	@echo "to this program in case of protocol value changes. Modification"
	@echo "to their code is unnessasary if the major version number matches."
	@echo " "


# ########################################################################
# Maintainance and Misc Rules:
#
clean:
	$(RM) $(RMFLAGS) a.out core *.o $(LIB) $(LIBPFX).so


# ########################################################################
