#!/usr/bin/make -f
# -*- makefile -*-
# debian makefile for weewx
# Copyright 2013-2024 Matthew Wall

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

PKG=weewx
PKG_VERSION=WEEWX_VERSION
PYTHON=python3
SRC=$(CURDIR)
DST=$(CURDIR)/debian/$(PKG)
DST_BINDIR=$(DST)/usr/share/weewx
DST_CFGDIR=$(DST)/etc/weewx
DST_DOCDIR=$(DST)/usr/share/doc/weewx

# these are the entry points
ENTRIES=weewxd weectl

%:
	dh $@ --with python3

override_dh_auto_clean:
	dh_auto_clean
	rm -rf build dist
	rm -f *.egg-info

# this rule grabs all of the bits from the source tree and puts them into
# a staging area that has the directory structure of a debian system.  it
# explicitly does *not* do things the 'python way' using pip.
install:
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs
	dh_installchangelogs

# create the directory structure
	mkdir -p $(DST_BINDIR)
	mkdir -p $(DST_CFGDIR)
	mkdir -p $(DST_DOCDIR)
	mkdir -p $(DST)/usr/bin

# copyright, license, and upstream changelog
	cp docs_src/copyright.md $(DST_DOCDIR)/copyright
	cp LICENSE.txt $(DST_DOCDIR)/license
	cp docs_src/changes.md $(DST_DOCDIR)/changelog
	dh_compress usr/share/doc/weewx/changelog

# copy the weewx code
	cp -r $(SRC)/src/* $(DST_BINDIR)

# copy selected ancillary files to the config dir
	cp -r $(SRC)/src/weewx_data/examples $(DST_CFGDIR)
	cp -r $(SRC)/src/weewx_data/util/import $(DST_CFGDIR)
	cp -r $(SRC)/src/weewx_data/util/logwatch $(DST_CFGDIR)
	cp -r $(SRC)/src/weewx_data/util/rsyslog.d $(DST_CFGDIR)
	cp -r $(SRC)/src/weewx_data/util/logrotate.d $(DST_CFGDIR)
	mkdir $(DST_CFGDIR)/init.d
	cp $(SRC)/src/weewx_data/util/init.d/weewx $(DST_CFGDIR)/init.d
	cp $(SRC)/src/weewx_data/util/init.d/weewx-multi $(DST_CFGDIR)/init.d
	mkdir $(DST_CFGDIR)/systemd
	cp $(SRC)/pkg/etc/systemd/system/weewx.service $(DST_CFGDIR)/systemd
	cp $(SRC)/pkg/etc/systemd/system/weewx@.service $(DST_CFGDIR)/systemd
	mkdir $(DST_CFGDIR)/udev
	cp $(SRC)/pkg/etc/udev/rules.d/weewx.rules $(DST_CFGDIR)/udev

# create the configuration file
	sed \
 -e 's%HTML_ROOT = public_html%HTML_ROOT = /var/www/html/weewx%' \
 -e 's%SQLITE_ROOT = .*%SQLITE_ROOT = /var/lib/weewx%' \
 $(SRC)/src/weewx_data/weewx.conf > $(DST_CFGDIR)/weewx.conf

# make a virgin copy of the configuration file
	cp $(DST_CFGDIR)/weewx.conf $(DST_CFGDIR)/weewx.conf-$(PKG_VERSION)

# create the entry points
	for f in $(ENTRIES); do \
  sed \
 -e 's%WEEWX_BINDIR=.*%WEEWX_BINDIR=/usr/share/weewx%' \
 -e 's%WEEWX_PYTHON=.*%WEEWX_PYTHON=$(PYTHON)%' \
 $(SRC)/bin/$$f > $(DST)/usr/bin/$$f; \
done

# additional debian control files that dpkg-buildpackage seems to ignore
	mkdir -p $(DST)/DEBIAN
	cp $(SRC)/debian/config $(DST)/DEBIAN
	cp $(SRC)/debian/templates $(DST)/DEBIAN

binary-indep: install
	dh_fixperms
	dh_installdeb
	dh_gencontrol
	dh_lintian
	dh_md5sums
	dh_builddeb -- -Zgzip

binary-arch:

binary: binary-indep binary-arch

.PHONY: build clean binary-indep binary-arch binary install configure
