# $Id: Makefile,v 2.22 2009/06/11 14:12:46 teus Exp $
# #
# Makefile for installing and distributing cap and coap php package
# CAcert copyright under GPL
#
## this file should be named Makefile.pdf
## make args: install, uninstall, clean
## make options: make -e INSTALLDIR=/chroot/www/www

# where the php runtime "binaries" reside uncomment on target machine
#INSTALLDIR	= /chroot/www/www
#TARGET	= root@test1.cacert.at

# as long as form is draft provide background message
WATERMARK=only DRAFT version

# where needed shell commands reside, be secure
ifeq "$(TARGET)" ""
TARGET	= localhost
REMOTESH=
else
REMOTESH= ssh $(TARGET)
endif
GET	= /usr/bin/co
#WFLAGS	= -q -T 10 -t 5
WFLAGS	=  -q
WGET	= /usr/bin/wget $(WFLAGS)
RM	= /bin/rm
SED	= /bin/sed
LN	= /bin/ln -s
CP	= /bin/cp
TEST	= $(shell if [ -x /bin/test ] ; then echo /bin/test; else echo /usr/bin/test ; fi)
TAR	= $(shell if $(TEST) -x /bin/tar ; then echo /bin/tar; else echo /usr/bin/tar ; fi)
DIRNAME	= $(shell if $(TEST) -x /bin/dirname ; then echo /bin/dirname; else echo /usr/bin/dirname ; fi)
BASENAME= $(shell if $(TEST) -x /bin/basename ; then echo /bin/basename; else echo /usr/bin/basename ; fi)
PHPC	= /usr/bin/php
MAKE	= /usr/bin/make
CHMOD	= /bin/chmod
MKDIR	= /bin/mkdir
MV	= /bin/mv
FIND	= /usr/bin/find
TOUCH	= /bin/touch
MAKE	= /usr/bin/make
ifeq "$(shell $(REMOTESH) which rsync)" ""
RSYNC	= /usr/bin/ssh
else
RSYNC	= /usr/bin/rsync
endif
SCP	= /usr/bin/scp
UNZIP	= /usr/bin/unzip
PATCH	= /usr/bin/patch
CONVERT	= ./php5-php4.sh
ifeq "$(shell $(REMOTESH) which $(UNZIP))" ""
NOZIP	= yes
# if unzip is not available convert zip to tgz for target machine
UNZIP	= $(TAR) xpzf
Z	:= tgz
else
NOZIP	= no
Z	:= zip
endif

# if php version is php4 we do some special work on install
PHP	= $(shell $(REMOTESH) $(PHPC) -v 2>/dev/null | $(SED) -e "s/PHP[ \t]*//" -e "s/\..*//" -e q)
ifeq "$(PHP)" ""
PHP	= $(shell $(REMOTESH) "/usr/bin/strings /chroot$(PHPC) | /bin/grep X-Powered-By" | $(SED) -e "s@.*PHP/@@" -e "s/\..*//" -e q)
endif
ifeq "$(PHP)" "4"
PHP	= php4
else
PHP	= php
endif

SOURCES	= transtab.php	# utf8 transliteration package (needs utf8 core)
SOURCES += $(foreach type,pdf html,cap.$(type).php) # CAP form
SOURCES += $(foreach type,pdf html,coap.$(type).php) # COAP form
SOURCES += $(foreach lib,css js test fies prnt pdf html,form_$(lib).php) # CAP/COAP generation general fies

# CAcert logos used here. Download them from svn tree on install
### eps files or due to eps problem not yet used however
LOGOS	= CAcert-logo-colour-1000.png  CAcert-logo-mono-1000.png \
	  CAcert-logo-colour.eps CAcert-logo-mono.eps
URLLOGO = http://svn.cacert.org/CAcert/PR/Logos

OUT	= /dev/null
ERR	= /dev/stderr

####
# tcpdf package: generates pdf file
# TCPDF package the zapfdingbats font may be generated from ITC ttf font file
### tcpdf is rather in development. Check for new version regularly and test them first
T	= tcpdf
# http://sourceforge.net/project/showfiles.php?group_id=128076&package_id=140403&release_id=651282
ifeq "$(PHP)" "php4"
# Last working version with php4
TVER	= 4_5_027
else
# this version has PCRE preg_match problems 2009-06-11
TVER	= 4_6_015
endif
TCPDFSRC:= $(T)_$(TVER).$(Z)
TCPDFURL:= http://sourceforge.net/project/downloading.php?filename=$(T)_$(TVER).zip
TC	:= $(T)/config
TCONF 	:= $(TC)/$(T)_config_alt.php $(TC)/$(T)_config.php
TCL	:= $(TC)/lang
TLANG	:= $(TCL)/eng.php
TF	= $(T)/fonts
TCPDFDIRS:= $(T) $(TC) $(TCL) $(TF)

# need to embed in pdf zapf dingbats font
#ZAPF	= Zapf-Dingbats-BT
#ZAPFURL	:= http://www.fontstock.net/font/11050/$(ZAPF).zip
ZAPF	= ITC_Zapf_Dingbats_Medium
ZAPFURL = http://www.free-fonts-ttf.org/true-type-fonts/download.php?action=zip&image_id=3438
ifeq "$(NOZIP)" "yes"
SOURCES += $(ZAPF).ttf
endif
# extra embedded font, some pdf readers do not have it
ZAPFFONT:= zapfdingbats
fonts   = symbol
fonts   += courier
fonts   += helvetica $(foreach t,b bi i,helvetica$(t))
fonts   += times $(foreach t,b bi i,times$(t))
EMBED   = freesans $(foreach t,b bi i,freesans$(t))
EMBED   += $(ZAPFFONT)
TFONTS  := $(patsubst %,$(TF)/%.php,$(fonts)) \
        $(foreach ext,ctg.z z php,$(patsubst %,$(TF)/%.$(ext),$(EMBED)))

TCPDFLIB=  $(T)/barcodes.$(PHP) $(T)/2dbarcodes.$(PHP) $(T)/htmlcolors.php \
        $(T)/LICENSE.TXT $(T)/$(T).$(PHP) $(T)/unicode_data.php \
        $(TCONF) $(TLANG) $(TFONTS)

ifeq ($(PHP),php4)
# html_entity_decode_php4.php originates from $(T)_$(TVER)_php4.zip file
# this file defines a routine to convert html char encoding to unicode chars for php4
TCPDFLIB += $(T)/html_entity_decode_php4.php
TCPDFLIB += $(T)/tcpdf.$(PHP) $(T)/barcodes.$(PHP) $(T)/2dbarcodes.$(PHP)
endif
ifeq "$(NOZIP)" "yes"
SOURCES	+= html_entity_decode_php4.php $(T)_$(TVER).$(Z)
endif
# handle patches obtained from diff -c file.origin file
# add bookmarks for html <a> anchers: ANCHER
# add limit on spacing on html tags: vert SPACE
# TCPDFTAG disables <tcpdf php methods code insertion
TCPDF_P	= ANCHER SPACE TCPDFTAG
SOURCES += $(foreach t,$(TCPDF_P),$(T).php.$(t).diff)

####
# utf8 unicode a very few routines are needed This may overlap with code from tcpdf
UTF8	= utf8
UVER	= 0.5
UTF8SRC	:= $(UTF8)-$(UVER).tar.gz
UTF8URL := http://downloads.sourceforge.net/phputf8/$(UTF8)-$(UVER).tar.gz
UTF8LIB := $(UTF8)/native/core.php $(UTF8)/utils/unicode.php
UTF8DIRS:= $(UTF8) $(UTF8)/native $(UTF8)/utils
#SOURCES +=  $(UTF8SRC)

##
SOURCES	+= ./php5-php4.sh Makefile README

####
# needed runtime php bin and libraries
LIBS	= transtab.php $(TCPDFLIB) $(UTF8LIB)
LIBS    += $(foreach lib,css js test fies prnt html,form_$(lib).php) form_pdf.$(PHP)
# runtime php "binaries"
BINS	= $(foreach type,cap coap,$(type).pdf.$(PHP)) # CAP form
BINS    += $(foreach type,cap coap,$(type).html.php) # COAP form

# where the php runtime "binaries" reside; define default values
ifeq ($(INSTALLDIR),)
ROOT	= bin
BINDIR	= .
LIBDIR	= .
else
ROOT	= /chroot/
BINDIR	= $(subst //,/,$(subst $(ROOT),/,$(INSTALLDIR)))
LIBDIR	= $(subst //,/,$(BINDIR)/phpmyadmin/libraries)
# disable TEST data flag
SS	:= //
endif
# where the runtime logos reside
LOGODIR = $(subst /./,/,$(subst //,/,$(ROOT)/$(BINDIR)/logos))
# where runtime library packages reside
LIBDIRS	:= $(addprefix $(ROOT)/$(LIBDIR)/,$(TCPDFDIRS)) $(addprefix $(ROOT)/$(LIBDIR)/,$(UTF8DIRS))


all:	$(SOURCES)

# logos import
%.png %.eps:
	$(WGET) -O $@ $(URLLOGO)/`$(BASENAME) $@`

# utf8 unicode
$(UTF8LIB) : $(UTF8)

$(UTF8)	: $(UTF8SRC)
	@$(TAR) xpzf $(UTF8SRC)

$(UTF8SRC) :
	$(WGET) -O $@ "$(UTF8URL)"

# next is just for completeness. Transtab alternative. Package is not complete.
utf8_to_ascii-0.3.tar.gz:
	$(WGET) -O $@ http://downloads.sourceforge.net/phputf8/utf8_to_ascii-0.3.tar.gz

# tcpdf
$(TCPDFLIB) : $(T)

$(TCPDFSRC) :
	@echo "Visit http://sourceforge.net/project/tcpdf and use manual download"
	@ echo "E.g. try" $(WGET) -O $@ "$(TCPDFURL)"
	exit 1

# set tcpdf source dir ready
$(T)	: $(TCPDFSRC)
	@$(RM) -rf $(T)
	@$(UNZIP) $(TCPDFSRC) 2>$(ERR) >$(OUT)
	@if $(TEST) $(PHP) = php4 ; then $(CP)  -f html_entity_decode_php4.php $(T) ; fi
	@if $(TEST) $(PHP) = php4 ; then $(CONVERT) $(T)/*.php ; fi
	@$(FIND) $(T) -type f -a -exec $(CHMOD) ug-x {} \;
	@$(MV) $(T) $(T)_$(TVER) ; ln -s $(T)_$(TVER) $(T)
	@for F in $(TCPDF_P) ; do if $(TEST) ! -f $(T)/$${F}.diff ; then echo patch $(T)/$(T).php with $(T).php.$${F}.diff ; $(PATCH) -N -s -V t -b $(T)/$(T).php <$(T).php.$${F}.diff >$(ERR)  2>$(ERR); $(CP) $(T).php.$${F}.diff $(T)/$${F}.diff; fi; done

# font  file generation
$(ZAPF).ttf:
	$(WGET) -O $(ZAPF).zip "$(ZAPFURL)"
	@/usr/bin/unzip $(ZAPF).zip $@ >$(OUT) ; $(TOUCH) $@
	@$(RM) -f $(ZAPF).zip

$(ZAPFFONT).ttf : $(ZAPF).ttf
	@$(LN) $< $@

$(TF)/$(ZAPFFONT).ctg.z $(TF)/$(ZAPFFONT).php $(TF)/$(ZAPFFONT).z: $(ZAPFFONT).tgz
	@cd $(TF); $(TAR) xpzf ../../$< `$(BASENAME) $@`

$(ZAPFFONT).php : $(ZAPFFONT).z

$(ZAPFFONT).ctg.z : $(ZAPFFONT).z

$(ZAPFFONT).tgz : $(ZAPFFONT).z $(ZAPFFONT).ctg.z $(ZAPFFONT).php
	@$(TAR) czf $@ $^
	@$(RM) -f $^

%.ufm : %.ttf $(TF)/utils/src/ttf2pt1
	@$(TF)/utils/src/ttf2pt1 -a -F $(basename $@).ttf  2>$(OUT) >$(OUT)
	@$(RM) -f $(TF)/utils/src/ttf2pt1

$(TF)/utils/src/ttf2pt1 : $(TF)/utils/src/ttf2ufm-src
	@cd $< ; $(CHMOD) +x scripts/unhtml; $(MAKE) -s all; $(CP) -u ttf2pt1 ..
	@$(RM) -rf $<

$(TF)/utils/src/ttf2ufm-src : $(T)
	@cd $(TF)/utils/src ; $(TAR) xzf ttf2ufm-src.tar.gz 2>$(ERR) >$(OUT);

$(TF)/utils/makefont.php: $(T)

%.z : %.ttf %.ufm $(TF)/utils/makefont.php
	@$(PHPC) -q $(TF)/utils/makefont.php $(basename $@).ttf $(basename $@).ufm 2>$(ERR) >$(OUT)
	@$(RM) -f $(basename $@).{ttf,afm,ufm,t1a}

### installation instructions
# various needed installation directories
$(ROOT)/$(BINDIR) $(LIBDIRS) $(LOGODIR):
	@$(MKDIR) -p $@

installbin : $(ROOT)/$(BINDIR) $(BINS)
	@echo "Install php binaries in: $(subst /./,/,$(subst //,/,$(ROOT)/$(BINDIR)))"
	@for F in $(BINS); do FT=`echo "$$F" | $(SED) "s/.php[45]$$/.php/"`; if $(TEST) -f $(ROOT)/$(BINDIR)/$$FT -a ! -f $(ROOT)/$(BINDIR)/$$FT.orig ; then $(CP) -f $(ROOT)/$(BINDIR)/$$FT $(ROOT)/$(BINDIR)/$$FT.orig; fi;$(SED) -e "s@^define([ ]*.TEST@$(SS)&@" -e "/^define([ ]*.RT.[ ]*,/s@,.*@,'$(LIBDIR)');@" -e "/^\/\*define([ ]*.WATERMARK/s@^/.define.*,.*);@define('WATERMARK','$(WATERMARK)');@" "$$F" >$(ROOT)/$(BINDIR)/"$$FT"; echo "	$(subst /./,/,$(subst //,/,$(ROOT)/$(BINDIR)/$$FT))"; done 

installlib : $(LIBDIRS) $(LIBS)
	@echo "Install php fonts, libraries in: $(subst /./,/,$(subst //,/,$(ROOT)/$(LIBDIR)))"
	@for F in $(LIBS); do FT=`echo "$$F" | $(SED) "s/.php[45]$$/.php/"`; $(CP) -f "$$F" $(ROOT)/$(LIBDIR)/"$$FT"; echo "	$(subst /./,/,$(subst //,/,$(ROOT)/$(LIBDIR)/$$FT))"; done

install : test installbin  installlib $(LOGODIR) $(addprefix $(LOGODIR)/,$(LOGOS)) CAP_COAP_distr.tgz

tcpdf_$(TVER)_php4.zip:
	$(WGET) -O $@ "$(patsubst .zip,_php4.zip,$(TCPDFURL))"

html_entity_decode_php4.php: tcpdf_$(TVER)_php4.zip
	$(UNZIP) tcpdf_$(TVER)_php4.zip tcpdf_php4/$@
	$(CP) tcpdf_php4/$@ $@
	$(RM) -rf tcpdf_php4

# php4 conversion
%.php4	: %.php
	@$(CONVERT) $< >$(OUT) 2>$(ERR)

# clean up but leave imported files
clean	:
	@$(RM) -rf $(ZAPFFONT).{z,ct.z,php,ufm,afm,t1a,ttf} $(T) $(UTF8) $(T)_$(TVER)
	@$(RM) -f *.php[45] {cap,coap}.{pdf,html} /tmp/{cap,coap}.{pdf,html}

# back to preinstall
uninstall :
	$(RM) -rf $(BINDIR)/{cap,coap}.{pdf,html}.php $(LIBDIR)/{$(T),transtab.php,$(UTF8)} $(foreach lib,css js test fies prnt pdf html,$(LIBDIR)/form_$(lib).php)
	for file in $(BINDIR)/{cap,coap}.{pdf,html}.php ; do if $(TEST) -f $$file.orig ; then $(MV) $$file.orig $$file; fi; done
	$(RM) -rf $(ZAPFFONT).tgz logos

# for those machines without unzip prepare this. This should be recursive...
%.tgz	: %.zip
	@$(MKDIR) @; cd @ ; $(UNZIP) ../$(basename $@).zip >$(OUT) 2>$(ERR)
	@cd @; for F in `$(FIND) . -name "*.zip"` ; do A=`pwd` ; cd `$(DIRNAME) $$F`; $(MKDIR) @; cd @ ; $(UNZIP) ../`$(BASENAME) $$F` 2>$(ERR) >$(OUT); $(RM) -f ../`$(BASENAME) $$F`; $(TAR) czf ../`$(BASENAME) $$F .zip`.tgz .; cd .. ; $(RM) -rf @ ; cd $$A; done
	@cd @; $(TAR) czf ../$(notdir $@) . ; cd ..; $(RM) -rf @


# the zip files are unzipped and tar re-archived. The target machine has no unzip utility
distr	: CAP_COAP_distr.tgz

# check prerequisites for installation: unzip and php4
test:
	@echo Use for runtime installation e.g.: make -e INSTALLDIR=/chroot/www/www -e TARGET=user@remote install
	@if $(TEST) $(PHP) = php4 ; then echo WARNING: $(TARGET) is a $(PHP) machine. Conversion is done from php5 to php4 ; fi
	@if $(TEST) $(Z) = tgz ; then echo WARNING: No unzip command on $(TARGET) present. Zip files are converted and included. ; fi

# simple check if things are properly ready for use on this machine
check: 
	cd $(ROOT)/$(BINDIR); FORM=empty $(PHPC) ../cap.pdf.php >/tmp/cap_en.pdf ; /usr/bin/file /tmp/cap_en.pdf; $(PHPC) ../cap.html.php >/tmp/cap_en.html; echo DO: $(RM) -f /tmp/cap_en.{pdf,html}
	@cd $(ROOT)/$(BINDIR); FORM=example $(PHPC) ../cap.pdf.php >/tmp/cap_example.pdf ; /usr/bin/file /tmp/cap_en.pdf; $(PHPC) ../cap.html.php >/tmp/cap_example.html; echo DO: $(RM) -f /tmp/cap_example.{pdf,html}
	@cd $(ROOT)/$(BINDIR); FORM=empty $(PHPC) ../coap.pdf.php >/tmp/coap_en.pdf ; /usr/bin/file /tmp/coap_en.pdf;  $(PHPC) ../coap.html.php >/tmp/coap_en.html; echo DO: $(RM) -f /tmp/coap_en.{pdf,html}
	@cd $(ROOT)/$(BINDIR); FORM=example $(PHPC) ../coap.pdf.php >/tmp/coap_example.pdf ; /usr/bin/file /tmp/coap_example.pdf;  $(PHPC) ../coap.html.php >/tmp/coap_example.html; echo DO: $(RM) -f /tmp/coap_example.{pdf,html}
	
ifeq "$(shell $(REMOTE) which unzip)" ""
CAP_COAP_distr.tgz: tcpdf_$(TVER).tgz
endif
CAP_COAP_distr.tgz: $(SOURCES) html_entity_decode_php4.php
	@$(TAR) czf $@ $^

#Makefile: CAP_COAP_distr.tgz
#	@$(TAR) xpzf $^ $@

rsync: 
	$(MAKE) -e ROOT=$(subst //,/,./$(ROOT)) -e BINDIR=$(subst //,/,./$(BINDIR)) -e LIBDIR=$(subst //,/,./$(LIBDIR))  install
	@for F in $(subst //,/,./$(ROOT)/$(BINDIR)/{cap,coap}.{pdf,html}.php) ; do $(CP)  -f $$F $$F.bak ; $(SED) -e "/^define( *'RT'/s@^define( *'RT'.*) *;@define('RT','$(subst //,/,$(LIBDIR))');@" $$F.bak >$$F ; $(RM) -f $$F.bak; done
ifeq "$(RSYNC)" "/usr/bin/rsync"
	cd $(subst //,/,./$(ROOT)); $(RSYNC) -ravb . $(TARGET):$(ROOT)
else
	@echo "No rsync available at $(TARGET), so use scp and ssh."
	@cd  $(subst //,/,./$(ROOT)); tar czf /tmp/CAP_COAP_bins.tgz .
#	@$(SCP) /tmp/CAP_COAP_bins.tgz $(TARGET):/tmp/CAP_COAP_bins.tgz
	$(RSYNC) $(TARGET) "cd $(ROOT) ; tar xvz --owner root -f /tmp/CAP_COAP_bins.tgz; $(RM) /tmp/CAP_COAP_bins.tgz"
endif
	@$(RM) -rf $(subst //,/,$(sort ./$(ROOT)/$(BINDIR) ./$(ROOT)/$(LIBDIR))) /tmp/CAP_COAP_bins.tgz
	@echo "This might leave directory $(ROOT) made and not removed."