#!/usr/bin/make -f

# Package name
#
p=seesat5

# Define variables and flags
#
STRIP = -s

INSTALL = install
INSTALL_BIN = $(INSTALL) $(STRIP) --owner=root --group=root --mode=0755
INSTALL_LIB = $(INSTALL) --owner=root --group=root --mode=0644
INSTALL_DIR = $(INSTALL) --directory --owner=root --group=root --mode=0755

GZIP = gzip

TARGET-ROOT =
PREFIX = /usr

BIN_DIR = $(TARGET-ROOT)$(PREFIX)/bin
MAN_DIR = $(TARGET-ROOT)$(PREFIX)/man
DOC_DIR = $(TARGET-ROOT)$(PREFIX)/doc/$(p)
EXAMPLE_DIR = $(TARGET-ROOT)$(PREFIX)/doc/$(p)/examples

# Build the components of the package
#
build:
	$(checkdir)
	make
	-rm clean
	touch build

# Clean up after a build
#
clean:
	$(checkdir)
	-rm -f build
	-rm -f *.o seesat5 cr
	-rm -rf debian/tmp *~ debian/*~ *.orig ./#*# 
	-rm -rf debian/substvars* debian/files*
	touch clean

# There are no binary independent components to this package
#
binary-indep:
# None
	@echo 'No indepenent portions for this package.'

# Collect the binary components and build the package
#
binary binary-arch:		checkroot build

# Make sure old tmp directory is gone
#
	-rm -rf debian/tmp

# Install new temporary directories for collecting components
#
	$(INSTALL_DIR) debian/tmp debian/tmp/DEBIAN

# Install scripts
#
	cp debian/postinst debian/tmp/DEBIAN/postinst
	chmod +x debian/tmp/DEBIAN/postinst

# Install the package components
#
	make -f debian/rules TARGET-ROOT=debian/tmp install

# Determine dependencies
#
	dpkg-shlibdeps $(p)

# Generate control information
#
	dpkg-gencontrol

# Set permissions and ownerships on all files in package
#
	chown -R root.root debian/tmp
	chmod -R g-ws debian/tmp

# Build the binary package
#
	dpkg --build debian/tmp ..

# Install all components of this package
#
install: build

# Create the installation directory
#
	$(INSTALL_DIR) $(BIN_DIR)

# Install the binaries for seesat5 and cr
#
	$(INSTALL_BIN) seesat5 $(BIN_DIR)/seesat5
	$(INSTALL_BIN) cr $(BIN_DIR)/cr

# Create man1 directory
#	
	$(INSTALL_DIR) $(MAN_DIR)/man1

# Install man pages for seesat5 and cr
#
	$(INSTALL_LIB) Docs/seesat5.man.1 $(MAN_DIR)/man1/seesat5.1
	$(INSTALL_LIB) Docs/cr.man.1 $(MAN_DIR)/man1/cr.1

# Create man5 directory
#
	$(INSTALL_DIR) $(MAN_DIR)/man5

# Install man pages for SEESAT5.INI and tle
#
	$(INSTALL_LIB) Docs/SEESAT5.INI.man.5 $(MAN_DIR)/man5/SEESAT5.INI.5
	$(INSTALL_LIB) Docs/tle.man.5 $(MAN_DIR)/man5/tle.5

# Create man7 directory
#
	$(INSTALL_DIR) $(MAN_DIR)/man7

# Install man pages for seesat5
#
	$(INSTALL_LIB) Docs/seesat5.man.7 $(MAN_DIR)/man7/seesat5.7

# Create the docs directory
#
	$(INSTALL_DIR) $(DOC_DIR)

# Install and gzip all the docs
#
	$(INSTALL_LIB) Docs/seesat5.docs.old $(DOC_DIR)
	$(INSTALL_LIB) debian/changelog $(DOC_DIR)
	$(GZIP) $(DOC_DIR)/*

# Except don't gzip the copyright
#
	$(INSTALL_LIB) debian/copyright $(DOC_DIR)

# Create the example directory
#
	$(INSTALL_DIR) $(EXAMPLE_DIR)

# Install and gzip example files
#
	$(INSTALL_LIB) Docs/Visual.tle $(EXAMPLE_DIR)
	$(INSTALL_LIB) Docs/SEESAT5.INI.example $(EXAMPLE_DIR)
	$(GZIP) $(EXAMPLE_DIR)/*
#
# End of component installation

# Bookkeeping
#
define checkdir
	test -f seesat.c -a -f debian/rules
endef

checkroot:
	$(checkdir)
	test root = "`whoami`"

.PHONY: binary binary-arch binary-indep source clean checkroot
