# There are two possible compilation modes of the Vbrsr package: by
# linking with librt, which contains an emulation of POSIX timers based
# on threads, or by linking with libposixtime, a library that is part of
# the High Resolution Timers kernel patch by Georg Azinger.
#
# If you want to link against libposixtime, which is only possible if
# the HRT kernel patch is applied, you need to `make hrt'.  Else, you
# need to `make rt'.
#
# In practice, you usually just `make': if this is the first time you
# compile Vbrsr, then this is the same as `make rt' (no HRT).  If you
# have already compiled Vbrsr, then it compiles as it did last time.
# For example:
#
# First time compilation: `make' --> compile without HRT
# subsequent times:       `make' --> compile without HRT
# want to change:         `make hrt' --> compile with HRT
# subsequent times:       `make' --> compile with HRT
# want to change:         `make rt' --> compile without HRT
# subsequent times:       `make' --> compile without HRT

REMOTE_HOSTS = passero cardellino pettirosso falco martin oca

CFLAGS = -O99 -g -Wall -std=gnu99

all last:
	@if [ -e hrt.time -a -e rt.time ]; then \
	    if [ hrt.time -nt rt.time ]; \
	        then $(MAKE) --no-print-directory hrt; \
	        else $(MAKE) --no-print-directory rt; \
	    fi; \
	else \
	    touch -t 200001010101 rt.time hrt.time; \
	    $(MAKE) clean rt; \
	fi

export

rt: LDFLAGS = -lm -liw -lrt -lpcap -lncurses 
rt: CPPFLAGS = -DHRT=0
rt: THISSTAMP = rt.time
rt: OTHERSTAMP = hrt.time

hrt: LDFLAGS = -lm -liw -lposixtime -lpcap -lncurses 
hrt: CPPFLAGS = -DHRT=1
hrt: THISSTAMP = hrt.time
hrt: OTHERSTAMP = rt.time

rt hrt:
	$(MAKE) --no-print-directory progs
	sleep 1; touch ${THISSTAMP}

progs: send receive

wapi.o send.o receive.o: ${OTHERSTAMP} vbrsr.h rcvwin.h

send receive: wapi.o

clean:
	rm --force {wapi,send,receive}{,.o}

tags:
	etags --members wapi.c send.c receive.c vbrsr.h rcvwin.h

dist: vbrsr.tgz
	cp vbrsr.tgz ~ftp/pub/software/linux

upgrade: vbrsr-upgrade.tgz
	@echo -e \\n\\tThis is `hostname`: start ---------------
	@rm -rf upgrade vbrsr
	@tar -zxf vbrsr-upgrade.tgz
	@mv vbrsr upgrade
	@cp --force --preserve=mode,timestamps upgrade/* .
	$(MAKE) --no-print-directory last
	@echo -e \\t`hostname` finished ---------------\\n

remote-upgrade: vbrsr.tgz
	if [ -d /var/nettmp ]; then cp vbrsr.tgz /var/nettmp; fi
	for host in ${REMOTE_HOSTS}; do ssh -l root $$host su - gabriele '"make -C vbrsr upgrade"'; done

remote-makefile:
	for host in ${REMOTE_HOSTS}; do scp Makefile root@$$host:/home/gabriele/vbrsr && ssh -l root $$host chown gabriele.gabriele /home/gabriele/vbrsr/Makefile; done

vbrsr-upgrade.tgz: /nettmp/vbrsr.tgz
	cp /nettmp/vbrsr.tgz $@

/nettmp/vbrsr.tgz: 
	-mount /nettmp

vbrsr.tgz: wapi.c send.c receive.c vbrsr.h rcvwin.h Makefile COPYING
	rm -rf tar.d
	mkdir -p tar.d/vbrsr
	cp -pl $^ tar.d/vbrsr
	cd tar.d && tar -zcf ../$@ vbrsr
	rm -rf tar.d
