SHELL = /bin/sh

.SUFFIXES:
# If COMPRESSION is not zero the executable will also work as
# a CGI-script. The number you specify will be passed to gzip
# as compression mode, if your browser can unzip it.
#
# It is recommended to use a not so high compression mode
# for gzip because otherwise the load on the web server
# grows without having big benefits.
# COMPRESSION = -DCOMPRESSION=4
COMPRESSION = -DCOMPRESSION=1

# Dont change things beyond this line.
TARGET          = @PROJECT_NAME@
VERSION         = @VERSION@
SRCS            = @srcdir@/mymain.c @srcdir@/colors.c
HDRS            = @srcdir@/colors.h @srcdir@/mymain.h 
LEXSRCS         = @srcdir@/$(TARGET).l
CONFIGS         = ./config.status Makefile config.h
MANPAGE	        = $(TARGET).1
LSM             = $(TARGET).lsm
DOCS            = AUTHORS COPYING NEWS README $(MANPAGE) $(LSM)
CONFIGIN        = Makefile.in configure.in configure install-sh config.h.in
TMPCONFIGFILES  = config.cache config.status config.log config.h
FILENAME        = $(TARGET)-$(VERSION)

CC              = @CC@
LEX             = @LEX@
LEXLIB          = @LEXLIB@
LEXOUTPUT       = @LEX_OUTPUT_ROOT@.c
INSTALL         = @INSTALL@
INSTALL_PROGRAM = @INSTALL_PROGRAM@
INSTALL_DATA    = @INSTALL_DATA@
prefix          = /usr/local
exec_prefix     = ${prefix}
bindir          = @bindir@
mandir          = @mandir@
man1dir         = @mandir@/man1
srcdir          = @srcdir@
CPPFLAGS        = @CPPFLAGS@

CFLAGS += -O2 -Wall
ALL_CFLAGS = $(CFLAGS) -I$(srcdir) $(COMPRESSION) 

all: $(TARGET)

$(TARGET): $(CONFIGIN) $(CONFIGS) $(LEXOUTPUT) $(SRCS) $(HDRS) 
	$(CC) $(ALL_CFLAGS) -o $(TARGET) $(LEXOUTPUT) $(SRCS) $(LEXLIB)

$(LEXOUTPUT): $(CONFIGS) $(LEXSRCS) $(HDRS)
	$(LEX) $(LEXSRCS)

install: $(TARGET)
	@echo "Installing" $(TARGET) "to" $(bindir)
	-mkdir -p $(bindir) $(man1dir)
	$(INSTALL_PROGRAM) $(TARGET) $(bindir)/$(TARGET)
	$(INSTALL_DATA) $(srcdir)/$(MANPAGE) $(man1dir)/$(MANPAGE)

install-strip: $(TARGET)
	@echo "Installing" $(TARGET) "to" $(bindir)
	-mkdir -p $(bindir) $(man1dir)
	$(INSTALL_PROGRAM) -s $(TARGET) $(bindir)/$(TARGET)
	$(INSTALL_DATA) $(srcdir)/$(MANPAGE) $(man1dir)/$(MANPAGE)

uninstall:
	rm $(bindir)/$(TARGET)
	rm $(man1dir)/$(MANPAGE)

clean:
	rm -rf *.o *~ $(LEXOUTPUT) TAGS

distclean:
	rm -rf *.o *~ Makefile $(LEXOUTPUT) $(TARGET) $(TMPCONFIGFILES) TAGS

# stuff to update Makefile when changing configuration

$(srcdir)/configure: $(srcdir)/configure.in
	cd $(srcdir) && autoconf

Makefile: $(srcdir)/Makefile.in $(srcdir)/config.h.in
	@echo "regeneration with (in ./config.status) saved configure results..."
	./config.status
	@echo
	@echo Please rerun make so it will use the updated Makefile.
	exit 1

./config.status: $(srcdir)/configure
	$(srcdir)/configure
	@echo
	@echo Please rerun make so it will use the updated Makefile.
	exit 1

# stuff to create a distribution
# also updates the .lsm file with version and size of archive
dist: $(CONFIGIN) $(CONFIGS) $(LEXSRCS) $(SRCS) $(HDRS) $(DOCS)
#	@echo This only works if you have not changed
#	@echo the version number directly before.
#	@echo If so, start a make without parameters first.
	@echo
	@echo Will create
	@echo $(FILENAME).tar.gz
	@echo
	@echo 'Type CTRL-C to abort (you have 5 seconds)!'
	@sleep 5  
	@echo "updating" $(TARGET).lsm
	@rm -rf $(FILENAME)
	@mkdir $(FILENAME)
	@cp $(LEXSRCS) $(SRCS) $(HDRS) $(DOCS) $(CONFIGIN) $(FILENAME)
	@tar -czf $(FILENAME).tar.gz $(FILENAME)
	@sed -e "s/^Version:.*/Version:        $(VERSION)/" \
	    -e "s/^Entered-date:.*/Entered-date:   `date \
			'+%d%b%y' | tr '[a-z]' '[A-Z]'`/" \
	    -e "s/$(TARGET)-.*.tar.gz/$(FILENAME).tar.gz/" \
	    -e "s/[0-9]*kB $(FILENAME).tar.gz/`du \
		 -k $(FILENAME).tar.gz\
		  | cut -f1`kB $(FILENAME).tar.gz/" \
	    < $(TARGET).lsm > $(FILENAME)/$(TARGET).lsm
	@cp $(FILENAME)/$(TARGET).lsm .
	@echo "creating" $(FILENAME).tar.gz
	@tar -czf $(FILENAME).tar.gz $(FILENAME)
