#!/bin/bash

Version=0.00
 Myname="${0##*/}"

:<<'DOC'
= X - Y
= Synopsis
X [options]

== Options
= Description
DOC

Red='\e[38;5;1m'
    die() { local i; for i; do echo -e "$Myname: $Red$i"; done 1>&2; exit 1; }

if [[ $(/sbin/ldconfig -v 2>/dev/null|grep -cE '(libolgx|libxview)') -ne 2 ]]; then
   die 'missing xview libraries; please install the following packages:
      - xviewg:     see https://packages.debian.org/en/jessie/xviewg
      - xviewg-dev: see https://packages.debian.org/en/jessie/xviewg-dev
   '
fi
install_directory='/usr/local'	# change this eventually
web_directory='/var/www'	# for html docs; needs SSI!
dots_per_inch=600		# dots/inch for the printer
hprint=2			# default symbol height for printer
hscreen=3			# default symbol height for screen
pixels_per_mm="$(xdpyinfo |sed -n 's/.*dimensions[^0-9]*\([0-9]\+\)x.*(\([0-9]\+\)x.*/\1\/\2/p')"
				# pixels/mm for the screen
needed='gs pnmcrop pdflatex convert '

for i in $needed; do
   command -v "$i" >/dev/null && needed=${needed/$i /}
done
[[ -z $needed ]] || die "Missing executables: $needed" 

[[ -d $install_directory ]] || die "$install_directory not found - please update configure"

cat <<-EOF >include/simpltfn.h
	#define SIMPLOTFONTDIR "$install_directory/simplot/fonts"
	#define DOTSPERINCH $dots_per_inch. // dots/inch for the printer
	#define SCREEN_PIXELS_PER_MM $pixels_per_mm // pixels/mm for the screen
	#define SYMHIG (P_driver==X?$hscreen:$hprint) // symbol height for printer and screen
EOF

mkdir -p -m 0755 lib

{ # following lines redirected to Makefile
cat <<EOF
INSTALLDIR=$install_directory
WEBDIR=$web_directory/simplot
PATH=/bin:/usr/bin
SHELL=/bin/bash

EOF

cat <<'EOF'
all:  lib simplter simpltps simplot xyplot simple manpages logo

nodoc:  lib simplter simpltps simplot xyplot simple

lib: src
	cd src; make lib

simplot: lib src/simplot.c
	cd src; make ot

logo:	doc/graphics/logo.tex doc/graphics/icondd.png
	cd doc/graphics; \
	lualatex logo.tex; \
	convert -density 600 -trim logo.pdf -quality 60 -flatten -sharpen 0x1.0 logo.jpg; \
	rm logo.{aux,pdf,log}

simple: lib src/simple.c
	cd src; make si

xyplot: lib src/xyplot.c
	cd src; make xy

simpltps: src/simpltps.c
	cd src; make ps

simplter: src/simplter.c
	cd src; make er

manpages:
	cd doc; \
	make; \
	cd ..;

install: all tar
	mkdir -p $(INSTALLDIR)/{lib,include,bin,simplot/{fonts,doc},simplot/html/graphics}; \
	cp -af lib/libsimplot.a $(INSTALLDIR)/lib; \
	cp -af include/simpl{ot,tsy}.h $(INSTALLDIR)/include; \
	cp -af bin/simpl{ot,ter,tps} bin/xyplot $(INSTALLDIR)/bin; \
	strip $(INSTALLDIR)/bin/simpl{ot,ter,tps} $(INSTALLDIR)/bin/xyplot; \
	mkdir -p $(WEBDIR); \
	rsync -a --delete --exclude='*.pdf' --exclude=CVS doc/{graphics,*.shtml,*.inc} $(WEBDIR)/; \
	cp -af simplot-*.tar.gz doc/simplot.css doc/manual.pdf xy $(WEBDIR)/; \
        mkdir -p $(INSTALLDIR)/simplot/fonts; \
	cp `find fonts -type f -print` $(INSTALLDIR)/simplot/fonts; \
        echo '#define SIMPLOTFONTDIR "$(INSTALLDIR)/simplot/fonts"' >$(INSTALLDIR)/include/simpltfn.h; \
	chmod -R a+r ${WEBDIR}; \
	chmod a+x ${WEBDIR}; \
	chmod a+x ${WEBDIR}/graphics;

uninstall:
	rm -rf $(INSTALLDIR)/simplot $(WEBDIR)
	rm $(INSTALLDIR)/lib/libsimplot.a
	rm $(INSTALLDIR)/include/simpl{ot,tsy}.h
	rm $(INSTALLDIR)/bin/simpl{ot,ter,tps}
	rm $(INSTALLDIR)/bin/xyplot

clean:
	rm -rf lib
	find doc/graphics -type f |grep -vE '(button.png|simplot.ico|icondd.png|logo.tex|logo.jpg)' | xargs rm -f
	find doc -name *.c|sed 's/\.c//' | xargs rm -f
	find doc -name *.shtml -not -name index.shtml | xargs rm -f
	rm -f src/*.o \
	   doc/{*.eps,manual.*} \
	   {.,doc,src}/Makefile \
	   bin/{simpl{e,ot,tps,ter},xyplot} \
	   include/simpltfn.h

tar:
	v=`grep = include/version.h|tr -dc "[0-9.]"`; \
	tmp=/tmp/simplot-$$v; \
	wd=`pwd`; \
	mkdir -p $$tmp; \
	rm -rf $$tmp/*; \
	cp -a * $$tmp; \
	cd $$tmp; \
	make clean; \
	cd ..; \
	tar czf $$wd/simplot-$$v.tar.gz simplot-$$v; \
	rm -r simplot-$$v 

EOF
} > Makefile

{ # following many lines redirected to doc/Makefile
echo "PATH=.:../bin:$PATH"
cat <<'EOF'
export SIMPLOTFONTDIR=../fonts
export TEXINPUTS=.::
LIBDIR  = -L../lib
LIBS    = -lsimplot -lxview -lolgx -lX11 -lm
DBXFLAGS= -g -Wall
INCLUDES= -I../include
CFLAGS  = $(DBXFLAGS) $(INCLUDES) -Dlinux
LDFLAGS	= $(LIBDIR) $(LIBS)
.SUFFIXES : .c .o .sim .doc .eps .png .shtml

.c:	
	gcc -o $@.o $(CFLAGS) -c $<
	gcc -o $@ $@.o $(LDFLAGS) 
	rm $@.o

.sim.eps: ../bin/simplot
	../bin/simplot $<

.doc.shtml:
	../bin/doc2html $<

manual:	manpages
	../bin/doc2latex

manpages: \
EOF
cd doc || die 'Could not chdir to doc'
ls -x ./*.doc |expand|sed 's/$/ \\/;s/\.doc/.shtml/g'

echo
echo '# dependencies of manpages on program texts and eps-files'
for i in *.doc; do
  ( echo "$i:"; grep -E '(^FILE |^PNG)' "$i" ) |
    sed -E '
       :a
       N
       s/PNG (.*)/graphics\/\1.png/g
       s/FILE (.*)/\1/g
       s/\n/ /g
       ta
    ' |
    grep -v ":$"
done

echo
echo '# dependencies of png files'
for i in *.c; do
   [[ $i == plformat.c ]] && continue # treated separately below
   b=${i%.c}
   file=$(
      grep plinit "$i" |
      head -1 |
      sed 's/[^"]*"\([^"]*\).*/\1/'
   )
   if [[ -z $file ]]; then
      echo "graphics/$b.png: $b; $b > $b.eps; ../bin/p2g $b.eps"
   else
      echo "graphics/$file.png: $b; $b; ../bin/p2g $file.eps"
   fi
done
for i in *.sim; do
   b=${i%.sim}
   file=$(grep plinit "$i" | awk '{print $3}')
   echo "graphics/$file.png: $i; $i; ../bin/p2g $file.eps"
done
for i in H S ZCMI ZD A AB T; do
   echo "graphics/$i.png: plformat; plformat; ../bin/p2g $i.eps"
done
cat <<EOF
graphics/xy.png:
	../bin/xyplot -b -x 'East Longitude' -y Latitude -t 'Dutch cities' ../xy
	convert -crop 0x0 ../xy.pdf graphics/xy.png
	mv ../xy.pdf graphics/xy.pdf
EOF
} > doc/Makefile

cd ..

{ # following lines redirected to src/Makefile
echo "PATH=../bin:$PATH"
cat <<'EOF'
export SIMPLOTFONTS=..
LIBDIR  = -L../lib
LIBS    = -lsimplot -lxview -lolgx -lX11 -lm
INCLUDE	= -I../include
CFLAGS  = -g -Wall $(DBXFLAGS) -Dlinux $(INCLUDE)
CPPFLAGS  = -g -Dlinux -Wall $(DBXFLAGS) $(INCLUDE)
LDFLAGS = $(LIBDIR) $(LIBS)

include Objects

all:  lib er ps ot xy
er: ../bin/simplter
ps: ../bin/simpltps
ot: ../bin/simplot
xy: ../bin/xyplot
si: ../bin/simple
lib: ../lib/libsimplot.a

../lib/libsimplot.a: $(OBJ)
	ar rv ../lib/libsimplot.a $?
	ranlib ../lib/libsimplot.a

../bin/simplot:  simplot.o  lib; gcc -o $@ simplot.o  $(LDFLAGS)
../bin/xyplot:   xyplot.o   lib; gcc -o $@ xyplot.o   $(LDFLAGS)
../bin/simple:   simple.o   lib; gcc -o $@ simple.o   $(LDFLAGS)
../bin/simpltps: simpltps.o lib; gcc -o $@ simpltps.o $(LDFLAGS)
../bin/simplter: simplter.o lib; gcc -o $@ simplter.o $(LDFLAGS)

clean:
	rm -f *.o *~ core *.bak 
depend:
	gcc -I../include -M *.c >>Makefile

# DO NOT DELETE
EOF
} > src/Makefile

cd src || die 'Could not cd to src'
make depend
