###############################################################################
# BRLTTY - A background process providing access to the console screen (when in
#          text mode) for a blind person using a refreshable braille display.
#
# Copyright (C) 1995-2024 by The BRLTTY Developers.
#
# BRLTTY comes with ABSOLUTELY NO WARRANTY.
#
# This is free software, placed under the terms of the
# GNU Lesser General Public License, as published by the Free Software
# Foundation; either version 2.1 of the License, or (at your option) any
# later version. Please see the file LICENSE-LGPL for details.
#
# Web Page: http://brltty.app/
#
# This software is maintained by Dave Mielke <dave@mielke.cc>.
###############################################################################

all install uninstall install-documents:
	cd $(PGM_DIR) && $(MAKE) $@

install-extras: install-appstream install-messages install-polkit install-systemd install-udev

install-appstream uninstall-appstream:
	cd Autostart/AppStream && $(MAKE) $(@:-appstream=)

install-dracut uninstall-dracut:
	cd Initramfs/Dracut && $(MAKE) $(@:-dracut=)

install-messages uninstall-messages:
	cd $(MSG_DIR) && $(MAKE) $(@:-messages=)

install-polkit uninstall-polkit:
	cd Authorization/Polkit && $(MAKE) $(@:-polkit=)

install-systemd uninstall-systemd:
	cd Autostart/Systemd && $(MAKE) $(@:-systemd=)

install-udev uninstall-udev:
	cd Autostart/Udev && $(MAKE) $(@:-udev=)

ARCHIVE_NAME = $(PACKAGE_TARNAME)-$(PACKAGE_VERSION)
TAR_EXTENSION = tar
GZIP_EXTENSION = gz
BZIP2_EXTENSION = bz2
ZIP_EXTENSION = zip
XZ_EXTENSION = xz

SRC_TAR = $(ARCHIVE_NAME).$(TAR_EXTENSION)
$(SRC_TAR):
	$(SRC_TOP)mktar $(SRC_TOP) $(ARCHIVE_NAME) $@
src-tar: $(SRC_TAR)

SRC_TAR_GZIP = $(SRC_TAR).$(GZIP_EXTENSION)
$(SRC_TAR_GZIP): $(SRC_TAR)
	gzip -9 -c $(SRC_TAR) >$@
src-tar-gzip: $(SRC_TAR_GZIP)

SRC_TAR_BZIP2 = $(SRC_TAR).$(BZIP2_EXTENSION)
$(SRC_TAR_BZIP2): $(SRC_TAR)
	bzip2 -9 -c $(SRC_TAR) >$@
src-tar-bzip2: $(SRC_TAR_BZIP2)

SRC_TAR_XZ = $(SRC_TAR).$(XZ_EXTENSION)
$(SRC_TAR_XZ): $(SRC_TAR)
	xz -9 -c $(SRC_TAR) >$@
src-tar-xz: $(SRC_TAR_XZ)

rpm: $(SRC_TAR_GZIP)
	$(SRC_TOP)mkrpm $(SRC_TAR_GZIP)

SRC_ZIP = $(ARCHIVE_NAME).$(ZIP_EXTENSION)
$(SRC_ZIP):
	$(SRC_TOP)mkzip $(SRC_TOP) $(ARCHIVE_NAME) $@
src-zip: $(SRC_ZIP)

BIN_DIR = `pwd`/BinTree
bin-tree:
	$(INSTALL_DIRECTORY) $(BIN_DIR)
	$(MAKE) install INSTALL_ROOT=$(BIN_DIR)

BIN_TAR = $(ARCHIVE_NAME).$(HOST_OS).$(HOST_CPU).$(TAR_EXTENSION)
$(BIN_TAR): bin-tree
	tar -C $(BIN_DIR) -c -f $@ .
bin-tar: $(BIN_TAR)

BIN_TAR_GZIP = $(BIN_TAR).$(GZIP_EXTENSION)
$(BIN_TAR_GZIP): $(BIN_TAR)
	gzip -9 -c $(BIN_TAR) >$@
bin-tar-gzip: $(BIN_TAR_GZIP)

BIN_TAR_BZIP2 = $(BIN_TAR).$(BZIP2_EXTENSION)
$(BIN_TAR_BZIP2): $(BIN_TAR)
	bzip2 -9 -c $(BIN_TAR) >$@
bin-tar-bzip2: $(BIN_TAR_BZIP2)

BIN_TAR_XZ = $(BIN_TAR).$(XZ_EXTENSION)
$(BIN_TAR_XZ): $(BIN_TAR)
	xz -9 -c $(BIN_TAR) >$@
bin-tar-xz: $(BIN_TAR_XZ)

ITERATE = \
   -for directory in \
   $(PGM_DIR) \
   $(DOC_DIR) \
   $(MSG_DIR) \
   Autostart/AppStream \
   Autostart/Systemd \
   Autostart/Udev \
   Autostart/X11 \
   Authorization/Polkit \
   Initramfs/Dracut \
   Android/Gradle \
   ; do (cd $$directory && $(MAKE) $@); done

clean:: clean-archives
	$(ITERATE)
	-rm -f -r -- $(BIN_DIR)

clean-archives:
	-rm -f -- *.$(TAR_EXTENSION)
	-rm -f -- *.$(GZIP_EXTENSION)
	-rm -f -- *.$(BZIP2_EXTENSION)
	-rm -f -- *.$(XZ_EXTENSION)
	-rm -f -- *.$(ZIP_EXTENSION)
	-rm -f -- *.rpm

distclean:: clean
	$(ITERATE)
	-rm -f brltty.spec
	-rm -fr autom4te*.cache
	-rm -f config.log config.cache config.status
	-rm -f config.mk config.h brltty-config.sh brltty.pc
	-rm -f forbuild.log forbuild.cache forbuild.status
	-rm -f forbuild.mk forbuild.h
