# VNetLab project's top level Makefile
#
# Please compile the kernel module separately before calling the target
# 'install'.

# ** IMPORTANT **
# Be careful before you call the target 'install', it deletes some important
# directories and all the local changes might be lost. Unless you know what
# you are doing, please don't do that.
#

SRC_DIR=src
CONF_DIR=conf
BIN_DIR=bin
TOOLS_DIR=tools


HOME_VNETLAB=/home/vnetlab
HOME_VNETLAB_BIN=${HOME_VNETLAB}/bin
HOME_VNETLAB_CONF=${HOME_VNETLAB}/conf
HOME_VNETLAB_MANAGED=${HOME_VNETLAB}/managed

TOOLS_ASTYLE_DIR=${TOOLS_DIR}/astyle

ifeq ($(DEBUG),y)
FLAGS = DEBUG=y
endif


all: chaco vnet-manager group-admin group-manager host-manager kmt tools 

chaco:
	${MAKE} ${FLAGS} -C ${SRC_DIR}/allocator/graph_partition/code

#allocator:
	#${MAKE}  ${FLAGS} -C ${SRC_DIR}/allocator

group-manager:
	${MAKE} ${FLAGS} -C ${SRC_DIR}/server

host-manager:
	${MAKE} ${FLAGS} host-manager -C ${SRC_DIR}/hostMgr

vnet-manager:
	${MAKE} ${FLAGS} vnet-manager -C ${SRC_DIR}/userInterface

group-admin:
	${MAKE} ${FLAGS} group-admin -C ${SRC_DIR}/userInterface

kmt:
	${MAKE} ${FLAGS} KMTDIR=$(PWD)/src/kernelModule -C ${SRC_DIR}/kernelModule

tools:
	${MAKE} ${FLAGS} -C ${SRC_DIR}/kernelModule/tools

#vix-tool:
#	${MAKE} ${FLAGS} startAndWaitForTools -C ${SRC_DIR}/vix

format-code:
	${MAKE} -C ${TOOLS_ASTYLE_DIR}/buildgcc
	${TOOLS_ASTYLE_DIR}/bin/astyle --options="${CONF_DIR}/astyle/astyle.conf" \
	${SRC_DIR}/*.C ${SRC_DIR}/*.c ${SRC_DIR}/*.h
	${MAKE} clean -C ${TOOLS_ASTYLE_DIR}/buildgcc

clean: clean-chaco clean-allocator clean-group-manager clean-host-manager clean-vnet-manager clean-tools \
	clean-kmt clean-vix-tool clean-compiler clean-astyle
	rm -rf ${BIN_DIR}/* *~
clean-chaco:
	${MAKE} clean -C ${SRC_DIR}/allocator/graph_partition/code/

clean-allocator:
	${MAKE} clean -C ${SRC_DIR}/allocator/

clean-group-manager:
	${MAKE} clean -C ${SRC_DIR}/server

lclean-group-manager:
	${MAKE} lclean -C ${SRC_DIR}/server

clean-host-manager:
	${MAKE} clean -C ${SRC_DIR}/hostMgr

clean-vnet-manager:
	${MAKE} clean -C ${SRC_DIR}/userInterface

clean-kmt:
	${MAKE} KMTDIR=$(PWD)/src/kernelModule clean -C ${SRC_DIR}/kernelModule

clean-tools:
	${MAKE} clean -C ${SRC_DIR}/kernelModule/tools

clean-vix-tool:
	${MAKE} clean -C ${SRC_DIR}/vix

clean-compiler:
	${MAKE} clean -C ${SRC_DIR}/compiler/src

clean-astyle:
	${MAKE} clean -C ${TOOLS_ASTYLE_DIR}/buildgcc

install:
	# cleanup vnetlab dir structure
	rm -Rf ${HOME_VNETLAB_BIN}
	rm -Rf ${HOME_VNETLAB_CONF}
	rm -Rf ${HOME_VNETLAB_MANAGED}/common
	rm -Rf ${HOME_VNETLAB_MANAGED}/scripts
	rm -Rf ${HOME_VNETLAB_MANAGED}/teamNetworks
	rm -Rf ${HOME_VNETLAB_MANAGED}/userNetworks
	rm -Rf ${HOME_VNETLAB_MANAGED}/tools
	rm -Rf ${HOME_VNETLAB}

	# make vnetlab dir structure
	mkdir ${HOME_VNETLAB}
	mkdir ${HOME_VNETLAB_BIN}
	mkdir ${HOME_VNETLAB_CONF}
	mkdir ${HOME_VNETLAB_CONF}/group
	mkdir ${HOME_VNETLAB_CONF}/team
	mkdir ${HOME_VNETLAB_MANAGED}
	mkdir ${HOME_VNETLAB_MANAGED}/common
	mkdir ${HOME_VNETLAB_MANAGED}/scripts
	mkdir ${HOME_VNETLAB_MANAGED}/teamNetworks
	mkdir ${HOME_VNETLAB_MANAGED}/userNetworks
	mkdir ${HOME_VNETLAB_MANAGED}/vmSrc
	mkdir ${HOME_VNETLAB_MANAGED}/tools
	mkdir ${HOME_VNETLAB_MANAGED}/tools/obj

	#copy binaries and scripts
	cp -f ${BIN_DIR}/* ${HOME_VNETLAB_BIN}
	cp -f ${SRC_DIR}/kernelModule/kmt.ko ${HOME_VNETLAB_MANAGED}/tools/obj
	cp -f ${SRC_DIR}/kernelModule/reader ${HOME_VNETLAB_MANAGED}/tools/obj
	#cp -f ${SRC_DIR}/kernelModule/tools/vnctl ${HOME_VNETLAB_MANAGED}/tools
	cp -f ${SRC_DIR}/kernelModule/tools/vnconf.pl ${HOME_VNETLAB_MANAGED}/tools
	cp -f ${SRC_DIR}/kernelModule/tools/*.sh ${HOME_VNETLAB_MANAGED}/tools
	cp -f ${SRC_DIR}/scripts/*.sh ${HOME_VNETLAB_MANAGED}/scripts
	cp -f ${CONF_DIR}/hosts/* ${HOME_VNETLAB_MANAGED}/common/
	cp -f ${CONF_DIR}/*.txt ${HOME_VNETLAB_CONF}/group
