# Makefile for AGrandTech EMS project

PROJECT = OMC
VERSION = 5GC1.0.0
RelDate = `date +%Y%m%d`
PLATFORM = amd64
ARMPLATFORM = aarch64
GitLocalRoot = $(HOME)/local.git
EmsBuildRoot=$(GitLocalRoot)/build.ems
BuildDir = $(EmsBuildRoot)/build
DebBuildDir = $(EmsBuildRoot)/debbuild
RpmBuildDir = $(EmsBuildRoot)/rpmbuild
OmcInstallDir = /usr/local/omc
ReleaseDir = $(EmsBuildRoot)/release
EmsBEDir = $(GitLocalRoot)/ems.agt
ConfigEtcDir = $(EmsBEDir)/config/etc
EmsFEDir = $(GitLocalRoot)/fe.ems
LibDir = $(EmsBEDir)/lib
RESTAGENT = restagent
CRONTASK = crontask
SshSvcBin = sshsvc
NBI_ALARM = nbi_alarm
NBI_AGENT = nbi_agent
4A_AGENT = 4a_agent
RestagentDir = $(EmsBEDir)/$(RESTAGENT)
CrontaskDir = $(EmsBEDir)/$(CRONTASK)
SshSvcDir =  $(EmsBEDir)/$(SshSvcBin)
DBSQLSrcDir = $(EmsBEDir)/database
MiscDir = $(EmsBEDir)/misc
FrontBuildDir = $(BuildDir)/omc/htdocs
FrontSrcDir = $(EmsBEDir)/front
ReleaseDebs = $(ReleaseDir)/debs/$(PLATFORM)
BinDir = $(BuildDir)/omc/bin
BinDir2 = $(BuildDir)/omc/run
CrontaskSize = 27788951
RestagentSize = 29525312
BinWriterDir = $(HOME)/bin

.PHONY: all $(RESTAGENT) $(CRONTASK)
all: $(RESTAGENT) $(CRONTASK)
	cd $(RestagentDir)
	go build -o $(RESTAGENT) -v -ldflags "-X 'ems.agt/lib/conifg.Version=$(VERSION)' \
		-X '$(LibDir)/conifg.BuildTime=`date`' \
		-X '$(LibDir)/conifg.GoVer=`go version`'"
	cd $(CrontaskDir)
	go build -o $(CRONTASK) -v -ldflags "-X '$(LibDir)/conifg.Version=$(VERSION)' \
		-X '$(LibDir)/conifg.BuildTime=`date`' \
		-X '$(LibDir)/conifg.GoVer=`go version`'"


clean:
	rm  ./restagent/$(RESTAGENT) ./crontask/$(CRONTASK)

dist: 
	tar -zvcPf $(ReleaseDir)/$(PROJECT)-src-$(VERSION).tar.gz \
		../lib \
		../restagent \
		../crontask \
		../initems \
		../database \
		../docs \
		../misc \
		../config \
		--exclude=../restagent/restagent \
		--exclude=../crontask/crontask \
		--exclude=../initems/initems 

deb: $(BINNAME)
	cp -rf ./restagent/$(RESTAGENT) $(BinDir)
	cp -rf ./crontask/$(CRONTASK) $(BinDir)
	cp -rf $(SshSvcDir)/$(SshSvcBin) $(BinDir)
	cp -rf $(MiscDir)/* $(BinDir)
	cp -rf $(ConfigEtcDir)/* $(BuildDir)/omc/etc/
	cp -rf $(DBSQLSrcDir)/* $(BuildDir)/omc/etc/db/
	rm -rf $(FrontBuildDir)/*
	mkdir -p $(FrontBuildDir)/front
	cp -rf $(EmsFEDir)/* $(FrontBuildDir)/front >/dev/null
	chmod 755 $(BinDir)/*
	chmod 755 $(BuildDir)/omc/bin/*
	chmod 755 $(DebBuildDir)/DEBIAN/*
	cp -rf $(BuildDir)/omc/*  $(DebBuildDir)/usr/local/omc/
	chmod +x $(DebBuildDir)/usr/local/omc/bin/*
	cp -rf $(BuildDir)/nginx/*  $(DebBuildDir)/etc/nginx/conf.d
	cp -rf $(BuildDir)/systemd/*.service  $(DebBuildDir)/lib/systemd/system/
	dpkg -b $(DebBuildDir) $(ReleaseDebs)/$(PROJECT)-$(VERSION).$(RelDate).$(PLATFORM).deb

rpm: $(BINNAME)
	cp -rf ./restagent/$(RESTAGENT) $(BinDir)
	cp -rf ./crontask/$(CRONTASK) $(BinDir)
	cp -rf $(SshSvcDir)/$(SshSvcBin) $(BinDir)
#	$(BinWriterDir)/binWriter $(BinDir)/$(RESTAGENT) $(RestagentSize)
#	$(BinWriterDir)/binWriter $(BinDir)/$(CRONTASK) $(CrontaskSize)
#	cp -rf $(MiscDir)/ne-hosts $(BinDir)
#	cp -rf ./nbi/$(NBI_ALARM)/bin/$(NBI_ALARM) $(BinDir2)
#	cp -rf ./nbi/$(NBI_AGENT)/bin/$(NBI_AGENT) $(BinDir2)
#	cp -rf ./$(4A_AGENT)/bin/$(4A_AGENT) $(BinDir2)
	cp -rf $(MiscDir)/* $(BinDir)
#	rm -rf $(BinDir2)/ne-hosts
	cp -rf $(ConfigEtcDir)/* $(BuildDir)/omc/etc/
	cp -rf $(DBSQLSrcDir)/* $(BuildDir)/omc/etc/db/
	rm -rf $(FrontBuildDir)/*
	#unzip $(FrontSrcDir)/front.zip -d $(FrontBuildDir) >/dev/null
	mkdir -p $(FrontBuildDir)/front
	cp -rf $(EmsFEDir)/* $(FrontBuildDir)/front >/dev/null
	chmod 755 $(BinDir)/*
#	chmod 755 $(BinDir2)/*
	cp -rf $(BuildDir)/omc $(RpmBuildDir)/BUILD
	cp -rf $(BuildDir)/nginx $(RpmBuildDir)/BUILD
	cp -rf $(BuildDir)/systemd $(RpmBuildDir)/BUILD
	cd $(RpmBuildDir)
	rpmbuild -bb -D "_topdir $(RpmBuildDir)" $(RpmBuildDir)/SPECS/omc.spec
