# Makefile for AGrandTech EMS project

ProjectL = omc
ProjectU = OMC
PROJECT = $(ProjectL)
VERSION = 2.2403.1
RelDate = `date +%Y%m%d`
Release = $(RelDate)
RelVer = $(VERSION)-$(RelDate)
PLATFORM = amd64
ARMPLATFORM = aarch64
GitLocalRoot = $(HOME)/local.git
EmsBuildRoot=$(GitLocalRoot)/build.ems
BuildDir = $(EmsBuildRoot)/build
BuildOMCDir = $(BuildDir)/usr/local/omc
BuildOMCBinDir = $(BuildOMCDir)/bin
BuildOMCEtcDir = $(BuildOMCDir)/etc
BuildLibDir = $(BuildDir)/lib
BuildLibSystemDir = $(BuildLibDir)/systemd/system
DebBuildDir = $(EmsBuildRoot)/debbuild
DebFEBuildDir = $(EmsBuildRoot)/debbuild/usr/local/omc/htdocs
DebBuild2204Dir = $(EmsBuildRoot)/debbuild/22.04
DebFEBuild2204Dir = $(EmsBuildRoot)/debbuild/22.04/usr/local/omc/htdocs
RpmBuildDir = $(EmsBuildRoot)/rpmbuild
RpmFEBuildDir = $(RpmBuildDir)/BUILD/usr/local/omc/htdocs
OmcInstallDir = /usr/local/omc
ReleaseDir = $(EmsBuildRoot)/release
EmsBEDir = $(GitLocalRoot)/nms_nbi
ConfigEtcDir = $(EmsBEDir)/config/etc
ConfigSystemdDir = $(EmsBEDir)/config/systemd
EmsFEDir = $(GitLocalRoot)/fe.ems
EmsFEVue3Dir = $(GitLocalRoot)/fe.ems.vue3/dist
LibDir = $(EmsBEDir)/lib
RESTAGENT = restagent
CRONTASK = crontask
SshSvcBin = sshsvc
CapTraceBin = captrace
Data2htmlBin = data2html
NBI_ALARM = nbi_alarm
NBI_AGENT = nbi_agent
4A_AGENT = 4a_agent
RestagentDir = $(EmsBEDir)/$(RESTAGENT)
CrontaskDir = $(EmsBEDir)/$(CRONTASK)
SshSvcDir =  $(EmsBEDir)/$(SshSvcBin)
CapTraceDir =  $(EmsBEDir)/$(CapTraceBin)
Data2htmlDir =  $(EmsBEDir)/$(Data2htmlBin)
DBSQLSrcDir = $(EmsBEDir)/database
MiscDir = $(EmsBEDir)/misc
FrontBuildDir = $(BuildOMCDir)/htdocs
FrontSrcDir = $(EmsBEDir)/front
ReleaseDebs = $(ReleaseDir)/debs/$(PLATFORM)
CrontaskSize = 27788951
RestagentSize = 29525312
BinWriterDir = $(HOME)/bin
ifeq ($(shell grep VERSION_ID /etc/os-release), VERSION_ID="22.04")
DebBuildDir := $(DebBuild2204Dir)
DebFEBuildDir := $(DebFEBuild2204Dir)
#DebPkgFile := $(PROJECT)-$(VERSION)-22.04-$(Release).$(PLATFORM).deb
DebPkgFile := $(PROJECT)-r$(VERSION)-$(Release)-ub22.deb
else ifeq ($(shell grep VERSION_ID /etc/os-release), VERSION_ID="18.04")
DebPkgFile := $(PROJECT)-r$(VERSION)-$(Release)-ub18.deb
endif

.PHONY: all $(RESTAGENT) $(CRONTASK) $(SshSvcBin) $(CapTraceBin) $(Data2htmlBin)
all: $(RESTAGENT) $(CRONTASK) $(SshSvcBin) $(CapTraceBin) $(Data2htmlBin)
	cd $(RestagentDir)
	go build -o $(RESTAGENT) -v -ldflags "-X 'nms_nbi/lib/conifg.Version=$(RelVer)' \
		-X '$(LibDir)/conifg.BuildTime=`date`' \
		-X '$(LibDir)/conifg.GoVer=`go version`'"
	cd $(CrontaskDir)
	go build -o $(CRONTASK) -v -ldflags "-X '$(LibDir)/conifg.Version=$(RelVer)' \
		-X '$(LibDir)/conifg.BuildTime=`date`' \
		-X '$(LibDir)/conifg.GoVer=`go version`'"
	cd $(SshSvcDir)
	go build -o $(SshSvcBin) -v -ldflags "-X '$(LibDir)/conifg.Version=$(RelVer)' \
		-X '$(LibDir)/conifg.BuildTime=`date`' \
		-X '$(LibDir)/conifg.GoVer=`go version`'"
	cd $(CapTraceDir)
	go build -o $(CapTraceBin) -v -ldflags "-X '$(LibDir)/conifg.Version=$(RelVer)' \
		-X '$(LibDir)/conifg.BuildTime=`date`' \
		-X '$(LibDir)/conifg.GoVer=`go version`'"
		cd $(Data2htmlDir)
	go build -o $(Data2htmlBin) -v -ldflags "-X '$(LibDir)/conifg.Version=$(RelVer)' \
		-X '$(LibDir)/conifg.BuildTime=`date`' \
		-X '$(LibDir)/conifg.GoVer=`go version`'"	

clean:
	rm  ./restagent/$(RESTAGENT) ./crontask/$(CRONTASK) $(SshSvcDir)/$(SshSvcBin) $(CapTraceDir)/$(CapTraceBin) $(Data2htmlDir)/$(Data2htmlBin)

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)
	# clear build cache
	rm -rf $(FrontBuildDir)/front/*
	rm -rf $(DebFEBuildDir)/front/*
	rm -rf $(DebBuildDir)/usr/local/omc/etc/db/*

	cp -rf $(RestagentDir)/$(RESTAGENT) $(BuildOMCBinDir)
	cp -rf $(CrontaskDir)/$(CRONTASK) $(BuildOMCBinDir)
	cp -rf $(SshSvcDir)/$(SshSvcBin) $(BuildOMCBinDir)
	cp -rf $(CapTraceDir)/$(CapTraceBin) $(BuildOMCBinDir)
	cp -rf $(Data2htmlDir)/$(Data2htmlBin) $(BuildOMCBinDir)	
	cp -rf $(MiscDir)/* $(BuildOMCBinDir)
	cp -rf $(ConfigEtcDir)/* $(BuildOMCEtcDir)
	rm -rf $(BuildOMCEtcDir)/db/*
	cp -rf $(DBSQLSrcDir)/* $(BuildOMCEtcDir)/db/
	cp -rf $(ConfigSystemdDir)/* $(BuildLibSystemDir)
	cp -rf $(EmsFEVue3Dir)/* $(FrontBuildDir)/front >/dev/null
	chmod 755 $(BuildOMCBinDir)/*
	chmod 755 $(DebBuildDir)/DEBIAN/preinst	
	chmod 755 $(DebBuildDir)/DEBIAN/postinst
	chmod 755 $(DebBuildDir)/DEBIAN/postrm
	cp -rf $(BuildDir)/*  $(DebBuildDir)/
	#cp -rf $(BuildDir)/nginx/*  $(DebBuildDir)/etc/nginx/conf.d
	#cp -rf $(BuildDir)/systemd/*.service  $(DebBuildDir)/lib/systemd/system/
	chmod +x $(DebBuildDir)/usr/local/omc/bin/*
	#sed -i.bak 's/YYYYMMDD/$(shell date +"%Y%m%d")/g' $(DebBuildDir)/DEBIAN/control
	sed -i 's/YYYYMMDD/$(shell date +"%Y%m%d")/g' $(DebBuildDir)/DEBIAN/control
	perl -0777 -i -pe 's/ne:\n  user: root/ne:\n  user: agtuser/g' $(DebBuildDir)/usr/local/omc/etc/default/restconf.yaml
	dpkg -b $(DebBuildDir) $(ReleaseDebs)/$(DebPkgFile)

rpm: $(BINNAME)
	# clear build cache
	rm -rf $(FrontBuildDir)/front/*
	rm -rf $(RpmFEBuildDir)/front/*
	rm -rf $(RpmBuildDir)/BUILD/usr/local/omc/etc/db/*

	cp -rf $(RestagentDir)/$(RESTAGENT) $(BuildOMCBinDir)
	cp -rf $(CrontaskDir)/$(CRONTASK) $(BuildOMCBinDir)
	cp -rf $(SshSvcDir)/$(SshSvcBin) $(BuildOMCBinDir)
	cp -rf $(CapTraceDir)/$(CapTraceBin) $(BuildOMCBinDir)
	cp -rf $(Data2htmlDir)/$(Data2htmlBin) $(BuildOMCBinDir)		
#	$(BinWriterDir)/binWriter $(BuildOMCBinDir)/$(RESTAGENT) $(RestagentSize)
#	$(BinWriterDir)/binWriter $(BuildOMCBinDir)/$(CRONTASK) $(CrontaskSize)
#	cp -rf $(MiscDir)/ne-hosts $(BuildOMCBinDir)
#	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)/* $(BuildOMCBinDir)
#	rm -rf $(BinDir2)/ne-hosts
	cp -rf $(ConfigEtcDir)/* $(BuildOMCEtcDir)
	rm -rf $(BuildOMCEtcDir)/db/*
	cp -rf $(DBSQLSrcDir)/* $(BuildOMCEtcDir)/db/
	cp -rf $(ConfigSystemdDir)/* $(BuildLibSystemDir)		
	#unzip $(FrontSrcDir)/front.zip -d $(FrontBuildDir) >/dev/null
	mkdir -p $(FrontBuildDir)/front
	cp -rf $(EmsFEVue3Dir)/* $(FrontBuildDir)/front >/dev/null
	chmod 755 $(BuildOMCBinDir)/*
#	chmod 755 $(BinDir2)/*
	cp -rf $(BuildDir)/* $(RpmBuildDir)/BUILD/
	#cp -rf $(BuildDir)/nginx $(RpmBuildDir)/BUILD/etc/
	#cp -rf $(BuildDir)/systemd $(RpmBuildDir)/BUILD/lib/
	cd $(RpmBuildDir)
	rpmbuild -bb -D "_topdir $(RpmBuildDir)" $(RpmBuildDir)/SPECS/omc.spec
