#   Makefile for psutils on Windows (This is not portable)
#
#   Copyright 2013 Akira Kakuto.
#
#   This library is free software; you can redistribute it and/or
#   modify it under the terms of the GNU Lesser General Public
#   License as published by the Free Software Foundation; either
#   version 2.1 of the License, or (at your option) any later version.
#
#   This library is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#   Lesser General Public License for more details.
#
#   You should have received a copy of the GNU Lesser General Public License
#   along with this library; if not, see <http://www.gnu.org/licenses/>.

KPSEPARENT = ..
KPSEDIR = ../kpathsea
LIBPAPERDIR = ../../libs/libpaper/lib
KPSELIB = $(KPSEDIR)/kpathsealibw64.lib
LIBPAPER = $(LIBPAPERDIR)/libpaper.lib
CFLAGS = -nologo -wd4819 -favor:blend -DWIN32=1 -DHAVE_CONFIG_H \
-D_CRT_SECURE_NO_DEPRECATE -I. -I.. -I$(KPSEPARENT) -I$(KPSEDIR) \
-I$(LIBPAPERDIR) -MD -O2

libpsutils_SOURCES = psutil.c psspec.c progname.c paper-size.c verror.c

LDADD = libpsutils.lib $(KPSELIB) $(LIBPAPER)

PROGRAMS = epsffit.exe psbook.exe psselect.exe pstops.exe psnup.exe psresize.exe

SCRIPTS_WRAPPERS = psjoin.exe extractres.exe includeres.exe

MANS = psutils.1 psbook.1 psselect.1 pstops.1 epsffit.1 psnup.1 \
psresize.1 psjoin.1 extractres.1 includeres.1

PSDOCS = psutils.ps psbook.ps psselect.ps pstops.ps epsffit.ps psnup.ps \
psresize.ps psjoin.ps extractres.ps includeres.ps

PDFDOCS = psutils.pdf psbook.pdf psselect.pdf pstops.pdf epsffit.pdf psnup.pdf \
psresize.pdf psjoin.pdf extractres.pdf includeres.pdf

all: $(PROGRAMS) $(SCRIPTS_WRAPPERS) $(PDFDOCS)

#
# bins from C
#
psbook.exe: psbook.obj $(LDADD)
	$(CC) -Fe$@ psbook.obj $(LDADD)
psselect.exe: psselect.obj $(LDADD)
	$(CC) -Fe$@ psselect.obj $(LDADD)
pstops.exe: pstops.obj $(LDADD)
	$(CC) -Fe$@ pstops.obj $(LDADD)
epsffit.exe: epsffit.obj $(LDADD)
	$(CC) -Fe$@ epsffit.obj $(LDADD)
psnup.exe: psnup.obj $(LDADD)
	$(CC) -Fe$@ psnup.obj $(LDADD)
psresize.exe: psresize.obj $(LDADD)
	$(CC) -Fe$@ psresize.obj $(LDADD)

#
# script wrappers
#
SCRIPTBIN=../../texk/calldll/runscr.exe
psjoin.exe: $(SCRIPTBIN)
	cp -p $(SCRIPTBIN) $@
extractres.exe: $(SCRIPTBIN)
	cp -p $(SCRIPTBIN) $@
includeres.exe: $(SCRIPTBIN)
	cp -p $(SCRIPTBIN) $@

#
# library
#
LIBOBJS = psutil.obj psspec.obj progname.obj paper-size.obj verror.obj

libpsutils.lib: $(LIBOBJS)
	lib -out:$@ $(LIBOBJS)

.c.obj:
	$(CC) $(CFLAGS) -c $<

config.h: config.h.msvc
	cp config.h.msvc $@

#
# documents
#
epsffit.pdf: epsffit.ps
	distill $<
epsffit.ps: epsffit.1
	groff -mandoc -Tps $< >$@

extractres.pdf: extractres.ps
	distill $<
extractres.ps: extractres.1
	groff -mandoc -Tps $< >$@

includeres.pdf: includeres.ps
	distill $<
includeres.ps: includeres.1
	groff -mandoc -Tps $< >$@

psbook.pdf: psbook.ps
	distill $<
psbook.ps: psbook.1
	groff -mandoc -Tps $< >$@

psjoin.pdf: psjoin.ps
	distill $<
psjoin.ps: psjoin.1
	groff -mandoc -Tps $< >$@

psnup.pdf: psnup.ps
	distill $<
psnup.ps: psnup.1
	groff -mandoc -Tps $< >$@

psresize.pdf: psresize.ps
	distill $<
psresize.ps: psresize.1
	groff -mandoc -Tps $< >$@

psselect.pdf: psselect.ps
	distill $<
psselect.ps: psselect.1
	groff -mandoc -Tps $< >$@

pstops.pdf: pstops.ps
	distill $<
pstops.ps: pstops.1
	groff -mandoc -Tps $< >$@

psutils.pdf: psutils.ps
	distill $<
psutils.ps: psutils.1
	groff -mandoc -Tps $< >$@

INSTALLBINDIR=c:/usr/work/edrive/wk/w64dist/PSUTILS/bin64/
ZZ=c:/usr/local/txdir/w32tex/bin64/

install: $(PROGRAMS) $(SCRIPTS_WRAPPERS)
	cp -p $(PROGRAMS) $(SCRIPTS_WRAPPERS) $(INSTALLBINDIR)
	cp -p $(PROGRAMS) $(SCRIPTS_WRAPPERS) $(ZZ)

clean::
	rm *~ *.obj *.exe *.lib
realclean::
	rm *~ *.obj *.exe *.lib *.ps *.pdf

include depend.mk
