
AUTOMAKE_OPTIONS = foreign no-dependencies no-installinfo

SUFFIXES = .go
OBJEXT= none

sbin_PROGRAMS = minilbadm minilbd 
bin_PROGRAMS = minilbctl 

BUILD = $(shell date -u '+%Y-%m-%d-%H%M')
GOFLAGS = -v -ldflags='-s -w'

minilbadm_SOURCES = \
	cmd/minilbadm/main.go \
	cmd/minilbadm/tool.go

EXTRA_minilbadm_SOURCES = \
	cmd/minilbadm/hash/hashcmd.go

minilbadm$(EXEEXT): $(minilbadm_SOURCES) $(EXTRA_minilbadm_SOURCES) $(EXTRA_minilbd_SOURCES)
	env CGO_ENABLED=0 $(GO) build $(GOFLAGS) -o minilbadm$(EXEEXT) $(minilbadm_SOURCES)

minilbctl_SOURCES = \
	cmd/minilbctl/tool.go \
	cmd/minilbctl/main.go

EXTRA_minilbctl_SOURCES = \
	cmd/minilbctl/service/srvcmd.go \
	cmd/minilbctl/forwarder/listforw.go \
	cmd/minilbctl/forwarder/deleteforw.go \
	cmd/minilbctl/forwarder/createforw.go \
	cmd/minilbctl/forwarder/forwcmd.go


minilbctl$(EXEEXT): $(minilbctl_SOURCES) $(EXTRA_minilbctl_SOURCES)  $(EXTRA_minilbd_SOURCES)
	env CGO_ENABLED=0 $(GO) build $(GOFLAGS) -o minilbctl$(EXEEXT) $(minilbctl_SOURCES)

minilbd_SOURCES = \
	cmd/minilbd/main.go

EXTRA_minilbd_SOURCES = \
	cmd/minilbd/starter/starter.go

minilbd$(EXEEXT): $(minilbd_SOURCES) $(EXTRA_minilbd_SOURCES)
	env CGO_ENABLED=0 $(GO) build $(GOFLAGS) -o minilbd$(EXEEXT) $(minilbd_SOURCES)

EXTRA_minilbd_SOURCES += \
	app/config/config.go \
	app/handler/handler.go \
	app/handler/hello.go \
	app/operator/auth.go \
	app/operator/hello.go \
	app/operator/oper.go \
	app/server/server.go \
	app/logger/logger.go \
	pkg/passwd/passwd.go \
	pkg/x509crt/x509cert.go \
	pkg/client/auth.go \
	pkg/client/client.go \
	pkg/client/refer.go \
	app/service/service.go \
	pkg/mlbctl/mlbctl_grpc.pb.go \
	pkg/mlbctl/mlbctl.pb.go

EXTRA_DIST = \
	debian/compat \
	debian/.gitignore \
	debian/minilb-control.install \
	debian/minilb-control.postinst \
	debian/minilb-control.postrm \
	debian/minilb-control.prerm \
	debian/minilb-service.install \
	debian/minilb-service.postinst \
	debian/minilb-service.postrm \
	debian/minilb-service.prerm \
	debian/patches/series \
	debian/README.Debian \
	debian/rules \
	debian/source/format \
	debian/watch \
	etc/minilb/minilbd.yaml \
	.gitignore \
	go.mod \
	go.sum \
	proto/mlbctl.proto \
	README.md \
	vendor/*


FREEBSD_LOCALBASE = /usr/local
FREEBSD_RCDIR = $(FREEBSD_LOCALBASE)/etc/rc.d
LINUX_SYSTEMDDIR = /lib/systemd/system

install-data-local:
	test -z $(DESTDIR)$(srv_confdir) || $(MKDIR_P) $(DESTDIR)$(srv_confdir)
	test -z $(DESTDIR)$(srv_logdir) || $(MKDIR_P) $(DESTDIR)$(srv_logdir)
	test -z $(DESTDIR)$(srv_rundir) || $(MKDIR_P) $(DESTDIR)$(srv_rundir)
	test -z $(DESTDIR)$(SYSTEMD_LIBDIR) || $(MKDIR_P) $(DESTDIR)$(SYSTEMD_LIBDIR)
if FREEBSD_OS
	test -z $(DESTDIR)$(FREEBSD_RCDIR) || $(MKDIR_P) $(DESTDIR)$(FREEBSD_RCDIR)
	$(INSTALL_DATA) initrc/minilbd $(DESTDIR)$(FREEBSD_RCDIR)
	chmod a+x $(DESTDIR)$(FREEBSD_RCDIR)/minilbd
endif
if LINUX_OS
if SYSTEMD
	test -z $(DESTDIR)$(LINUX_SYSTEMDDIR) || $(MKDIR_P) $(DESTDIR)$(LINUX_SYSTEMDDIR)
	$(INSTALL_DATA) initrc/minilbd.service $(DESTDIR)$(LINUX_SYSTEMDDIR)
endif
endif



GENDIR=pkg/mlbctl
grpc:
	mkdir -p $(GENDIR)
	$(PROTOC) --proto_path=proto --go_out=$(GENDIR) --go-grpc_out=$(GENDIR) proto/mlbctl.proto

format:
	for dir in $$(find app pkg cmd -type d); do \
	  (cd $$dir && $(GO) fmt .); \
	done

run:
	test -z $(DESTDIR)$(srv_logdir) || $(MKDIR_P) $(DESTDIR)$(srv_logdir)
	test -z $(DESTDIR)$(srv_rundir) || $(MKDIR_P) $(DESTDIR)$(srv_rundir)
	env CGO_ENABLED=0 $(GO) run $(GOFLAGS) ./cmd/minilbd/... --asDaemon=false

BUILD_DIR= $(shell pwd)/TMP.build
DIST_DIR= $(shell pwd)/DIST

$(DIST_ARCHIVES): dist

package: debian-package
debian-package: $(DIST_ARCHIVES)
	mkdir -p $(BUILD_DIR)
	mv $(DIST_ARCHIVES) $(BUILD_DIR)
	cd $(BUILD_DIR) && $(AMTAR) -xf $(DIST_ARCHIVES)
	cd $(BUILD_DIR)/$(distdir) && ./configure --prefix=/usr
	cd $(BUILD_DIR)/$(distdir) && $(MAKE) clean
	cd $(BUILD_DIR)/$(distdir) && $(DBUILDPACKAGE) -nc -us -uc -ui -i -b
	mkdir -p $(DIST_DIR)
	$(CP) $(BUILD_DIR)/*.deb $(DIST_DIR)
	rm -rf $(BUILD_DIR)	

distclean-local: clean
	rm -rf autom4te.cache
	rm -rf $(BUILD_DIR) 
	rm -rf $(DIST_DIR) 

CWD=$(shell pwd)

clean-local:
	$(FIND) $(CWD)  -name '*~' | $(XARGS) rm -f
	rm -rf autom4te.cache
	rm -f cmd/minilbadm/minilbadm
	rm -f cmd/minilbctl/minilbctl
	rm -f cmd/minilbd/minilbd
	rm -rf tmp/
	rm -rf $(BUILD_DIR) 
	rm -rf $(EXPL_DIR)
