# makefile for build package 

build_root=$(shell pwd)
project=ems
# os platform
platform=ub22
build_dir=$(build_root)/build
release_dir=$(build_root)/release
extra_dir=$(build_root)/extra
RelDate=$(shell date "+%Y%m%d")

package_name=$(project)-$(version)-$(RelDate)-$(platform)
package_dir=$(package_name)
package_path=$(build_dir)/$(package_name)
svc_root=$(build_root)/../svc.ems
svc_bin=$(svc_root)/bin
web_root=$(build_root)/../web.ems
package_file=$(package_name).tgz

current_date=$(shell date "+%Y-%m-%d %H:%M:%S")
vfile=$(extra_dir)/emsVersion
# get version from emsVersion
version=$(shell grep "Version:" $(vfile) | awk -F' ' '{print $$2}')

src_log=make_src.log
plat_log=make_plat.log

print-version:
	@echo "Version: $(version)"
	@echo "Release Date: $(current_date)"

src_all:
	@echo -n "make all src programs ..."
	@$(MAKE) -C $(svc_root)/src 1>$(src_log) 2>&1
	@echo "refer to log $(src_log)"

plat_all:
	@echo -n "make all plat modules ..."	
	@$(MAKE) -C $(svc_root)/plat 1>$(plat_log) 2>&1
	@echo "refer to log $(plat_log)"

src_clean:
	$(MAKE) -C $(svc_root)/src src_clean

plat_clean:
	$(MAKE) -C $(svc_root)/plat clean

bin: plat_all src_all

clean: plat_clean src_clean 

package: bin
	@echo -n "prepare build environment ... "
	@rm -rf $(package_path)
	@mkdir -p $(package_path)
	@echo "done"
	@echo -n "copy bin to build directory ... "
	@cp -rf $(svc_bin) $(package_path)
	@echo "done"
	@echo -n "update release date ... "
	@sed -i "s/Release Date:.*/Release Date: $(current_date)/" $(vfile)
	@echo "done"
	@echo -n "copy extra file to build directory ... "
	@cp -rf $(extra_dir)/* $(package_path)
	@echo "done"
	@echo -n "copy web file to build directory ... "
	@mkdir -p $(package_path)/web
	@cp -rf $(web_root)/* $(package_path)/web
	@echo "done"
	@echo -n "build package $(package_file) ... "
	@tar cvfz $(build_dir)/$(package_file) -C $(build_dir) $(package_name) >/dev/null
	@echo "done"
	@echo -n "move $(package_file) to $(release_dir) ... "
	@mv -f $(build_dir)/$(package_file) $(release_dir)
	@echo "done"
	@echo -n "clear temp build directory ... " 
	@rm -rf $(package_path)
	@echo "done"