AUTOMAKE_OPTIONS = foreign no-dependencies no-installinfo

SUBDIRS = mans

GOFLAGS= -v
bin_PROGRAMS = mstorectl
sbin_PROGRAMS = mstored

mstorectl_SOURCES = \
	cmd/mstorectl/main.go \
	cmd/mstorectl/util.go

EXTRA_mstorectl_SOURCES = \
    cmd/mstorectl/accountcmd/acccmd.go \
    cmd/mstorectl/accountcmd/auxfunc.go \
    cmd/mstorectl/accountcmd/createacc.go \
    cmd/mstorectl/accountcmd/creategrant.go \
    cmd/mstorectl/accountcmd/delacc.go \
    cmd/mstorectl/accountcmd/delgrant.go \
    cmd/mstorectl/accountcmd/getacc.go \
    cmd/mstorectl/accountcmd/getgrant.go \
    cmd/mstorectl/accountcmd/grantcmd.go \
    cmd/mstorectl/accountcmd/listacc.go \
    cmd/mstorectl/accountcmd/listgrant.go \
    cmd/mstorectl/accountcmd/printresp.go \
    cmd/mstorectl/accountcmd/updacc.go \
    cmd/mstorectl/accountcmd/updgrant.go \
    cmd/mstorectl/filecmd/delcoll.go \
    cmd/mstorectl/filecmd/delfile.go \
    cmd/mstorectl/filecmd/expfiles.go \
    cmd/mstorectl/filecmd/filecmd.go \
    cmd/mstorectl/filecmd/fileinfo.go \
    cmd/mstorectl/filecmd/getfile.go \
    cmd/mstorectl/filecmd/impfiles.go \
    cmd/mstorectl/filecmd/listcolls.go \
    cmd/mstorectl/filecmd/listfiles.go \
    cmd/mstorectl/filecmd/putfile.go \
    cmd/mstorectl/imagecmd/imagecmd.go \
    cmd/mstorectl/imagecmd/printresp.go

mstored_SOURCES = cmd/mstored/main.go

EXTRA_mstored_SOURCES = cmd/mstored/starter/starter.go

mstorectl$(EXEEXT): $(mstorectl_SOURCES) $(EXTRA_mstorectl_SOURCES) $(EXTRA_mstored_SOURCES)
	env CGO_ENABLED=0 $(GO) build $(GOFLAGS) -o mstorectl$(EXEEXT) $(mstorectl_SOURCES)

mstored$(EXEEXT): $(mstored_SOURCES) $(EXTRA_mstored_SOURCES)
	env CGO_ENABLED=1 $(GO) build $(GOFLAGS) -o mstored$(EXEEXT) $(mstored_SOURCES)

run: $(mstored_SOURCES)
	cd cmd/mstored && env CGO_ENABLED=1 $(GO) run . --asDaemon=false

CWD=$(shell pwd)

EXTRA_mstored_SOURCES += \
	app/accoper/createacc.go \
	app/accoper/creategrant.go \
	app/accoper/delacc.go \
	app/accoper/delgrant.go \
	app/accoper/getacc.go \
	app/accoper/getgrant.go \
	app/accoper/listacc.go \
	app/accoper/listgrant.go \
	app/accoper/operator.go \
	app/accoper/updacc.go \
	app/accoper/updgrant.go \
	app/config/config.go \
	app/config/variant.go \
	app/fileoper/delcats.go \
	app/fileoper/delfile.go \
	app/fileoper/fileinfo.go \
	app/fileoper/getfile.go \
	app/fileoper/listcats.go \
	app/fileoper/listfiles.go \
	app/fileoper/operator.go \
	app/fileoper/putfile.go \
	app/handler/aaafunc.go \
	app/handler/account.go \
	app/handler/blob.go \
	app/handler/file.go \
	app/handler/grant.go \
	app/handler/handler.go \
	app/handler/manifest.go \
	app/handler/notfound.go \
	app/handler/response.go \
	app/handler/service.go \
	app/handler/version.go \
	app/imageoper/blobexist.go \
	app/imageoper/delblob.go \
	app/imageoper/delman.go \
	app/imageoper/getblob.go \
	app/imageoper/getman.go \
	app/imageoper/getrefer.go \
	app/imageoper/gettags.go \
	app/imageoper/imgaux.go \
	app/imageoper/listman.go \
	app/imageoper/manexist.go \
	app/imageoper/ociaux.go \
	app/imageoper/operator.go \
	app/imageoper/patchupload.go \
	app/imageoper/postupload.go \
	app/imageoper/putman.go \
	app/imageoper/putupload.go \
	app/imageoper/service.go \
	app/imageoper/version.go \
	app/locker/locker.go \
	app/logger/logger.go \
	app/maindb/account.go \
	app/maindb/blob.go \
	app/maindb/file.go \
	app/maindb/grant.go \
	app/maindb/init.go \
	app/maindb/maindb.go \
	app/maindb/manifest.go \
	app/maindb/scheme.go \
	app/router/bindobj.go \
	app/router/context.go \
	app/router/corsmw.go \
	app/router/loggingmw.go \
	app/router/pathc.go \
	app/router/recovermw.go \
	app/router/router.go \
	app/server/server.go \
	app/service/service.go \
	app/storage/storage.go \
	pkg/accntcli/client.go \
	pkg/accntcli/createacc.go \
	pkg/accntcli/creategrant.go \
	pkg/accntcli/delacc.go \
	pkg/accntcli/delgrant.go \
	pkg/accntcli/getacc.go \
	pkg/accntcli/getgrant.go \
	pkg/accntcli/httpcall.go \
	pkg/accntcli/listacc.go \
	pkg/accntcli/listgrants.go \
	pkg/accntcli/referer.go \
	pkg/accntcli/servhello.go \
	pkg/accntcli/updateacc.go \
	pkg/accntcli/updgrant.go \
	pkg/auxhttp/basic.go \
	pkg/auxhttp/crange.go \
	pkg/auxoci/ociaux.go \
	pkg/auxpwd/passwd.go \
	pkg/auxtool/cleandir.go \
	pkg/auxtool/fileex.go \
	pkg/auxtool/randstr.go \
	pkg/auxtool/tmpfile.go \
	pkg/auxtool/unixnow.go \
	pkg/auxutar/utar.go \
	pkg/auxuuid/uuid.go \
	pkg/auxx509/x509cert.go \
	pkg/client/client.go \
	pkg/client/imageaux.go \
	pkg/client/imagedelete.go \
	pkg/client/imageinfo.go \
	pkg/client/imagepull.go \
	pkg/client/imagepush.go \
	pkg/client/servaux.go \
	pkg/descr/account.go \
	pkg/descr/blob.go \
	pkg/descr/file.go \
	pkg/descr/grant.go \
	pkg/descr/manifest.go \
	pkg/descr/server.go \
	pkg/digest/digest.go \
	pkg/filecli/authbas.go \
	pkg/filecli/client.go \
	pkg/filecli/copyctx.go \
	pkg/filecli/delcoll.go \
	pkg/filecli/delfile.go \
	pkg/filecli/fileinfo.go \
	pkg/filecli/getfile.go \
	pkg/filecli/listcoll.go \
	pkg/filecli/listfiles.go \
	pkg/filecli/putfile.go \
	pkg/filecli/referer.go \
	pkg/repocli/blobexist.go \
	pkg/repocli/client.go \
	pkg/repocli/copywctx.go \
	pkg/repocli/delblob.go \
	pkg/repocli/delman.go \
	pkg/repocli/getblob.go \
	pkg/repocli/getman.go \
	pkg/repocli/gettoken.go \
	pkg/repocli/getupload.go \
	pkg/repocli/imager.go \
	pkg/repocli/manexist.go \
	pkg/repocli/patchupload.go \
	pkg/repocli/pullimage.go \
	pkg/repocli/putman.go \
	pkg/repocli/putupload.go \
	pkg/repocli/reference.go \
	pkg/repocli/referer.go \
	pkg/repocli/uuid.go \
	pkg/terms/terms.go


EXTRA_DIST = vendor/*  \
	\
	Containerfile \
	go.mod \
	go.sum \
	LICENSE.txt \
	README.md \
	DEVEL.md \
	\
	test/attic/account_test.go \
	test/attic/file_test.go \
	test/attic/image_test.go \
	test/file_test.go \
	\
	chart/Chart.yaml.in \
	chart/.gitignore \
	chart/.helmignore \
	chart/templates/configmap.yaml \
	chart/templates/deployment.yaml \
	chart/templates/_imagepath.tpl \
	chart/templates/_serviceport.tmpl \
	chart/templates/service.yaml \
	chart/templates/_storageclass.tpl \
	chart/templates/_storagesize.tpl \
	chart/templates/_userpass.tpl \
	chart/templates/volumeclaim.yaml \
	chart/values.yaml.in \
	\
	debian/changelog.in \
	debian/compat \
	debian/control.in \
	debian/files \
	debian/.gitignore \
	debian/mstore-control.install \
	debian/mstore-control.postinst \
	debian/mstore-control.postrm \
	debian/mstore-control.preinst \
	debian/mstore-control.prerm \
	debian/mstore-service.install \
	debian/mstore-service.postinst \
	debian/mstore-service.postrm \
	debian/mstore-service.preinst \
	debian/mstore-service.prerm \
	debian/patches/series \
	debian/README.Debian \
	debian/rules \
	debian/source/format \
	debian/source/include-binaries \
	debian/watch \
	\
	initrc/.gitignore \
	initrc/mstored.in \
	initrc/mstored.service.in \
	\
	app/locker/locker_test.go \
	app/logger/logger_test.go \
	app/maindb/file_test.go \
	app/maindb/grant_test.go \
	app/router/pathc_test.go \
	app/router/router_test.go \
	pkg/auxpwd/passwd_test.go \
	pkg/auxx509/x509cert_test.go \
	pkg/repocli/blobexist_test.go \
	pkg/repocli/copywctx_test.go \
	pkg/repocli/getblob_test.go \
	pkg/repocli/getman_test.go \
	pkg/repocli/gettoken_test.go \
	pkg/repocli/getupload_test.go \
	pkg/repocli/imager_test.go \
	pkg/repocli/manexist_test.go \
	pkg/repocli/pullimage_test.go \
	pkg/repocli/referer_test.go
	\
	docs/helm-chart-manifest.json.txt \
	docs/mstore.drawio \
	docs/mstore.png \
	docs/podman-manifest.json.txt




format:
	@dirs=$$($(FIND) $(CWD)/app $(CWD)/cmd  $(CWD)/pkg $(CWD)/test \
		 -name '*.go' | $(XARGS) -n1 dirname | $(SORT) | $(UNIQ)); \
	    for dir in $$dirs;do \
		(echo "====$$dir===="; cd $$dir && $(GO) fmt .); \
	    done

.PHONY: test
test:
	cd test && $(GO) test -v .


DIST_DIR= $(shell pwd)/DIST
BUILD_DIR= $(shell pwd)/BUILD

IMAGE_REPO = localhost
IMAGE_NAME = $(PACKAGE_NAME):$(PACKAGE_VERSION)
IMAGE_TARNAME = $(PACKAGE_NAME)-$(PACKAGE_VERSION).img
IMAGE_CONTAINERFILE = Containerfile

image:: build-image
build-image: clean
#	$(GO) mod vendor
	mkdir -p $(DIST_DIR)
	$(SUDO) $(PODMAN) build -t $(IMAGE_REPO)/$(IMAGE_NAME) -f $(IMAGE_CONTAINERFILE) .
	rm -f $(DIST_DIR)/$(IMAGE_TARNAME)
	$(SUDO) $(PODMAN) image save $(IMAGE_REPO)/$(IMAGE_NAME) --format oci-archive \
	      -o $(DIST_DIR)/$(IMAGE_TARNAME)
	username=$$(whoami); \
	    $(SUDO) chown $$username $(DIST_DIR)/$(IMAGE_TARNAME)
#	rm -rf vendor

CHART_NAME = $(PACKAGE_NAME)-$(PACKAGE_VERSION).tgz

chart:: build-chart
build-chart:
	mkdir -p $(DIST_DIR)
	$(HELM) package --destination $(DIST_DIR) chart/

$(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) && $(DPKGSOURCE) -i --before-build .
	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)

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)$(srv_datadir) || $(MKDIR_P) $(DESTDIR)$(srv_datadir)
if FREEBSD_OS
	test -z $(DESTDIR)$(FREEBSD_RCDIR) || $(MKDIR_P) $(DESTDIR)$(FREEBSD_RCDIR)
	$(INSTALL_DATA) initrc/mstored $(DESTDIR)$(FREEBSD_RCDIR)
	chmod a+x $(DESTDIR)$(FREEBSD_RCDIR)/mstored
endif
if LINUX_OS
if SYSTEMD
	test -z $(DESTDIR)$(LINUX_SYSTEMDDIR) || $(MKDIR_P) $(DESTDIR)$(LINUX_SYSTEMDDIR)
	$(INSTALL_DATA) initrc/mstored.service $(DESTDIR)$(LINUX_SYSTEMDDIR)
endif
endif

clean-local:
	$(FIND) $(CWD)  -name '*~' | $(XARGS) rm -f
	rm -rf autom4te.cache
	rm -f cmd/mstored/istored
	rm -f cmd/mstorectl/mstorectl
#	rm -rf tmp/

distclean-local:
	rm -rf autom4te.cache
	rm -rf $(DIST_DIR)
	rm -rf tmp/
