# distribution 
date=$(shell date +%y%m%d)
distdir = $(PACKAGE)-$(VERSION)
installer_rel = $(shell cat $(root_dir)/etc/docomomo-release)
linuxinstallerdir = $(installer_rel)
DIST_ARCHIVES = $(build_dir)/$(distdir).tar.gz
LINUX_INSTALLER = $(build_dir)/$(distdir)-linux-installer-$(date).tar.gz
linux_installer_md5 = $(build_dir)/$(distdir)-linux-installer-$(date).md5sum

distsourcedir = $(PACKAGE)-sources-$(system_release)
srpms_dir = $(distsourcedir)/SRPMS
sources_dir = $(distsourcedir)/SOURCES
#mirrorsite = ftp://ftp.iij.ad.jp/pub/linux/momonga
mirrorsite = ftp://202.232.140.135/pub/linux/momonga
mirrorbase = $(mirrorsite)/4
mirrorupdates = $(mirrorsite)/updates/4

# www server
www_user = zunda
www_host = tty.freeshell.org
www_path = html/docomomo
www_rsync = '--rsync-path=~/local/bin/rsync'

# installer server
sf_user = zunda
sf_host = shell.sourceforge.jp
sf_path = /home/groups/d/do/docomomo/htdocs
sf_rsync = 

PHONY: commit
commit: .svn ChangeLog
	# commit
	cd $(top_srcdir) && \
	log=`sed -n '/^$$/q;p' ChangeLog` && \
	svn commit -m "$$log" && \
	echo "$$log"; \
	logver=`sed '/^$$/q;s/^- \+(\([0-9]\+\.[0-9]\+\.[0-9]\+\)).*/\1/;t;d;p' ChangeLog`; \
	if [ -n "$$logver" ]; then \
		if [ "$$logver" != "$(VERSION)" ]; then \
			echo "*** version mismatch between Makefile.install and ChangeLog"; \
			exit 1; \
		fi; \
		cur_repo=`svn info | grep URL: | cut -f2 -d\ `;\
		tag_repo=`echo  $$cur_repo | sed "s/trunk/tags/;s/$$/-$$logver/"`;\
		svn remove -m "$$log" $$tag_repo 2>/dev/null; \
		svn cp -m "$$log" $$cur_repo $$tag_repo; \
		echo Svn tag made at $$tag_repo; \
	fi

PHONY: clean-devel
clean-devel:
	rm -f README.ja.html linux-boot-process.ja.html
	rm -f $(build_dir)/sources.list
	rm -f $(build_dir)/sources.missing.list
	rm -f $(build_dir)/srpms.list
	rm -f $(build_dir)/srpms.missing.list
	rm -f $(build_dir)/download-srpms.timestamp
	rm -f $(build_dir)/sources.in-rpm.list
	rm -rf $(tmp_dir)/$(distdir)
	-[ -e $(root_dir)/etc/docomomo-release ] && rm -f $(tmp_dir)/$(linuxinstallerdir)
	#rm -f $(LINUX_INSTALLER) $(DIST_ARCHIVES)

PHONY: dist
dist:
	files=`svn stat | grep -v '^ ' | grep -v '^?'`;\
	if [ -n "$$files" ]; then \
		echo $$files; echo "*** Commit before making dist"; exit 1 ;\
	fi
	rm -rf $(tmp_dir)/$(distdir)
	mkdir -p $(tmp_dir) && \
	svn info | grep URL: | sed "s/.* //;s/trunk/tags/;s/$$/-$(VERSION)/" |\
	xargs -i svn export {} $(tmp_dir)/$(distdir) && \
	(cd $(tmp_dir); tar --exclude .svn --create --file - $(distdir)) | gzip -c >| $(DIST_ARCHIVES)
	@echo "Created $(DIST_ARCHIVES) from tagged version"

PHONY: $(DIST_ARCHIVES)
$(DIST_ARCHIVES): dist

README.ja.html: README.ja
	rd2 -r rd/rd2html-ext-lib.rb --with-css=http://zunda.freeshell.org/css/zunda.css --html-title="DocoMomo README.ja" --html-charset=EUC-JP --html-lang=ja --native-inline $^ >| $@ || (rm -f $@; exit 1)

linux-boot-process.ja.html: linux-boot-process.ja
	rd2 -r rd/rd2html-lib.rb --with-css=http://zunda.freeshell.org/css/zunda.css --html-title="DocoMomo/linux-boot-process.ja" --html-charset=EUC-JP --html-lang=ja $^ >| $@ || rm -f $@

PHONY: upload
upload: README.ja.html linux-boot-process.ja.html $(DIST_ARCHIVES)
	mkdir -p $(tmp_dir)/www/dist
	cp README.ja.html $(tmp_dir)/www/README.ja.html
	cp linux-boot-process.ja.html $(tmp_dir)/www/linux-boot-process.ja.html
	cp ChangeLog $(tmp_dir)/www/ChangeLog.txt
	cp GPL.txt $(tmp_dir)/www/GPL.txt
	cp gpl-3.0.txt $(tmp_dir)/www/gpl-3.0.txt
	cp COPYING $(tmp_dir)/www/COPYING.txt
	cp $(DIST_ARCHIVES) $(tmp_dir)/www/dist
	rsync -avz -e ssh $(www_rsync) $(tmp_dir)/www/ $(www_user)@$(www_host):$(www_path)

# obtaining source files
PHONY: list-rpms
list-rpms: $(distsourcedir)/$(PACKAGE)-$(VERSION).$(system_release).rpms.list
$(distsourcedir)/$(PACKAGE)-$(VERSION).$(system_release).rpms.list: $(root_dir)/var/cache/yum/base/repomd.xml
	mkdir -p $(distsourcedir)
	rpm -qa --root $(absolute_root_dir) | sort >| $@

PHONY: list-srpms
list-srpms: $(build_dir)/srpms.list
$(build_dir)/srpms.list: $(root_dir)/var/cache/yum/base/repomd.xml
	rpmquery --queryformat='%{SOURCERPM}\n' --root $(absolute_root_dir) -a |\
	grep -v -x '(none)' | sort -u >| $@

PHONY: download-srpms
download-srpms: $(build_dir)/download-srpms.timestamp
$(build_dir)/download-srpms.timestamp: $(build_dir)/srpms.list
	mkdir -p $(srpms_dir)
	-for url in $(mirrorbase) $(mirrorupdates); do \
		find $(srpms_dir) -name '*src.rpm' -printf '%f\n' >| $(build_dir)/srpms.local.list; \
		grep -v -x -f $(build_dir)/srpms.local.list $(build_dir)/srpms.list |\
		(cd  $(srpms_dir); xargs --no-run-if-empty -i $(DOWNLOAD) $$url/SRPMS/{}) \
	done
	find $(srpms_dir) -name '*src.rpm' -printf '%f\n' >| $(build_dir)/srpms.local.list
	-grep -v -x -f $(build_dir)/srpms.local.list $(build_dir)/srpms.list >| $(build_dir)/srpms.missing.list
	rm -f $(build_dir)/srpms.local.list
	touch $(build_dir)/download-srpms.timestamp

PHONY: list-sources
list-sources: $(build_dir)/sources.list
$(build_dir)/sources.list: $(build_dir)/download-srpms.timestamp
	rpmquery --queryformat='%{SOURCE}\n' -p $(srpms_dir)/*.nosrc.rpm |\
	grep -v -x '(none)' | sort -u >| $(build_dir)/sources.queried.list
	rpmquery --queryformat='%{FILENAMES}\n' -p $(srpms_dir)/*.nosrc.rpm |\
	sort -u >| $(build_dir)/sources.in-rpm.list
	-grep -v -x -f  $(build_dir)/sources.in-rpm.list $(build_dir)/sources.queried.list >| $@
	rm -f $(build_dir)/sources.in-rpm.list
	rm -f $(build_dir)/sources.queried.list

PHONY: download-sources
download-sources: $(build_dir)/download-sources.timestamp
$(build_dir)/download-sources.timestamp: $(build_dir)/sources.list
	mkdir -p $(sources_dir)
	-for url in $(mirrorbase) $(mirrorupdates); do \
		find $(sources_dir) -printf '%f\n' >| $(build_dir)/sources.local.list; \
		grep -v -x -f $(build_dir)/sources.local.list $(build_dir)/sources.list |\
		(cd  $(sources_dir); xargs --no-run-if-empty -i $(DOWNLOAD) $$url/SOURCES/{}) \
	done
	find $(sources_dir) -printf '%f\n' >| $(build_dir)/sources.local.list
	-grep -v -x -f $(build_dir)/sources.local.list $(build_dir)/sources.list >| $(build_dir)/sources.missing.list
	rm -f $(build_dir)/sources.local.list
	touch $(build_dir)/download-sources.timestamp

PHONY: create-linux-installer
create-linux-installer: $(LINUX_INSTALLER)
$(LINUX_INSTALLER): update-packages $(tmp_dir)/initrd.img $(tmp_dir)/usr.sqs setup-users $(root_dir)/etc/docomomo-release
	files=`svn stat | grep -v '^ ' | grep -v '^?'`;\
	if [ -n "$$files" ]; then \
		echo $$files; echo "*** Warning: there are changes not commited ***"; \
	fi
	rm -rf $(tmp_dir)/$(linuxinstallerdir)
	mkdir -p $(tmp_dir)/$(linuxinstallerdir)
	for f in COPYING ChangeLog GPL.txt gpl-3.0.txt README.ja; do \
		cp $$f $(tmp_dir)/$(linuxinstallerdir)/; \
	done
	mkdir -p $(tmp_dir)/$(linuxinstallerdir)/$(script_dir)
	cp $(script_dir)/absolute-path.py $(tmp_dir)/$(linuxinstallerdir)/$(script_dir)/
	cp Makefile.install $(tmp_dir)/$(linuxinstallerdir)/Makefile
	mkdir -p $(tmp_dir)/$(linuxinstallerdir)/$(tmp_dir)
	for f in initrd.img usr.sqs; do \
		cp $(tmp_dir)/$$f $(tmp_dir)/$(linuxinstallerdir)/$(tmp_dir)/; \
	done
	mkdir -p  $(tmp_dir)/$(linuxinstallerdir)/$(root_dir)
	cp -a $(root_dir)/home $(tmp_dir)/$(linuxinstallerdir)/$(root_dir)/
	mkdir -p $(tmp_dir)/$(linuxinstallerdir)/$(conf_dir)
	for f in grub.conf docomomo.xgz; do \
		cp $(conf_dir)/$$f $(tmp_dir)/$(linuxinstallerdir)/$(conf_dir); \
	done
	mkdir -p $(tmp_dir)/$(linuxinstallerdir)/$(root_dir)/etc/
	cp $(root_dir)/etc/docomomo-release $(tmp_dir)/$(linuxinstallerdir)/$(root_dir)/etc/
	mkdir -p $(tmp_dir)/$(linuxinstallerdir)/$(root_dir)/boot/
	cp $(root_dir)/boot/vmlinuz $(tmp_dir)/$(linuxinstallerdir)/$(root_dir)/boot/
	(cd $(tmp_dir); tar --exclude .svn --create --file - $(linuxinstallerdir)) | gzip -c >| $(LINUX_INSTALLER)
	@echo "Created $(LINUX_INSTALLER)"

PHONY: upload-sources
upload-sources: download-sources
	rsync -avz -e ssh $(sf_rsync) $(distsourcedir) $(sf_user)@$(sf_host):$(sf_path)

PHONY: upload-installer
upload-installer: upload-sources
	md5sum $(LINUX_INSTALLER) > $(linux_installer_md5)
	rsync -vvz -e ssh $(sf_rsync) $(LINUX_INSTALLER) $(linux_installer_md5) $(sf_user)@$(sf_host):$(sf_path)

