# Makefile for xloadimage.  this replaces the multiple makefiles used in
# versions prior to xloadimage 2.00.
#
# WARNING: if you use gcc 1.37, there is an optimizer bug which causes GIF
# images to come out black and colormap reduction to fail.  Use gcc-1-37 as
# your make target if you have gcc 1.37.

# Include system configuration parameters
include Make.conf

CFLAGS=$(OPT_FLAGS) $(CC_FLAGS) $(CC_CONFIG_FLAGS) $(X11_INC_DIR) \
  -DSYSPATHFILE=\"$(SYSPATHFILE)\"
LIBS=$(X11_LIB_DIR) $(X11_LIB_NAME) $(SYS_LIBS) -lm

# stuff that should eventually make the configuration file
SYSPATHFILE=/usr/lib/X11/Xloadimage

default: configure$(CONFIG_STYLE)
	$(MAKE) xloadimage
	$(MAKE) uufilter

# this target gets hit automatically on the first run; it sets up
# the Make.conf file and recursively calls make.

autoconfig: autoconfig.c
	$(CC) -g -o autoconfig autoconfig.c

# manual configuration target
configure:: autoconfig
	./autoconfig
	$(MAKE) configure-libraries

# this is the default target for the initial compilation
configure-auto:: autoconfig
	./autoconfig
	$(MAKE) configure-libraries

# target that doesn't do anything	
configure-skip::
	@echo "Already configured, skipping configuration process."

configure-clean:: autoconfig
	./autoconfig -clean
	echo "# this will contain information generated by libconfig" > jpeg.conf
	echo "# this will contain information generated by libconfig" > tiff.conf
	cd jpeg ; make clean
	cd tiff ; make clean

# specific configurations
configure-bsd:: autoconfig
	./autoconfig -bsd
	$(MAKE) configure-libraries

configure-sysv:: autoconfig
	./autoconfig -sysv
	$(MAKE) configure-libraries

# this program configures optional libraries
configure-libraries::
	$(CC) $(CC_FLAGS) -g -o libconfig libconfig.c
	./libconfig "$(CC)" "$(OPT_FLAGS)"

MISC= Imakefile Makefile Makefile.std VMS_BUILD.COM README build-imake \
      build-vms build-info buildshar.c packtar.c xloadimage.man \
      xloadimage.txt autoconfig.c Make.conf libconfig.c \
      makefloppy jpeg.c tiff.c uufilter.man uufilter.txt uufilter.c \
      xloadimagerc

# files for the loader library
LOADER_HDRS= cmuwmraster.h fbm.h gif.h imagetypes.h img.h \
             kljcpyrght.h mac.h mcidas.h mit.cpyrght mrmcpyrght.h \
             niff.h pbm.h pcx.h pds.h rle.h sunraster.h xwd.h tgncpyrght.h 
LOADER_SRCS= cmuwmraster.c faces.c fbm.c gif.c imagetypes.c img.c \
             mac.c mcidas.c mc_tables.c pbm.c pcx.c pdsuncomp.c \
             rle.c rlelib.c sunraster.c vff.c vicar.c xbitmap.c xpixmap.c \
             xwd.c
LOADER_OBJS= ${LOADER_SRCS:.c=.o}

# files for the image library
IMAGE_HDRS= copyright.h image.h
IMAGE_SRCS= new.c niff.c value.c zio.c
IMAGE_OBJS= ${IMAGE_SRCS:.c=.o}

# files for the image processing library
PROCESS_HDRS=
PROCESS_SRCS= bright.c clip.c compress.c dither.c fill.c halftone.c \
              merge.c reduce.c rotate.c smooth.c undither.c zoom.c
PROCESS_OBJS= ${PROCESS_SRCS:.c=.o}

OPTIONAL_OBJS= ${OPTIONAL_SRCS:.c=.o}

# files for xloadimage (X display utility)
X_HDRS= options.h patchlevel xloadimage.h
X_SRCS= config.c misc.c options.c root.c send.c window.c \
        xloadimage.c
X_OBJS= ${X_SRCS:.c=.o}

INCS= $(IMAGE_HDRS) $(LOADER_HDRS) $(PROCESS_HDRS) $(X_HDRS)
SRCS= $(IMAGE_SRCS) $(LOADER_SRCS) $(PROCESS_SRCS) $(X_SRCS) $(OPTIONAL_SRCS)
OBJS= $(IMAGE_OBJS) $(LOADER_OBJS) $(PROCESS_OBJS) $(X_OBJS) $(OPTIONAL_OBJS)

ALL= $(MISC) $(INCS) $(SRCS)

.c.o: Make.conf xloadimage.h image.h $*.c
	$(CC) -c $(CFLAGS) $*.c

jpeg.o: Make.conf image.h
	$(CC) -c $(CFLAGS) -Ijpeg $*.c

uufilter: uufilter.c
	$(CC) -o uufilter $(CFLAGS) uufilter.c

xloadimage: $(OBJS) $(OPTIONAL_LIBS)
	./build-info
	$(CC) -c $(CFLAGS) build.c
	$(CC) -o xloadimage $(CFLAGS) build.o $(OBJS) $(OPTIONAL_LIBS) $(LIBS)

install:: $(SYSPATHFILE) uufilter
	$(RM) $(INSTALLDIR)/xloadimage
	$(RM) $(INSTALLDIR)/xsetbg
	$(RM) $(INSTALLDIR)/xview
	$(RM) $(INSTALLDIR)/uufilter
	$(CP) xloadimage $(INSTALLDIR)/xloadimage
	$(LN) $(INSTALLDIR)/xloadimage $(INSTALLDIR)/xsetbg
	$(LN) $(INSTALLDIR)/xloadimage $(INSTALLDIR)/xview
	$(CP) uufilter $(INSTALLDIR)/uufilter

clean::
	rm -f autoconfig
	make configure-clean
	rm -f *.o *~ xloadimage uufilter autoconfig libconfig packtar \
	  buildshar doshar shar.* *.tar *.tar.Z *.tc

$(SYSPATHFILE):
	@echo "*** Creating default $(SYSPATHFILE) since you"
	@echo "*** don't have one. This file is used to set up default places"
	@echo "*** and names to look for images.  You probably want to edit"
	@echo "*** it for your site.  See the xloadimage manual page for"
	@echo "*** details on the contents of this file."
	cp xloadimagerc $(SYSPATHFILE)

# this is for building Imakefiles and such for distributions

new-auxiliaries:: clean
	rm -f Imakefile Makefile.std VMS_BUILD.COM xloadimage.txt
	cp Makefile Makefile.std # backup in case they want it
	./build-imake "$(SRCS)" "$(OBJS)"
	./build-vms "$(SRCS)" "$(OBJS)"
	./default-info
	nroff -Tcrt -man xloadimage.man > xloadimage.txt
	nroff -Tcrt -man uufilter.man > uufilter.txt

# these targets are for building shar distributions

buildshar: buildshar.o
	$(CC) -o buildshar buildshar.o

shar:: buildshar new-auxiliaries
	rm -f shar.* doshar
	./buildshar $(ALL) > doshar
	sh doshar
	rm -f doshar Makefile.std

# targets for building tar distributions

packtar: packtar.c
	$(CC) -o packtar packtar.c

tar:: new-auxiliaries packtar
	./packtar $(ALL) sample/* jpeg/* tiff/*
	rm -f Imakefile Makefile.std

tar.Z: tar
	compress xloadimage.tar

# create a floppy-disk distribution.

floppy: new-auxiliaries
	./makefloppy $(ALL) sample
	./makefloppy tiff
	./makefloppy jpeg
	rm -f Imakefile Makefile.std

# these targets are for those of us who have CodeCenter/Saber-C

# load all objects in CodeCenter.

cl_obj: $(DEBUG_OBJS)
	#setopt ccargs -g $(CC_FLAGS) $(X11_INC_DIR) -DSYSPATHFILE=\\\"$(SYSPATHFILE)\\\"
	#setopt load_flags $(CC_FLAGS) $(X11_INC_DIR) -DSYSPATHFILE=\\\"$(SYSPATHFILE)\\\"
	#load build.o $(OBJS) $(OPTIONAL_LIBS) $(LIBS)
	#link

# load all sources in CodeCenter.

cl_src:
	#setopt ccargs -g $(CC_FLAGS) $(X11_INC_DIR) -DSYSPATHFILE=\\\"$(SYSPATHFILE)\\\"
	#setopt load_flags $(CC_FLAGS) $(X11_INC_DIR) -DSYSPATHFILE=\\\"$(SYSPATHFILE)\\\"
	#load build.c $(SRCS) $(OPTIONAL_LIBS) $(LIBS)
	#link

# proof xloadimage using TestCenter.

xloadimage.tc:
	proof -errors $(CC) -o xloadimage.tc build.o $(CFLAGS) $(OBJS) $(OPTIONAL_LIBS) $(LIBS)

jpeg/libjpeg.a:
	cd jpeg ; $(MAKE) libjpeg.a

tiff/libtiff.a:
	cd tiff ; $(MAKE) libtiff.a

# a few dependencies
imagetypes.o: imagetypes.h imagetypes.c

