# ROOT is added when building RPM

prefix = $(ROOT)@prefix@
exec_prefix = @exec_prefix@
datarootdir = @datarootdir@
datadir = @datadir@
bindir = @bindir@

default: Makefile Content

configure: configure.ac
	autoconf

Makefile: $(wildcard *.in) configure etoys.spec
	./autogen.sh --prefix=@prefix@


# install regular etoys

content_url = http://etoys.squeak.org/svn/trunk/Etoys
content_rev = $(shell cat Content.revision)
content_host = $(shell svn info Content 2>/dev/null | grep ^URL | sed 's,.*//,,;s,/.*,,')

Content: Content.revision
	if [ "$(content_host)" = "etoys.laptop.org" ] ; then \
		mv Content Content-old ; \
	fi
	if [ ! -d Content ] ; \
	then svn checkout -r $(content_rev) $(content_url) Content ; \
	else svn update -r $(content_rev) Content ; \
	fi
	touch $@ -r $<

ChangeLog: Content
	./mkChangeLog

etoys = Content/etoys.image     \
	Content/etoys.changes \
	Content/EtoysV5.stc \
	$(wildcard Content/*.pr)

examples = $(wildcard \
	Content/ExampleEtoys/*.pr  \
	Content/ExampleEtoys/*.gif \
	Content/ExampleEtoys/*.ogg)

docs = README INSTALL LICENSE NOTICE ChangeLog Content/NEWS

etoysdir = $(datadir)/etoys
mimedir  = $(datadir)/mime/packages
exmpldir = $(etoysdir)/ExampleEtoys
docsdir = $(datadir)/doc/etoys
dirs = $(bindir) $(etoysdir) $(mimedir) $(exmpldir) $(docsdir)

install-etoys: Content
	for d in $(dirs) ; do mkdir -p $$d ; done
	install -v etoys $(bindir)
	for f in $(etoys) ; do install -v -m 644 $$f $(etoysdir); done
	for f in $(examples); do install -v -m 644 $$f $(exmpldir); done
	for f in Content/locale/*/*/*.sexp.data.gz ; do \
		dir=`dirname $$f|sed 's/^Content.//'` ; \
		mkdir -p $(etoysdir)/$$dir ; \
		dest=$(etoysdir)/$$dir/`basename $$f` ; \
		echo -n $$dest; \
		if gunzip -t $$f ; \
		then gunzip -c $$f | gzip -v --rsyncable > $$dest ; \
		else cp $$f $$dest ; echo "recovered: copied instead" ; \
		fi ; \
		chmod 644 $$dest; \
	done
	for f in Content/locale/*/*/index.txt ; do \
		dir=`dirname $$f|sed 's/^Content.//'` ; \
		mkdir -p $(etoysdir)/$$dir ; \
		install -v -m 644 $$f $(etoysdir)/$$dir ; \
	done
	for f in $(docs) ; do install -v -m 644 $$f $(docsdir); done
	for f in Content/locale/*/*/*.mo ; do \
		dir=`dirname $$f|sed 's/^Content.//'` ; \
		mkdir -p $(etoysdir)/$$dir ; \
		install -v -m 644 $$f $(etoysdir)/$$dir ; \
	done
	install -v -m 644 etoys.xml $(mimedir)


# install activity - only used from sugar-jhbuild

activitydir = $(datadir)/sugar/activities/Etoys.activity
activityfiles = NEWS COPYING.activity etoys-activity
activitymeta = activity.info activity-etoys.svg
activityall = $(activityfiles) $(activitymeta)
activityicons = $(shell grep ^mime_types activity.info|tr ' ;/' '\n\n-'|grep squeak)

install-activity: $(activityall)
	mkdir -p $(activitydir)/activity
	mkdir -p $(activitydir)/bin
	install -v etoys-activity $(activitydir)/bin
	rm -f $(activitydir)/etoys-activity
	install -v setup.py $(activitydir)
	install -v -m 644 NEWS $(activitydir)
	install -v -m 644 COPYING.activity $(activitydir)/COPYING
	install -v -m 644 activity.info $(activitydir)/activity
	install -v -m 644 activity-etoys.svg $(activitydir)/activity
	for f in $(activityicons) ; do \
		ln -sf activity-etoys.svg $(activitydir)/activity/$$f.svg ; \
	done


# install both, etoys and activity.
# RPM uses install-etoys only, and runs update-mime as post script

install: install-etoys install-activity
	update-mime-database $(datadir)/mime

check:
	@echo SKIPPED: No tests defined for Etoys

# build .xo bundle - invoke "make xo" manually 

xo_version = @ACTIVITY_VERSION@

Etoys.activity: $(activityall)
	rm -rf $@/*
	$(MAKE) activitydir=$@ install-activity

Etoys-$(xo_version).xo: Etoys.activity
	rm -f $</MANIFEST
	find $< -type f -o -type l| sed 's|^[^/]*/||' > MANIFEST
	mv MANIFEST $</
	./mkDist.py

xo: Etoys-$(xo_version).xo

# stuff below for maintainer, only invoked manually

run:
	$(bindir)/etoys	

etoys-kill:
	-(ps ax|egrep '[s]queak.*etoys.image' |sed 's/[^ 0-9].*//'|xargs -r kill)

etoys-rebuild: etoys-kill
	cp $(etoysdir)/etoys.image Content/
	cp $(etoysdir)/etoys.changes Content/
	./autogen.sh --prefix=@prefix@
	make
	make install

etoys_ver = $(shell rpm -q --queryformat '%{VERSION}' --specfile etoys.spec)

etoys-src: Content ChangeLog configure
	-rm -rf etoys-$(etoys_ver) etoys-$(etoys_ver).tar.gz
	mkdir etoys-$(etoys_ver)
	tar cf - --no-recursion \
	    `git ls-files` \
		`svn ls -R Content|sed s@^@Content/@` \
		configure \
		| tar xf - -C etoys-$(etoys_ver)
	tar cvzf etoys-$(etoys_ver).tar.gz etoys-$(etoys_ver)
	-rm -rf etoys-$(etoys_ver)

etoys_rpm = $(shell rpm -q --queryformat '%{NAME}-%{VERSION}-%{RELEASE}' --specfile etoys.spec)
rpm_build = $(shell rpmbuild -E '%_topdir' etoys.spec 2>/dev/null)

etoys-rpm:
	ln etoys-$(etoys_ver).tar.gz $(rpm_build)/SOURCES/
	rpmbuild -ba etoys.spec

etoys-release:
	scp $(rpm_build)/RPMS/noarch/$(etoys_rpm).noarch.rpm \
	    $(rpm_build)/SRPMS/$(etoys_rpm).src.rpm \
            tinlizzie.org:/var/www/tinlizzie/olpc/sugar/rpm/
	scp $(rpm_build)/BUILD/etoys-$(etoys_ver)/etoys.spec \
	    tinlizzie.org:/var/www/tinlizzie/olpc/sugar/rpm/$(etoys_rpm).spec


xo-release:  Etoys-$(xo_version).xo
	scp $< tinlizzie.org:/var/www/tinlizzie/olpc/sugar/xo/

upload-to-sugarlabs:
	scp etoys-$(etoys_ver).tar.gz \
	    download.sugarlabs.org:/srv/upload/sources/sucrose/glucose/etoys/

upload-to-laptop-org:
	scp $(rpm_build)/RPMS/noarch/$(etoys_rpm).noarch.rpm \
	    dev.laptop.org:/var/www/etoys/rpms/
	scp $(rpm_build)/SRPMS/$(etoys_rpm).src.rpm \
	    dev.laptop.org:/var/www/etoys/srpm/
	scp $(rpm_build)/BUILD/etoys-$(etoys_ver)/etoys.spec \
	    dev.laptop.org:/var/www/etoys/srpm/$(etoys_rpm).spec

