#!/bin/bash

# Silently exit if the changes are already applied
[ -f $1/filter/imagetopdf.c ] && exit 0

# Copy files
cp filter/imagetopdf.c $1/filter/
cp filter/pdftoijs.cxx $1/filter/
cp filter/texttopdf.c $1/filter/
cp filter/pdfutils.h $1/filter/
cp filter/pdfutils.c $1/filter/
cp filter/test_pdf1.c $1/filter/
cp filter/test_pdf2.c $1/filter/
cp conf/imagetopdf.convs $1/conf/
cp conf/imagetopdf.types $1/conf/
cp conf/pdftopdf.convs $1/conf/
cp conf/pdf.types $1/conf/
cp conf/texttopdf.convs $1/conf/
cp data/pdf.utf-8.heavy $1/data/
cp data/pdf.utf-8.simple $1/data/
cp config-scripts/cups-pdf-filters.m4 $1/config-scripts/

# Copy directories
cp -r filter/fontembed $1/filter/
cp -r pdftopdf $1
cp -r pdftoopvp $1

cd $1

# Edit configure script
cp configure.in configure.in.pdf-filters
cp configure configure.pdf-filters
cp config.h.in config.h.in.pdf-filters
perl -p -i -e 's/(sinclude\(config-scripts\/cups-defaults\.m4\))/$1\nsinclude(config-scripts\/cups-pdf-filters.m4)/' configure.in
aclocal
autoconf
perl -p -i -e 's:(\#endif\s*\/\*\s*\!_CUPS_CONFIG_H_\s*\*\/):

/* CUPS Version is 1.1 */
\#undef CUPS_1_1

/* CUPS Version is 1.4 or newer */
\#undef CUPS_1_4

/* Use libjpeg instead of builtin jpeg decoder. */
\#undef ENABLE_LIBJPEG

/* Use zlib instead of builtin zlib decoder. */
\#undef ENABLE_ZLIB

/* Have FreeType2 include files */
\#undef HAVE_FREETYPE_H

/* Define to 1 if you have the <inttypes.h> header file. */
\#undef HAVE_INTTYPES_H

/* Define to 1 if you have the "cups" library (-lcups). */
\#define HAVE_LIBCUPS 1

/* Define to 1 if you have the <memory.h> header file. */
\#undef HAVE_MEMORY_H

/* have new GfxFontType */
\#undef HAVE_NEW_GFX_FONTTYPE

/* Define to 1 if you have the "popen" function. */
\#undef HAVE_POPEN

/* Define to 1 if you have the <stdint.h> header file. */
\#undef HAVE_STDINT_H

/* Define to 1 if you have the <dirent.h> header file, and it defines "DIR".
   */
\#undef HAVE_DIRENT_H

/* Define to 1 if you have the <ndir.h> header file, and it defines "DIR". */
\#undef HAVE_NDIR_H

/* Define to 1 if you have the <sys/dir.h> header file, and it defines "DIR".
   */
\#undef HAVE_SYS_DIR_H

/* Define to 1 if you have the <sys/ndir.h> header file, and it defines "DIR".
   */
\#undef HAVE_SYS_NDIR_H

/* Define to 1 if you have the <zlib.h> header file. */
\#undef HAVE_ZLIB_H

/* Parser\:\:Parser has two arguments. */
\#undef PARSER_HAS_2_ARGS

/* Define to 1 if you have the "poppler" library (-lpoppler). */
\#undef HAVE_LIBPOPPLER

/* New font type enumeration */
\#undef FONTTYPE_ENUM2

/* GlobalParams\:\:GlobalParams has a argument. */
\#undef GLOBALPARAMS_HAS_A_ARG

/* Have Stream\:\:getUndecodedStream */
\#undef HAVE_GETUNDECODEDSTREAM

/* Have UGooString.h */
\#undef HAVE_UGOOSTRING_H

/* Old CharCodeToUnicode\:\:mapToUnicode */
\#undef OLD_MAPTOUNICODE

/* Old GfxColorSpace\:\:parse */
\#undef OLD_CS_PARSE

/* Poppler uses CMS */
\#undef USE_CMS

/* Poppler data dir */
\#undef POPPLER_DATADIR

\n$1:' config.h.in

# Edit Makefiles
cp conf/Makefile conf/Makefile.pdf-filters
cp filter/Makefile filter/Makefile.pdf-filters
cp data/Makefile data/Makefile.pdf-filters
cp Makefile Makefile.pdf-filters
cp Makedefs.in Makedefs.in.pdf-filters
perl -p -i -e 's/^(\s*REPLACE\s*=.*)$/$1 imagetopdf.convs imagetopdf.types pdftopdf.convs pdf.types texttopdf.convs/' conf/Makefile
perl -p -i -e 's/^(\s*FILTERS\s*=\s+)/$1imagetopdf pdftoijs texttopdf /' filter/Makefile
perl -p -i -e 's/^(\s*OBJS\s*=\s+)/$1imagetopdf.o pdftoijs.o texttopdf.o pdfutils.o /' filter/Makefile
perl -p -i -e 's/^(\s*CHARSETS\s*=\s+)/$1pdf.utf-8.heavy pdf.utf-8.simple /' data/Makefile
cat >> filter/Makefile <<EOF

#
# imagetops
#

imagetopdf:	imagetopdf.o common.o \$(LIBCUPSIMAGE) \\
		../cups/\$(LIBCUPS)
	echo Linking \$@...
	\$(CC) \$(LDFLAGS) -o \$@ imagetopdf.o common.o \$(LINKCUPSIMAGE) \\
		\$(IMGLIBS) \$(LIBS)


#
# pdftoijs
#

pdftoijs:	pdftoijs.o ../cups/\$(LIBCUPS)
	echo Linking \$@...
	\$(CXX) \$(LDFLAGS) -o \$@ pdftoijs.o \\
		\$(POPPLER_LIBS) \$(IJS_LIBS)


#
# texttopdf
#

fontembed/libfontembed.a:
	\$(MAKE) -C fontembed

texttopdf:	texttopdf.o textcommon.o common.o pdfutils.o fontembed/libfontembed.a \\
		../cups/\$(LIBCUPS)
	echo Linking \$@...
	\$(CC) \$(LDFLAGS) -o \$@ texttopdf.o textcommon.o common.o pdfutils.o -Lfontembed -lfontembed \$(LIBS)

EOF
perl -p -i -e 's/^(\s*DIRS\s*=.*\s+filter\s+)/$1pdftoopvp /' Makefile
perl -p -i -e 's/^(\s*DIRS\s*=.*\s+filter\s+)/$1pdftopdf /' Makefile
perl -p -i -e 's/^(\s*LIBS\s*=.*$)/$1\nPOPPLER_LIBS\t=\t\@POPPLER_LIBS\@ \$(LIBS)/' Makedefs.in
perl -p -i -e 's/^(\s*LIBS\s*=.*$)/$1\nIJS_LIBS\t=\t\@IJS_LIBS\@ \$(LIBS)/' Makedefs.in
perl -p -i -e 's/^(\s*LIBS\s*=.*$)/$1\nFREETYPE_LIBS\t=\t\@FREETYPE_LIBS\@ \$(LIBS)/' Makedefs.in
perl -p -i -e 's/^(\s*LIBS\s*=.*$)/$1\nFONTCONFIG_LIBS\t=\t\@FONTCONFIG_LIBS\@ \$(LIBS)/' Makedefs.in
perl -p -i -e 's/^(\s*ALL_CFLAGS\s*=\s*)/$1-Ifontembed /' Makedefs.in
perl -p -i -e 's/^(\s*ALL_CXXFLAGS\s*=\s*)/$1-Ioprs -Iopvp -I..\/opvp -I..\/.. /' Makedefs.in
