# Make command to use for dependencies
MAKECMD=make
                                                                                                        
# If no distributive is specified, "suse" will be used                                                                                                        
ifndef "DIST"
DIST=suse
endif
                                                                                                        
# If no release is specified, 1 will be used                                                                                                        
ifndef RELEASE
RELEASE=1
endif

ifndef TAG
SVN_URL=svn://esvn.umputun.com/esvn/trunk
endif

ifdef TAG
SVN_URL=svn://esvn.umputun.com/esvn/tags/$(TAG)
endif

# if no VERSION is specified content of VERSION file will be used
ifndef VERSION
VERSION=`cat esvn/VERSION`
endif

ifdef TYPE
TYPE=DYNAMIC
endif

ifeq ($(TYPE),STATIC)
CCFLAGS += QT_STATIC
endif
                                                                                                        
DATE=`date +%Y%m%d`

ifeq ($(DIST),suse)
    ID=suse92
    PACKAGES=packages
    CPU=i586
    REQ=qt3-devel, qt3
endif

ifeq ($(DIST),fedora)
    ID=fc2
    PACKAGES=redhat
    CPU=i386
    REQ=qt-devel, qt
endif

.PHONY : update gz clean spec rpm html-doc pdf move-docs site index download upload remote

update:
	@echo "Loading fresh copy of eSvn ..."
	rm -r -f esvn
	svn export $(SVN_URL) esvn

gz:
	@echo "Compressing source files ..."
	rm -r -f esvn/test
	rm -r -f esvn/dist
	rm -r -f esvn/html
	tar -czf esvn-$(VERSION)-$(RELEASE).tar.gz esvn       


clean:
	rm -r -f esvn
	rm -r -f html-docs

#make spec file for DIST
spec:
	@echo $(VERSION)-$(RELEASE) for $(DIST)
	cat esvn/esvn.spec | sed "s/Version.*/Version: $(VERSION)/" | sed "s/Release.*/Release: $(RELEASE)/" | sed "s/Requires.*/Requires: $(REQ)/" > esvn/esvn.spec.tmp
	mv -f esvn/esvn.spec.tmp esvn/esvn.spec

#build rpm and srpm and copy to current dir
rpm:
	su -c "rpmbuild -ta esvn-$(VERSION)-$(RELEASE).tar.gz"
	cp esvn/esvn.spec esvn-$(VERSION)-$(RELEASE).$(ID).spec
	cp /usr/src/$(PACKAGES)/SRPMS/esvn-$(VERSION)-$(RELEASE).src.rpm esvn-$(VERSION)-$(RELEASE).src.rpm
	cp /usr/src/$(PACKAGES)/RPMS/$(CPU)/esvn-$(VERSION)-$(RELEASE).$(CPU).rpm esvn-$(VERSION)-$(RELEASE).$(CPU).$(ID).rpm
	

html-doc:
	
	java com.icl.saxon.StyleSheet esvn/docs/manual/esvn.xml esvn/docs/libs/html-cust.xsl
	cp esvn/docs/libs/*.css html-docs
	mkdir html-docs/images
	cp -r esvn/docs/manual/images/* html-docs/images
	mkdir html-docs/common
	mkdir html-docs/common/navig
	cp -r esvn/docs/common/navig/* html-docs/common/navig
	mkdir html-docs/common/admon
	cp -r esvn/docs/common/admon/* html-docs/common/admon
	
	java com.icl.saxon.StyleSheet -o html-docs/faq.html esvn/docs/faq/faq.xml  /usr/share/xml/docbook/stylesheet/nwalsh/current/html/docbook.xsl
	
	java com.icl.saxon.StyleSheet -o filelist.sh esvn/docs/manual/esvn.xml esvn/docs/libs/foImageList.xsl
	chmod 755 filelist.sh
	./filelist.sh	
	rm filelist.sh
                                                                                                                               
pdf:
	java com.icl.saxon.StyleSheet -o esvn/docs/manual/esvn.fo esvn/docs/manual/esvn.xml esvn/docs/libs/fo-cust.xsl
	java -Xmx128M org.apache.fop.apps.Fop -fo esvn/docs/manual/esvn.fo -pdf esvn.pdf	
	rm esvn/docs/manual/esvn.fo

move-docs:
	mv -f html-docs esvn	
	#mv -f html-docs/esvn.pdf esvn

site:
	cp -f esvn-$(VERSION)-$(RELEASE).tar.gz /mnt/www/umputun/esvn       
	cp -f esvn-$(VERSION)-$(RELEASE).$(ID).spec /mnt/www/umputun/esvn
	cp -f esvn-$(VERSION)-$(RELEASE).src.rpm /mnt/www/umputun/esvn
	cp -f esvn-$(VERSION)-$(RELEASE).$(CPU).$(ID).rpm /mnt/www/umputun/esvn
	cp -r -f esvn/html-docs /mnt/www/umputun/esvn

index:
	cp -f esvn/html/index.html /mnt/www/umputun/esvn
	cp -f esvn/html/versions.html /mnt/www/umputun/esvn
	cp -f esvn/html/update.shtml /mnt/www/umputun/esvn
	find /mnt/www/umputun/esvn -maxdepth 1 -name "*.tar.gz" -exec ln -f -s '{}' /mnt/www/umputun/esvn/versions ';'
	find /mnt/www/umputun/esvn -maxdepth 1 -name "*.rpm" -exec ln -f -s '{}' /mnt/www/umputun/esvn/versions ';'
	find /mnt/www/umputun/esvn -maxdepth 1 -name "*.exe" -exec ln -f -s '{}' /mnt/www/umputun/esvn/versions ';'
	find /mnt/www/umputun/esvn -maxdepth 1 -name "*.zip" -exec ln -f -s '{}' /mnt/www/umputun/esvn/versions ';'
	find /mnt/www/umputun/esvn -maxdepth 1 -name "*.spec" -exec ln -f -s '{}' /mnt/www/umputun/esvn/versions ';'
	
#download tar.gz
download:
	rm -f esvn-$(VERSION)-$(RELEASE).tar.gz
	rm -f -r esvn
	wget http://esvn.umputun.com/esvn-$(VERSION)-$(RELEASE).tar.gz
	tar -zxf esvn-$(VERSION)-$(RELEASE).tar.gz

#upload results of build to site (uses with remote target)
upload:
	scp esvn-$(VERSION)-$(RELEASE).$(CPU).$(ID).rpm evgb@192.168.0.1:/var/www/umputun/esvn
	scp esvn-$(VERSION)-$(RELEASE).$(ID).spec evgb@192.168.0.1:/var/www/umputun/esvn
	

#composite targets

#prepare documentetion and update site
doc: update html-doc pdf
	cp -r -f html-docs /mnt/www/umputun/esvn
	
#build all (docs, gz, rpm and update site)
all: clean update doc move-docs spec gz rpm site
	@echo "all done. $(VERSION)-$(RELEASE) was built"

#build rpm and gz only
prog:  update spec gz rpm
	
#remote mode (building without docs rebuild and without getting version from svn)
remote: clean download spec gz rpm upload
	@echo "$(VERSION)-$(RELEASE) was built"

local:	clean doc move-docs prog
	
homepage: clean update index



