#
# Makefile for NWS EMWIN Data stream processing software
#
# (should look familiar to anyone who's looked at the Linux Makefile)
#
# Note! Dependencies are done automagically by 'make dep', which also
# removes any old dependencies. DON'T put your own dependencies here
# unless it's something special (ie not a .c file).

all:    do-it-all

#
# Make "config" the default target if there is no configuration file or
# "depend" the target if there is no top-level dependency information.
#
ifeq (.config,$(wildcard .config))
include .config
ifeq (.depend,$(wildcard .depend))
include .depend
do-it-all:      serport.o emwinser utf2ps sattopgm emwinmcr emwinmcs
	@echo If you have latex and dvips, type \"make docs\"
else
CONFIGURATION = depend
do-it-all:      version.h depend
endif
else
CONFIGURATION = config
do-it-all:      version config
endif

config:
	@echo Making .config file...
	@echo `uname`=`uname -r` > .config
	@echo COMPILEHOST=`uname -n` >> .config

version: dummy
	rm -f version.h
 
version.h: Makefile
	@echo \#define COMPILATION_TIME \"`date`\" > version.h
	@echo \#define COMPILATION_HOST \"`uname -a`\" >> version.h
	@echo \#define COMPILATION_USER \"`whoami`\" >> version.h
	@echo \#define COMPILATION_TYPE \"$(CC) $(CFLAGS)\" >> version.h

OBJS	=  emwin.o utf.o sattopgm.o serport.o emwinmcr.o emwinmcs.o
SRCS	=  emwin.c utf.c sattopgm.c serport.c emwinmcr.c emwinmcs.c
BINDIR	=  /usr/bin
SBINDIR	=  /usr/sbin
DOCDIR	=  /usr/share/doc/emwin
INFODIR	=  /usr/info
INSTALL = /usr/bin/install -c
INSTALL_PROGRAM = $(INSTALL)
INSTALL_DATA = $(INSTALL) -m 644

ifdef SunOS
CFLAGS=-Wall -O6 -msupersparc
XINCLUDES=-I/usr/openwin/include
XLIBS=-lXaw3d -lXt -lXmu -lX11
RANLIB=echo
endif

ifdef Linux
#ENDIAN=-DLITTLE_ENDIAN
XINCLUDES=-I/usr/X11R6/include
XLIBS=-L/usr/X11R6/lib -lXaw -lXt -lXmu -lX11
CFLAGS=-Wall
endif

#libwxp.a: $(OBJS) 
#	$(AR) $(ARFLAGS) $@ $(OBJS) 
#	$(RANLIB) $@

docs: h5v6p2.ps

utf: utf.c version.h
	$(CC) -DSTANDALONE $(CFLAGS) -o utf utf.c

utf2ps: utf2ps.c version.h
	$(CC) $(CFLAGS) -o utf2ps utf2ps.c

xutf: utf.c xutf.c version.h
	$(CC) $(CFLAGS) $(XINCLUDES) -o xutf xutf.c utf.c $(XLIBS)

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

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

emwinmcs: emwinmcs.c serport.o
	$(CC) $(CFLAGS) -o emwinmcs emwinmcs.c serport.o

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

serport: serport.c
	$(CC) $(CFLAGS) -DSTANDALONE -o serport serport.c

emwinser: version.h serport.o
	$(CC) $(CFLAGS) -o emwinser emwinser.c serport.o

h5v6p2.ps: h5v6p2.tex
	latex h5v6p2.tex
	latex h5v6p2.tex
	dvips -o h5v6p2.ps h5v6p2.dvi

installdirs:
	mkdir -p $(DESTDIR)/$(BINDIR)
	mkdir -p $(DESTDIR)/$(SBINDIR)
	mkdir -p $(DESTDIR)/$(DOCDIR)
#	mkdir -p $(DESTDIR)/$(INFODIR)

install: installdirs emwinmcr emwinser utf2ps sattopgm docs
	$(INSTALL_PROGRAM) emwinser $(DESTDIR)/$(SBINDIR)/emwinser
	$(INSTALL_PROGRAM) emwinmcr $(DESTDIR)/$(SBINDIR)/emwinmcr
	$(INSTALL_PROGRAM) emwinmcs $(DESTDIR)/$(SBINDIR)/emwinmcs
	$(INSTALL_PROGRAM) utf2ps $(DESTDIR)/$(BINDIR)/utf2ps
	$(INSTALL_PROGRAM) sattopgm $(DESTDIR)/$(BINDIR)/sattopgm
	$(INSTALL_DATA) README $(DESTDIR)/usr/share/doc/emwin/README
	$(INSTALL_DATA) README.Debian $(DESTDIR)/usr/share/doc/emwin/README.Debian
	$(INSTALL_DATA) README.mcast $(DESTDIR)/usr/share/doc/emwin/README.mcast
	$(INSTALL_DATA) h5v6p2.ps $(DESTDIR)/usr/share/doc/emwin/h5v6p2.ps
	$(INSTALL_DATA) h5v6p2.tex $(DESTDIR)/usr/share/doc/emwin/h5v6p2.tex
	$(INSTALL_DATA) AUTHORS $(DESTDIR)/usr/share/doc/emwin/AUTHORS
	$(INSTALL_DATA) TODO $(DESTDIR)/usr/share/doc/emwin/TODO
	$(INSTALL_DATA) THANKS $(DESTDIR)/usr/share/doc/emwin/THANKS

depend:
	echo Makeing .depend file...
	$(CPP) $(CFLAGS) -M $(SRCS) > .depend

dummy:

dataclean:
	rm -f *.TXT.*
	rm -f *.NWS.*
	rm -f *.CC1.*
	rm -f *.ZAP.*
	rm -f GPH*
	rm -f *.plot

clean: dataclean
	rm -f emwinser emwinmcr emwinmcs utf utf2ps xutf sattopgm version.h
	rm -f core
	rm -f *.ps
	rm -f *.dvi
	rm -f *.aux
	rm -f *.log
	rm -f *.o
	rm -f *.a
	rm -f *~

reallyclean: clean
	rm -f .config* .dep*

backup: reallyclean
	cd .. && tar cf - 163.35 | gzip -9 > 163.35.tar.gz
