#############################################################################
# default: generate all
all:	mined help manual mnemodoc


#############################################################################
# generation
mined:
	./makemined

minced:
	./makemined minced

help:	usrshare/package_doc/VERSION
	./makemined help

mnemodoc:
	./makemined mnemodoc

generate:
	./makemined generate

clean:
	./makemined clean

manual:
	cd man; make

VER=$(shell sed -e 's,^[^0-9]*,,' -e 's,[^0-9][^0-9]*,.,g' -e q VERSION )
UNIVER=$(shell sed -e '/^\# Blocks-/ b ver' -e d -e ': ver' -e 's,^[^0-9]*\([0-9].*[0-9]\).*,\1,g' -e 's,\.0$$,,' src/Blocks.txt )

ver:
	echo mined $(VER)
	echo Unicode $(UNIVER)

src/version.h:	VERSION src/Blocks.txt
	echo '#define VERSION "$(VER)"' > src/version.h
	echo '#define UNICODE_VERSION "$(UNIVER)"' >> src/version.h

src/Blocks.txt:
	cd src; $(MAKE) -f mkinclud.mak Blocks.txt

usrshare/package_doc/VERSION:	VERSION
	echo $(VER) > usrshare/package_doc/VERSION

# "creole" file for sourceforge download page
CREOLE=README.creole.$(VER)
creole:	$(CREOLE)

$(CREOLE):	CHANGES VERSION
	# title
	echo "== MinEd $(VER)" > $(CREOLE).new
	# subtitle
	sed -e "1,/href=#$(VER)/ d" -e "/href=#/,$$ d" -e "s,^	*<[^/]*>.ighlights [(-]*\([^)<:]*\).*,//\1//," -e t -e d CHANGES >> $(CREOLE).new
	# prefix
	(echo; echo ----; echo; echo "=== Highlights"; echo) >> $(CREOLE).new
	# extract highlights
	sed -e "1,/href=#$(VER)/ d" -e "/href=#/,$$ d" -e "s,	<p[^/]*>\([^<]*\)</.*,==== \1," -e t -e "s,^		<li>,** ," -e t -e "s,^	<li>,* ," -e t -e "/^[ 	]*</ d" -e "s,^[ 	]*,," CHANGES >> $(CREOLE).new
	# suffix
	sed -e "1,/=== Highlights/ d" -e "/^----/,$$ b" -e d README.creole >> $(CREOLE).new
	# move in place
	mv -f $(CREOLE).new $(CREOLE)
	rm -f README.creole
	ln -s $(CREOLE) README.creole


#############################################################################
# description
#SUMMARY=Powerful text editor with extensive Unicode and CJK support.
SUMMARY=$(shell sed -e 's,<meta *name="*description"* content="*\([^"]*\)"* *>,\1,' -e t -e d doc/overview.html )

descriptions:	description.summary description.medium description.long

description.summary:	doc/overview.html makefile
	echo $(SUMMARY) > description.summary

description.medium:	DESCR
	sed -n -e "/Overview/ q" -e p DESCR > description.medium
	echo "For an overview and further information see http://mined.sourceforge.net/" >> description.medium

description.long:	DESCR
	cp DESCR description.long


wiki:	mined.wiki

wikisrc=doc/overview.html doc/design.html
DATE=` sed -e "s,.*-> mined $(VER) (\(.*\)),\1," -e t -e d doc/changes.html `

mined.wiki:	$(wikisrc) wiki.sed wiki.add makefile
	sed -e "/^$$/,$$ d" -e "s,VERSION,$(VER)," -e "s,DATE,$(DATE)," wiki.add > mined.wiki
	cat $(wikisrc) | tidy -utf8 -o -wrap 999 2> /dev/null | sed -f wiki.sed >> mined.wiki
	sed -e "/^$$/,$$ b" -e d wiki.add >> mined.wiki

ann:	announcements

announcements:	DESCR doc/changes.html
	DATE=$(DATE) sh -c "cd announcement; ./makeann > announcement-update"
	DATE=$(DATE) sh -c "cd announcement; ./makeann cygwin > cygwin-announce"
	cd announcement; ./makeupload > cygwin-upload


#############################################################################
# installation
install:
	./makemined install root=$(root) prefix=$(prefix) lprefix=$(lprefix) rundir=$(rundir) lrundir=$(lrundir)

localinstall:
	./makemined localinstall root=$(root) prefix=$(prefix) lprefix=$(lprefix) rundir=$(rundir) lrundir=$(lrundir)

optinstall:
	./makemined optinstall root=$(root) prefix=$(prefix) lprefix=$(lprefix) rundir=$(rundir) lrundir=$(lrundir)


#############################################################################
# source distribution and private archives
beta:	all
	if egrep -e "^(printf|#define debug)" src/*.[hc]; then false; fi
	./makearch -

LSM=release/mined.lsm
SPEC=release/mined.spec


lsm:	$(LSM)

rpm:	$(SPEC)

$(SPEC):	DESCR makefile
	echo $(VER)
	echo "# partial spec file for mined" > $(SPEC)
	echo "Name:         mined" >> $(SPEC)
	echo "Group:        Productivity/Editors/Other" >> $(SPEC)
	echo "Version:      $(VER)" >> $(SPEC)
	echo "URL:          http://towo.net/mined/" >> $(SPEC)
	echo "Summary:      $(SUMMARY)" >> $(SPEC)
	echo "" >> $(SPEC)
	echo "%description" >> $(SPEC)
	sed -e "s,^,	," DESCR >> $(SPEC)
	echo "" >> $(SPEC)


distribution:	release lsm

.PHONY: release

dist:	release

release:	src/version.h archive
	./makemined checksrc
	./makemined checkchar

archive:
	$(MAKE) generate
	$(MAKE) all
	$(MAKE) DESCR
	$(MAKE) release/mined-$(VER).tar.gz

release/mined-$(VER).tar.gz:	src/mined usrshare/help/mined.hlp doc/mnemodoc.html man/*.1
	./makemined pc
	./makemined dj
	./makemined vms
	./makearch


#############################################################################
# specific distribution packages

packages:	cygwin

REL=$(shell sed -e 's,.*package *\([0-9]*\).*,\1,' -e 's,^$$,0,' -e q README.cygwin )

rel:
	echo cygwin package release $(REL)

cygrel=release/cygwin

cygwin:
	$(MAKE) release/mined-$(VER).tar.gz
	$(MAKE) cygwinbin
	$(MAKE) cygwinsrc

cygwinsrc:
	echo making cygwin source package release $(REL) of mined $(VER)
	mkdir -p $(cygrel)/
	gunzip -c release/mined-$(VER).tar.gz | bzip2 -c > $(cygrel)/mined-$(VER)-$(REL)-src.tar.bz2

cygwinbin:	$(cygrel)/setup.hint cygwinexe
	echo making cygwin package release $(REL) of mined $(VER)
	cd src; rm -fr tmp.cyg; mkdir -p tmp.cyg
	cd src; $(MAKE) -f makefile.cygwin install root=tmp.cyg
	cd src/tmp.cyg; tar -cf mined-$(VER)-$(REL).tar *
	cd src/tmp.cyg; bzip2 mined-$(VER)-$(REL).tar
	mv src/tmp.cyg/mined-$(VER)-$(REL).tar.bz2 $(cygrel)/

cygwinexe:
	cd src; $(MAKE) -f makefile.cygwin mined.exe

$(cygrel)/setup.hint:	description.medium makefile
	mkdir -p $(cygrel)
	echo '# cygwin setup file' > $(cygrel)/setup.hint
	echo 'sdesc: $(SUMMARY)' >> $(cygrel)/setup.hint
	echo 'category: Editors' >> $(cygrel)/setup.hint
	echo 'requires: cygwin' >> $(cygrel)/setup.hint
	echo 'ldesc: "' >> $(cygrel)/setup.hint
	cat description.medium >> $(cygrel)/setup.hint
	echo '"' >> $(cygrel)/setup.hint

$(LSM):	DESCR makefile # release/mined-$(VER).tar.gz
	mkdir -p release
	echo $(VER)
	echo "Begin4" > $(LSM)
	echo "Title:		mined" >> $(LSM)
	echo "Version:	$(VER)" >> $(LSM)
	echo "Entered-date:	`date +%Y-%m-%d`" >> $(LSM)
	echo "Description:	$(SUMMARY)" >> $(LSM)
	sed -e "s,^,	," -e "s,:,;,g" DESCR >> $(LSM)
	echo "Keywords:	text editor, Unicode editor, UTF-8 editor, CJK editor" >> $(LSM)
	echo "Author:		mined@towo.net (Thomas Wolff)" >> $(LSM)
	echo "Primary-site:	http://towo.net/mined/" >> $(LSM)
	(cd release; ls -l mined-$(VER).tar.gz) | sed -e "s,[^ ]* *[^ ]* *[^ ]* *[^ ]* *\([^ ]*\).* \([^ ]*\)$$,		\1 \2," >> $(LSM)
	echo "Alternate-site:	http://www.ibiblio.org/pub/Linux/apps/editors/mined-$(VER).tar.gz" >> $(LSM)
	echo "Platforms:	Unix (Linux/Sun/HP/BSD/Mac and more), DOS (djgpp), Windows (cygwin)" >> $(LSM)
	echo "Copying-policy:	GPL with comments" >> $(LSM)
	echo "End" >> $(LSM)

# NetBSD pkgsrc entry
DESCR:	doc/overview.html descr.sed
	sed -f descr.sed doc/overview.html | sed -e "/./ b" -e "$$ d" > DESCR


# PC binary archives (development target, doesn't currently work locally)
bin:	winst bindj

win:	winst

dos:	src/dj/mined.exe bindj

src/dj/mined.exe:
	mkdir src/dj || true
	cp src/MINED.EXE src/dj/mined.exe

winst:	release/MinEd-$(VER)-install.exe

bincyg:	release/mined-$(VER)-windows.zip

bindj:	release/mined-$(VER)-dos.zip

winenv=src/win/cygwin1.dll src/win/cygwin-console-helper.exe src/win/mintty.exe src/win/dash.exe src/win/regtool.exe src/win/cygpath.exe

release/MinEd-$(VER)-install.exe:	release/mined-$(VER)-windows.zip
	rm -fr release/tmp.win
	mkdir -p release/tmp.win
	sed -e "s,%version%,$(VER)," makewinx.cfg > release/tmp.win/mined.SED
	cd release/tmp.win; unzip ../mined-$(VER)-windows.zip
	cd release/tmp.win; iexpress /n mined.SED
	mv -f release/tmp.win/MinEd-$(VER)-install.exe release/

release/mined-$(VER)-windows.zip:	mined src/win/mined.exe $(winenv) usrshare/help/mined.hlp README.windows
	mkdir -p release
	cd src/win; chmod +x *.exe cyg*.dll
	cd src/win; zip mined-$(VER)-windows.zip *.exe cyg*.dll
	mv -f src/win/mined-$(VER)-windows.zip release/
	cd usrshare/help; zip ../../release/mined-$(VER)-windows.zip mined.hlp
	cd usrshare/setup_install; zip ../../release/mined-$(VER)-windows.zip mined.ico
	cd usrshare/setup_install/win; zip ../../../release/mined-$(VER)-windows.zip *.*
	cp -fp README.windows release/README.txt
	cp -fp /usr/share/locale/locale.alias release/
	cd release; zip mined-$(VER)-windows.zip README.txt locale.alias
	cd release; rm -f README.txt locale.alias

src/win/mined.exe:	src/mined.exe
	cd src; $(MAKE) -f makefile.cygwin win/mined.exe

src/win/mintty.exe:
	cd src; $(MAKE) -f makefile.cygwin win/mintty.exe

src/win/cygwin1.dll:	/usr/bin/cygwin1.dll
	cp -fp /usr/bin/cygwin1.dll src/win/

src/win/cygwin-console-helper.exe:	/usr/bin/cygwin-console-helper.exe
	cp -fp /usr/bin/cygwin-console-helper.exe src/win/

src/win/dash.exe:	/usr/bin/dash.exe
	cp -fp /usr/bin/dash.exe src/win/

src/win/regtool.exe:	/usr/bin/regtool.exe
	cp -fp /usr/bin/regtool.exe src/win/

src/win/cygpath.exe:	/usr/bin/cygpath.exe
	cp -fp /usr/bin/cygpath.exe src/win/

release/mined-$(VER)-dos.zip:	src/dj/mined.exe usrshare/help/mined.hlp README.dos
	mkdir -p release
	cd src/dj; chmod +x mined.exe
	cd src/dj; zip mined-$(VER)-dos.zip mined.exe
	mv -f src/dj/mined-$(VER)-dos.zip release/
	cd usrshare/help; zip ../../release/mined-$(VER)-dos.zip mined.hlp
	cp -fp README.dos release/mined.txt
	cd release; zip mined-$(VER)-dos.zip mined.txt
	rm -f release/mined.txt

# Development repository archive (development target)
TARDIR=$(shell basename ${PWD})

tarup:
	cd ..; tar cvfz $(TARDIR).tgz --exclude=$(TARDIR)/attic --exclude=$(TARDIR)/src/etc/unidata --exclude=$(TARDIR)/src/etc/charinput --exclude="*.o" --exclude="*/mined" $(TARDIR)


#############################################################################
# end
