#
# Copyright (C) 2019 by the University of Southern California
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License,
# version 2, as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
#

prefix  = /usr
BINDIR  = $(prefix)/bin
DATADIR = $(prefix)/share/doc/verfploer_plotter
PROGRAM = verfploeter_plotter

.PHONY: install test test-fsdb test-pcap uninstall dist

######################################################################
#
# Installation
#
install:
	@bash -c 'hash dbcolcreate >&/dev/null || echo "Warning: need to install perl-Fsdb to process FSDB files">&2'
	@bash -c 'hash pingextract >&/dev/null || echo "Warning: need to install pingextract to process PCAP files">&2'
	mkdir -p $(BINDIR)    
	install --mode="u=rwx,go=rx" verfploeter_plotter geobin_to_worldmap $(BINDIR)/
	install --mode="u=rwx,go=rx" -d $(DATADIR) $(DATADIR)/Sample_Input $(DATADIR)/Sample_Output
	install --mode="u=rw,go=r"   Sample_Input/* $(DATADIR)/Sample_Input
	install --mode="u=rw,go=r"   Sample_Output/* $(DATADIR)/Sample_Output
	install --mode="u=rw,go=r"   ip_list.txt geolocation.fsdb.bz2 $(DATADIR)

######################################################################
#
# Testing
#

test: test-fsdb test-pcap

ifeq ($(shell bash -c 'hash dbcolcreate >& /dev/null && echo "fsdb"' ), fsdb)
test-fsdb:
	@echo "***Running test-fsdb"
	verfploeter_plotter --year=2019 --month=06 --day=07 --n=3 --s=LAX,ARI,MIA --id=15000 --format=fsdb
	@echo "Output generated in base directory"
else
test-fsdb:
	@echo "***Skipping test-fsdb (perl-Fsdb is not installed"
endif


ifeq ($(shell bash -c 'hash pingextract >& /dev/null && echo "pingextract"' ), pingextract)
test-pcap:
	@echo "***Running test-pcap"
	verfploeter_plotter --year=2019 --month=06 --day=07 --n=3 --s=LAX,ARI,MIA --id=15000
	@echo "Output generated in base directory"
else
test-pcap:
	@echo "***Skipping test-pcap (pingextract is not installed)"
endif

######################################################################
#
# Uninstall
#

uninstall:
	rm -f $(BINDIR)/verfploeter_plotter $(BINDIR)/geobin_to_worldmap
	rm -f -r $(DATADIR)
######################################################################
#
# packaging
#

# generic

VERSION=0.1
RELEASE=1
VR=$(VERSION)-$(RELEASE)


TARBALL=verfploeter_plotter-$(VR).tar.gz
dist: $(TARBALL)

$(TARBALL):
	ln -nfs . verfploeter_plotter-$(VR)
	tar -chvzf $(TARBALL) \
		verfploeter_plotter-$(VR)/README.md \
		verfploeter_plotter-$(VR)/verfploeter_plotter \
		verfploeter_plotter-$(VR)/Makefile \
		verfploeter_plotter-$(VR)/Sample_Input \
		verfploeter_plotter-$(VR)/Sample_Output \
		verfploeter_plotter-$(VR)/geobin_to_worldmap \
		verfploeter_plotter-$(VR)/geolocation.fsdb.bz2 \
		verfploeter_plotter-$(VR)/ip_list.txt
	rm verfploeter_plotter-$(VR)
