#
# Makefile for creating dictionary installer
# for GNU ASpell 0.50 (c) Kevin Atkinson
#
# Author: Thorsten Maerz
# License: LGPL (as GNU Aspell)
#
# It creates a self extracting .zip file, which contains the dictionaries as
# well as an installer for Win32 and one for *nix.
# To extract the dictionaries on *nix, use the unzip command
# The dictionaries can also be installed on *nix: Extract the .exe file using
# "unzip", move into the TmpInstall dir and call "sh unix-install.sh".
# Installation for Windows should be self-explanatory.
#
# Requirements:
# * InfoZip's unzip550
#   Origin: ftp://ftp.info-zip.org/pub/infozip/src/unzip550.zip
#   Comment: Unzip55 needs to be patched to allow autorun without prompt, see
#            unzip-autorun-noprompt.diff
# * InfoZip's zip23
#   Origin: ftp://ftp.info-zip.org/pub/infozip/WIN32/zip23xN.zip
#   Comment: Other zippers may also work.
# * InnoSetup
#   Origin: http://www.jrsoftware.org/isinfo.php
#   Comment: Version 2 or later.
# * MinGW/MSys build environment
# * GNU-ASpell 0.50
# * Dictionaries for GNU-ASpell
#

PACKAGE=Aspell

ifdef DICTMAJOR
ifdef DICTMINOR
ifdef DICTMICRO
  DICTVER=$(DICTMAJOR).$(DICTMINOR)-$(DICTMICRO)
endif
endif
endif

ifndef DICTVER
  ISERROR=1
endif

ifndef DICTLANG
  ISERROR=1
endif

ifdef DICTLANGUAGE
  DICTNAME=$(PACKAGE) $(DICTLANGUAGE) Dictionary
else
  ISERROR=1
endif

LICENSE=Copying
COPYRIGHT=Copyright
UNIXINSTALL=unix-install.sh

# SFXNAME is the name of the zip/wrapper, that calls INSTALLERNAME
INSTALLERNAME=setup-$(PACKAGE)-$(DICTLANG)-$(DICTVER).exe
SFXNAME=$(PACKAGE)-$(DICTLANG)-$(DICTVER).exe

# installer creation
SETUPCOMP=start /wait "f:\Programme\My Inno Setup Extensions 3\Compil32.exe"
TMPSCRIPTIN=setup.iss.in

################################################################################

# installer creation
TMPSETUPSCRIPT=setup.iss
TMPMKSETUP=mk_setup.bat
TMPOUTDIR=Output
TMPSETUPEXE=Setup.exe
LICENSEFILE=.\\\\$(LICENSE)
INFOBEFOREFILE=.\\\\$(TMPINSTDIR)\\\\$(COPYRIGHT)

# zip / sfx creation
TMPZIPNAME=data.zip
TMPINSTBAT=tmpinst.bat
TMPZIPCOMMENT=zip.comment
SFXSTUB=unzipsfx.exe

TMPINSTDIR=TmpInstall

# command needed for make
CAT=cat
CP=cp
ECHO=echo
MKDIR=mkdir
RM=rm -rf
SED=sed
TAR=tar
UNIX2DOS=unix2dos
ZIP=zip -9

################################################################################

.PHONY: $(SFXNAME) $(TMPSETUPSCRIPT)

ifdef ISERROR
error:
	@$(ECHO) "Required parameters missing"
	@$(ECHO) "  (DICTMAJOR, DICTMINOR, DICTMICRO, DICTLANG, DICTLANGUAGE)"
	@$(ECHO) "Syntax:"
	@$(ECHO) "  make DICTMAJOR=0 DICTMINOR=50 DICTMICRO=2 DICTLANG=en DICTLANGUAGE=English"
else
all: dictionary installer $(SFXNAME)
endif

################################################################################

dictionary:
	-$(TAR) -xvjf $(PACKAGE)-$(DICTLANG)-$(DICTVER).tar.bz2
	-cd $(PACKAGE)-$(DICTLANG)-$(DICTVER); configure; make

################################################################################

installer: filecopy $(UNIXINSTALL) $(TMPSETUPEXE)

filecopy:
	-$(MKDIR) $(TMPINSTDIR)
	-$(MKDIR) $(TMPINSTDIR)/data
	-$(MKDIR) $(TMPINSTDIR)/dict
	-$(CP) $(PACKAGE)-$(DICTLANG)-$(DICTVER)/*.alias \
	    $(PACKAGE)-$(DICTLANG)-$(DICTVER)/*.multi \
	    $(PACKAGE)-$(DICTLANG)-$(DICTVER)/*.rws \
	    $(TMPINSTDIR)/dict
	-$(CP) $(PACKAGE)-$(DICTLANG)-$(DICTVER)/*.dat \
	    $(TMPINSTDIR)/data
	-$(CP) $(PACKAGE)-$(DICTLANG)-$(DICTVER)/$(LICENSE) \
	    $(PACKAGE)-$(DICTLANG)-$(DICTVER)/$(COPYRIGHT) \
	    $(TMPINSTDIR)

$(UNIXINSTALL):
	$(ECHO) '#!/bin/sh'				>  $(TMPINSTDIR)/$@
	$(ECHO) '# Installation for *nix users'		>> $(TMPINSTDIR)/$@
	$(ECHO) 'less $(COPYRIGHT)'			>> $(TMPINSTDIR)/$@
	$(ECHO) 'cp data/* `aspell config data-dir`'	>> $(TMPINSTDIR)/$@
	$(ECHO) 'cp dict/* `aspell config dict-dir`'	>> $(TMPINSTDIR)/$@

$(TMPSETUPSCRIPT):	
	$(SED) -c -e "s/@APPNAME@/$(DICTNAME)/; \
		   s/@LICENSEFILE@/$(LICENSEFILE)/; \
		   s/@INFOBEFOREFILE@/$(INFOBEFOREFILE)/; \
		   s/@PACKAGE@/$(PACKAGE)/; \
	   s/@DICTMAJOR@/$(DICTMAJOR)/; \
	   s/@DICTMINOR@/$(DICTMINOR)/; \
	   s/@DICTMICRO@/$(DICTMICRO)/; \
		   s/@DICTLANG@/$(DICTLANG)/;" \
		$(TMPSCRIPTIN) > $(TMPSETUPSCRIPT)

$(TMPSETUPEXE):	$(TMPSETUPSCRIPT)
	# if invoked directly, curdir is changed to msys/bin, pathnames use /c/dir syntax -> .iss is not found :(
	# --> create batchfile for this task and call it using cmd/command (otherwise handled by sh)
	$(ECHO) '$(SETUPCOMP) /cc $(TMPSETUPSCRIPT)' > $(TMPMKSETUP)
	$(UNIX2DOS) $(TMPMKSETUP)
	$(subst \,/,$(COMSPEC)) /c $(TMPMKSETUP)
	-$(MV) $(TMPOUTDIR)/$(TMPSETUPEXE) $(TMPINSTDIR)/$(INSTALLERNAME)
	-$(RM) $(TMPMKSETUP)
	-$(RM) $(TMPOUTDIR)
#	-$(RM) $(TMPSETUPSCRIPT)

################################################################################
#DOSECHO: create batchfiles with CRLF ending
$(SFXNAME):
	-$(RM) $(TMPZIPNAME)
	$(ZIP) -r $(TMPZIPNAME) $(TMPINSTDIR)
	# create install.bat (to clean up afterwards)
	$(ECHO) 'echo Starting $(TMPINSTDIR)\$(INSTALLERNAME)'	>  $(TMPINSTBAT)
	$(ECHO) 'start /wait $(TMPINSTDIR)\$(INSTALLERNAME)'			>> $(TMPINSTBAT)
	$(ECHO) 'echo Removing $(TMPINSTDIR)'			>> $(TMPINSTBAT)
#		$(ECHO) 'PAUSE' >> $(TMPINSTBAT)	
	$(ECHO) 'deltree /Y $(TMPINSTDIR)'				>> $(TMPINSTBAT)
#		$(ECHO) 'PAUSE' >> $(TMPINSTBAT)	
	$(ECHO) 'del %0'						>> $(TMPINSTBAT)
#		$(ECHO) 'PAUSE' >> $(TMPINSTBAT)	
	$(UNIX2DOS) $(TMPINSTBAT)
	$(ZIP) $(TMPZIPNAME) $(TMPINSTBAT)
	# create comment for autostart
	$(ECHO) '$$AUTORUN$$>$(TMPINSTBAT)' > $(TMPZIPCOMMENT)
	$(ZIP) -z $(TMPZIPNAME) < $(TMPZIPCOMMENT)
	# glue sfx stub and zip to final .exe
	$(CAT) $(SFXSTUB) $(TMPZIPNAME) > $(SFXNAME)

	#-$(RM) $(TMPINSTBAT) $(TMPZIPCOMMENT) $(TMPZIPNAME)
	#-$(RM) $(TMPINSTDIR)
