# -*-Makefile-*-
# Makefile fragment to generate makefile.all and update version.c

# Non-core sources and version.c
noncoresrc = ansi2knr.c bf_test.c corplot.c gpexecute.c gplt_x11.c \
os9.c strftime.c version.c vms.c

coresrc := $(filter-out $(noncoresrc),$(wildcard *.c))

coreterm := $(shell cd ../term && echo *.trm)

version := $(shell cat ../VERSION)
file_version := $(shell sed -n 's/.*gnuplot_version.*"\(.*\)".*/\1/p' ./version.c|tr -d ' ')

patchlevel := $(shell cat ../PATCHLEVEL)
file_pl := $(shell sed -n 's/.*gnuplot_patchlevel.*"\(.*\)".*/\1/p' ./version.c|tr -d ' ')

last_change := $(shell dd if=../ChangeLog bs=11 count=1 2> /dev/null)

date := $(shell date)

makefile.all: Makefile.maint
	@echo "# $@ generated automatically by GNU make" >$@t
	@echo >>$@t
	@echo '# List of core object files except version.$$(O)' >>$@t
	@echo COREOBJS = $(coresrc:.c=.'$$(O)') \
	 | fmt | (tr '\012' @; echo) | sed 's/@$$/%/;s/@/ \\@/g' \
	 | tr @% '\012\012' >>$@t
	@echo "# List of terminal driver sources" >>$@t
	@echo CORETERM = $(patsubst %.trm,$$\(T\)%.trm,$(coreterm)) \
	 | fmt | (tr '\012' @; echo) | sed 's/@$$/%/;s/@/ \\@/g' \
	 | tr @% '\012\012' >>$@t
	@if cmp -s $@ $@t; then rm -f $@t; else mv $@t $@; fi

makefile.awc: makefile.all
	sed -e 's/\\/\&/g' makefile.all > $@

version.c: ../VERSION ../PATCHLEVEL ../ChangeLog
	@echo Making $@
	@if [ "$(version)" != "$(file_version)" -o \
	     "$(patchlevel)" != "$(file_pl)" ]; then \
	  sed -e '/^const char gnuplot_version/ s/"[^"]*"/"@VERS@"/' \
	      -e '/^const char gnuplot_patchlevel/ s/"[^"]*"/"@PLVL@"/' \
	      -e '/^const char gnuplot_date/ s/"[^"]*"/"@DATE@"/' \
	      -e "s/@VERS@/$(version)/" -e "s/@PLVL@/$(patchlevel)/" \
	      -e "s/@DATE@/$(date)/" $@ >$@t; \
	  if cmp -s $@ $@t; then rm -f $@t; else mv $@t $@; fi \
	fi
#	Take most recent change date from the very top of ChangeLog
	sed -e '/^const char gnuplot_date/ s/"[^"]*"/"$(last_change)"/' \
	    $@ > $@t;
	mv $@t $@
