# This file is part of tcollector.
# Copyright (C) 2011  The tcollector Authors.
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at your
# option) any later version.  This program is distributed in the hope that it
# will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
# General Public License for more details.  You should have received a copy
# of the GNU Lesser General Public License along with this program.  If not,
# see <http://www.gnu.org/licenses/>.

# This Makefile is a complete train wreck.  I'm sorry.  --tsuna

PACKAGE_VERSION := 1.2.1
RPM_REVISION := 1
# Where do we install our files.
tcollectordir := /usr/local/tcollector

# Various commands we need.
MKDIR_P := mkdir -p
CP_P := cp -p
# In some environment this command can be called `gsha1sum' or `digest'.
SHA1SUM := sha1sum
ZIP := zip -9
PYTHON_VERSION := 2.7
PYTHON := python$(PYTHON_VERSION)

# Our "source tree" is in the parent directory, always.
VPATH := ..

# What files to put in the RPM.
FILES := \
	collectors/0/dfstat.py	\
	collectors/0/ifstat.py	\
	collectors/0/iostat.py	\
	collectors/0/netstat.py	\
	collectors/0/procnettcp.py	\
	collectors/0/procstats.py	\
	collectors/0/smart-stats.py	\
	collectors/__init__.py	\
	collectors/lib/__init__.py	\
	collectors/lib/utils.py	\
	tcollector.py	\

# Extra EOS-specific collectors
EXTRA := \
	eos/collectors/agentcpu.sh	\
	eos/collectors/eos.py	\

git_version := \
  `git rev-list --pretty=format:%h HEAD --max-count=1 | sed 1d || echo unknown`
git_fullsha1 := \
  `git rev-list --pretty=format:%H HEAD --max-count=1 | sed 1d || echo unknown`
# What / where do we build.
DESTDIR := destdir
RPM_TARGET := noarch
BASENAME := tcollector-$(PACKAGE_VERSION)-$(RPM_REVISION)
RPM := $(BASENAME).$(RPM_TARGET).rpm
EOSRPM := $(BASENAME)-eos.$(RPM_TARGET).rpm
SWIX := $(BASENAME).swix

all: rpm $(SWIX)
rpm: $(RPM)

$(SWIX): $(EOSRPM) manifest.txt
	$(ZIP) $@ $^

manifest.txt: $(EOSRPM) Makefile
	set -e; { \
          echo 'format: 1'; \
          echo 'primaryRpm: $(EOSRPM)'; \
          echo -n '$(EOSRPM)-sha1: '; \
          set `$(SHA1SUM) "$(EOSRPM)"`; \
          echo $$1; \
        } >$@-t
	mv $@-t $@

# TODO(tsuna): Find whether we can get rpmbuild to not create the rpm under a
# subdirectory.  This is annoying.
.spec.rpm:
	rpmbuild --target=$(RPM_TARGET) --buildroot=`pwd`/$(DESTDIR) -bb $<
	test -f $@ || for rpm in $(RPM_TARGET)/$@ \
		`awk '$$1=="Name:"{print $$2}' $<`.`awk '$$1=="BuildArch:"{print $$2}' $<`.rpm; do \
          test -f "$$rpm" && mv "$$rpm" $@ && break; \
        done
	if test -d $(RPM_TARGET); then rmdir $(RPM_TARGET); fi

.dist-stamp: $(FILES) $(EXTRA) Makefile
	rm -rf $(DESTDIR)
	$(MKDIR_P) "$(DESTDIR)"
	@: >filelist >filelist.extra
	for file in $(FILES); do \
          d=`dirname "$$file"`;  \
          if test ! -d "$(DESTDIR)/$(tcollectordir)/$$d"; then \
            echo "%dir \"$(tcollectordir)/$$d\"" >>filelist; \
            echo $(MKDIR_P) "'$(DESTDIR)/$(tcollectordir)/$$d'"; \
            $(MKDIR_P) "$(DESTDIR)/$(tcollectordir)/$$d" || exit $$?; \
          fi; \
          echo "\"$(tcollectordir)/$$file\"" >>filelist; \
          case $$file in \
            (*.py) echo "\"$(tcollectordir)/$${file}c\"" >>filelist;; \
          esac; \
          echo $(CP_P) "$(VPATH)/$$file" "'$(DESTDIR)/$(tcollectordir)/$$file'"; \
          $(CP_P) "$(VPATH)/$$file" "$(DESTDIR)/$(tcollectordir)/$$file" || exit $$?; \
        done
	@for file in $(EXTRA); do \
          src=$(VPATH)/$$file; \
          file="$(tcollectordir)/collectors/0/`basename $$file`"; \
          echo "\"$$file\"" >>filelist.extra; \
          case $$file in \
            (*.py) echo "\"$${file}c\"" >>filelist.extra;; \
          esac; \
          echo $(CP_P) "$$src" "'$(DESTDIR)/$$file'"; \
          $(CP_P) "$$src" "$(DESTDIR)/$$file" || exit $$?; \
        done
	$(PYTHON) -m compileall -q "$(DESTDIR)"
	chmod -R a+rX "$(DESTDIR)"
	@touch $@

common.spec: .dist-stamp tcollector.spec ChangeLog $(VPATH)/README Makefile
	set -e; { \
          echo "# File generated by Makefile, do not edit"; \
	  sed \
            -e 's/@PACKAGE_VERSION@/$(PACKAGE_VERSION)/' \
            -e 's/@PYTHON_VERSION@/$(PYTHON_VERSION)/' \
            -e "s/@GIT_SHORTSHA1@/$(git_version)/" \
            -e "s/@GIT_FULLSHA1@/$(git_fullsha1)/" \
            -e 's/@RPM_REVISION@/$(RPM_REVISION)/' \
	    tcollector.spec; \
        } >$@-t
	mv $@-t $@

RPM_SPEC := $(RPM:.rpm=.spec)
$(RPM_SPEC): common.spec linux.spec
	mkdir -p $(DESTDIR)/etc/init.d
	cp initd.sh $(DESTDIR)/etc/init.d/tcollector
	{ \
          cat $^; \
          echo %files; \
          cat filelist; \
          echo '%attr(755, -, -) "/etc/init.d/tcollector"'; \
          echo %changelog; \
          cat ChangeLog; \
          echo %description; \
          cat $(VPATH)/README; \
	} >$@-t
	mv $@-t $@

EOSRPM_SPEC := $(EOSRPM:.rpm=.spec)
$(EOSRPM_SPEC): eos.spec common.spec
	$(MKDIR_P) $(DESTDIR)/usr/bin
	$(CP_P) "$(VPATH)/eos/tcollector" "$(DESTDIR)/usr/bin/"
	$(MKDIR_P) $(DESTDIR)/usr/lib/python$(PYTHON_VERSION)/site-packages/
	$(CP_P) "$(VPATH)/eos/tcollector_agent.py" "$(DESTDIR)/usr/lib/python$(PYTHON_VERSION)/site-packages/"
	{ \
          cat $^; \
          echo %files; \
          cat filelist; \
          cat filelist.extra; \
          echo /usr/lib/python$(PYTHON_VERSION)/site-packages/tcollector_agent.py; \
          echo '%attr(755, -, -) /usr/bin/tcollector'; \
          echo %changelog; \
          cat ChangeLog; \
          echo %description; \
          cat $(VPATH)/README; \
	} >$@-t
	mv $@-t $@

clean:
	rm -rf "$(DESTDIR)/$(tcollectordir)"
	rm -f $(DESTDIR)/etc/init.d/tcollector
	rm -f $(DESTDIR)/usr/bin/tcollector
	rm -f $(DESTDIR)/usr/lib/python$(PYTHON_VERSION)/site-packages/tcollector_agent.py*
	@test -d "$(DESTDIR)" || exit 0; \
        find "$(DESTDIR)" -depth -type d \
        | while read dir; do \
          echo rmdir "'$$dir'"; \
          rmdir "$$dir" || exit $$?; \
        done
	rm -f common.spec $(RPM_SPEC) $(EOSRPM_SPEC) filelist manifest.txt $(EOSRPM)
	@rm -f .dist-stamp

distclean: clean
	rm -f $(SWIX) $(RPM)

.PHONY: all rpm clean distclean
.SUFFIXES: .spec .rpm
