#!/usr/bin/env bash
set -x

# https://wiki.hyprland.org/Getting-Started/Installation/

# An example of a debian package source tree
# git clone https://salsa.debian.org/debian/hyprland.git

# Install deps
# https://wiki.hyprland.org/Getting-Started/Installation/

# NB. Hyprland now statically links to its own fork of wlroots.
# https://github.com/hyprwm/wlroots-hyprland

# Previous tree from the salsa packaged hyprland by lumin (Mo Zhou <lumin@debian.org>)
##  tree -L 3 /space/code_repositories/hyprland/hyprland/debian/
##  /space/code_repositories/hyprland/hyprland/debian/
##  ├── changelog
##  ├── control
##  ├── copyright
##  ├── debhelper-build-stamp
##  ├── files
##  ├── hyprland
##  │   ├── DEBIAN
##  │   │   ├── control
##  │   │   ├── md5sums
##  │   │   ├── shlibs
##  │   │   └── triggers
##  │   └── usr
##  │       ├── bin
##  │       ├── lib
##  │       └── share
##  ├── hyprland-common
##  │   ├── DEBIAN
##  │   │   ├── control
##  │   │   └── md5sums
##  │   └── usr
##  │       └── share
##  ├── hyprland-common.install
##  ├── hyprland-common.substvars
##  ├── hyprland-dev
##  │   ├── DEBIAN
##  │   │   ├── control
##  │   │   └── md5sums
##  │   └── usr
##  │       ├── include
##  │       └── share
##  ├── hyprland-dev.install
##  ├── hyprland-dev.substvars
##  ├── hyprland.install
##  ├── hyprland.substvars
##  ├── patches
##  │   ├── make.patch
##  │   └── series
##  ├── rules
##  ├── source
##  │   └── format
##  ├── tmp
##  │   └── usr
##  │       ├── bin
##  │       ├── include
##  │       ├── lib
##  │       └── share
##  └── watch

conf_print_debian_control() {
	cat <<'EOF'
Source: hyprland
Section: x11
Homepage: https://hyprland.org/
Priority: optional
Standards-Version: 4.6.2
Vcs-Git: https://salsa.debian.org/debian/hyperland.git
Vcs-Browser: https://salsa.debian.org/debian/hyperland
Maintainer: Mo Zhou <lumin@debian.org>
Build-Depends: 
  debhelper-compat (= 13),
  cmake,
  cmake-extras,
  meson,
  ninja-build,
  gettext,
  gettext-base,
  git,
  fontconfig,
  libfontconfig-dev,
  libffi-dev,
  libxml2-dev,
  libdrm2,
  libdrm-dev,
  libxkbcommon-x11-dev,
  libxkbregistry-dev,
  libxkbcommon-dev,
  libpixman-1-dev,
  libudev-dev,
  libseat-dev,
  seatd,
  libxcb-dri3-dev,
  libvulkan-dev,
  libvulkan-volk-dev,
  libvkfft-dev,
  libgulkan-dev,
  libegl-dev,
  libgles2,
  libegl1-mesa-dev,
  glslang-tools,
  libinput-bin,
  libinput-dev,
  libavutil-dev,
  libavcodec-dev,
  libavformat-dev,
  libxcb1,
  libxcb-composite0-dev,
  libxcb-ewmh2,
  libxcb-ewmh-dev,
  libxcb-present-dev,
  libxcb-icccm4-dev,
  libxcb-render-util0,
  libxcb-render-util0-dev,
  libxcb-res0-dev,
  libxcb-xinput-dev,
  xdg-desktop-portal-wlr,
  hwdata,
  hyprwayland-scanner >=0.3.1,
  hyprcursor >=0.1.7,
  xwayland,
  libwayland-server0,
  libwayland-dev,
  libpango1.0-dev,
  wayland-protocols,
  libtomlplusplus-dev,
	libhyprlang2 >=0.3.2,
  libhyprlang-dev,
  systemd-dev | libelogind-dev,
  libgbm-dev,
  libliftoff-dev,
  libdisplay-info-dev,
  cpio,

Package: hyprland
Architecture: any
Depends: ${misc:Depends}, ${shlibs:Depends}, hyprland-common
Suggests: kitty | foot, dolphin, waybar
Description: Tiling compositor with the looks
  Hyprland is a dynamic tiling Wayland compositor based on wlroots that doesn't
  sacrifice on its looks.
  .
  It provides the latest Wayland features, is highly customizable, has all the
  eyecandy, the most powerful plugins, easy IPC, much more QoL stuff than other
  wlr-based compositors and more.
  .
  This package contains the executables.

Package: hyprland-dev
Architecture: any
Depends: ${misc:Depends}, ${shlibs:Depends}, hyprland (= ${binary:Version})
Description: Tiling compositor with the looks
  Hyprland is a dynamic tiling Wayland compositor based on wlroots that doesn't
  sacrifice on its looks.
  .
  It provides the latest Wayland features, is highly customizable, has all the
  eyecandy, the most powerful plugins, easy IPC, much more QoL stuff than other
  wlr-based compositors and more.
  .
  This package contains the development files.

Package: hyprland-common
Architecture: all
Depends: ${misc:Depends}
Description: Tiling compositor with the looks
  Hyprland is a dynamic tiling Wayland compositor based on wlroots that doesn't
  sacrifice on its looks.
  .
  It provides the latest Wayland features, is highly customizable, has all the
  eyecandy, the most powerful plugins, easy IPC, much more QoL stuff than other
  wlr-based compositors and more.
  .
  This package contains architecture independent files such as wallpapers.

Package: hyprland-completions
Architecture: all
Depends: ${misc:Depends}
Description: Tiling compositor with the looks
  Hyprland is a dynamic tiling Wayland compositor based on wlroots that doesn't
      sacrifice on its looks.
  .
  It provides the latest Wayland features, is highly customizable, has all the
  eyecandy, the most powerful plugins, easy IPC, much more QoL stuff than other
  wlr-based compositors and more.
  .
  This package contains the completions for bash, zsh and fish 
EOF
}
# conf_print_debian_control | sudo tee debian/control

conf_print_hyprland_copyright() {
	cat <<-'EOF'
		Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
		Files-Excluded: subprojects/tracy/*

		Files: *
		      assets/*
		      docs/*
		      example/*
		      hyprctl/*
		      hyprpm/*
		      nix/*
		      src/*
		Copyright: 2022, 2023, vaxerski
		License: BSD-3-clause

		Files: debian/*
		Copyright: 2024, Mo Zhou <lumin@debian.org>
		License: BSD-3-clause

		###############################################################################

		Files: protocols/idle.xml
		Copyright: 2015, Martin Gräßlin
		License: LGPL-2.1+

		Files: protocols/pointer-constraints-unstable-v1.xml
		Copyright: 2015, Red Hat Inc.
		2014, Jonas Ådahl
		License: Expat

		Files: protocols/tablet-unstable-v2.xml
		Copyright: 2015, 2016, Red Hat, Inc.
		2014, Stephen "Lyude" Chandler Paul
		License: Expat

		Files: protocols/wlr-foreign-toplevel-management-unstable-v1.xml
		Copyright: 2018, Ilia Bozhinov
		License: NTP

		Files: protocols/wlr-layer-shell-unstable-v1.xml
		Copyright: 2017, Drew DeVault
		License: NTP

		Files: protocols/wlr-output-power-management-unstable-v1.xml
		Copyright: 2019, Purism SPC
		License: Expat

		Files: protocols/wlr-screencopy-unstable-v1.xml
		Copyright: 2019, Andri Yngvason
		2018, Simon Ser
		License: Expat

		###############################################################################

		Files: subprojects/hyprland-protocols/*
		Copyright: 2022, Hypr Development
		License: BSD-3-clause

		Files: subprojects/hyprland-protocols/protocols/*
		Copyright: 2022, Vaxry
		License: BSD-3-clause

		###############################################################################

		Files: subprojects/udis86/*
		Copyright: 2002-2013, Vivek Thampi <vivek.mt@gmail.com>
		License: BSD-2-clause

		Files: subprojects/udis86/m4/*
		Copyright: 2008, 2009, Francesco Salvestrini <salvestrini@users.sourceforge.net>
		          2008 Dustin J. Mitchell <dustin@cs.uchicago.edu>
		          2008, Tim Toolan <toolan@ele.uri.edu>
		License: FSFAP

		###############################################################################

		Files: subprojects/wlroots-hyprland/*
		Copyright: 2023, The wlroots contributors
		2017, 2018, Drew DeVault
		2014, Jari Vetoniemi
		License: Expat

		Files: subprojects/wlroots-hyprland/include/wlr/xcursor.h
		Copyright: 2012, Intel Corporation
		License: UNKNOWN
		Please fill license UNKNOWN from header of 

		Files: subprojects/wlroots-hyprland/include/xcursor/*
		Copyright: 2002, Keith Packard
		License: UNKNOWN
		Please fill license UNKNOWN from header of 

		Files: subprojects/wlroots-hyprland/include/xcursor/cursor_data.h
		Copyright: 1999, SuSE, Inc.
		License: UNKNOWN
		Please fill license UNKNOWN from header of 

		Files: subprojects/wlroots-hyprland/protocol/drm.xml
		Copyright: 2010, 2011, Intel Corporation
		2008-2011, Kristian Høgsberg
		License: HPND-sell-variant or NTP~disclaimer

		Files: subprojects/wlroots-hyprland/protocol/input-method-unstable-v2.xml
		Copyright: 2018, Purism SPC
		2017, 2018, Red Hat, Inc.
		2015, 2016, Jan Arne Petersen
		2012, 2013, Collabora, Ltd.
		2010-2013, Intel Corporation
		2008-2011, Kristian Høgsberg
		License: UNKNOWN
		Please fill license UNKNOWN from header of 

		Files: subprojects/wlroots-hyprland/protocol/server-decoration.xml
		Copyright: 2015, Martin Gräßlin
		License: LGPL-2.1+

		Files: subprojects/wlroots-hyprland/protocol/virtual-keyboard-unstable-v1.xml
		Copyright: 2018, Purism SPC
		2012, 2013, Collabora, Ltd.
		2010-2013, Intel Corporation
		2008-2011, Kristian Høgsberg
		License: UNKNOWN
		Please fill license UNKNOWN from header of 

		Files: subprojects/wlroots-hyprland/protocol/wlr-data-control-unstable-v1.xml
		Copyright: 2019, Ivan Molodetskikh
		2018, Simon Ser
		License: HPND-sell-variant or NTP~disclaimer

		Files: subprojects/wlroots-hyprland/protocol/wlr-export-dmabuf-unstable-v1.xml
		Copyright: 2018, Rostislav Pehlivanov
		License: UNKNOWN
		Please fill license UNKNOWN from header of 

		Files: subprojects/wlroots-hyprland/protocol/wlr-foreign-toplevel-management-unstable-v1.xml
		Copyright: 2018, Ilia Bozhinov
		License: HPND-sell-variant or NTP~disclaimer

		Files: subprojects/wlroots-hyprland/protocol/wlr-gamma-control-unstable-v1.xml
		Copyright: 2018, Simon Ser
		2015, Giulio camuffo
		License: HPND-sell-variant or NTP~disclaimer

		Files: subprojects/wlroots-hyprland/protocol/wlr-layer-shell-unstable-v1.xml
		Copyright: 2017, Drew DeVault
		License: HPND-sell-variant or NTP~disclaimer

		Files: subprojects/wlroots-hyprland/protocol/wlr-output-management-unstable-v1.xml
		Copyright: 2019, Purism SPC
		License: HPND-sell-variant or NTP~disclaimer

		Files: subprojects/wlroots-hyprland/protocol/wlr-output-power-management-unstable-v1.xml
		Copyright: 2019, Purism SPC
		License: UNKNOWN
		Please fill license UNKNOWN from header of 

		Files: subprojects/wlroots-hyprland/protocol/wlr-screencopy-unstable-v1.xml
		Copyright: 2019, Andri Yngvason
		2018, Simon Ser
		License: UNKNOWN
		Please fill license UNKNOWN from header of 

		Files: subprojects/wlroots-hyprland/protocol/wlr-virtual-pointer-unstable-v1.xml
		Copyright: 2019, Josef Gajdusek
		License: UNKNOWN
		Please fill license UNKNOWN from header of 

		Files: subprojects/wlroots-hyprland/tinywl/*
		Copyright: no-info-found
		License: CC0-1.0

		Files: subprojects/wlroots-hyprland/xcursor/wlr_xcursor.c
		Copyright: 2012, Intel Corporation
		License: UNKNOWN
		Please fill license UNKNOWN from header of 

		Files: subprojects/wlroots-hyprland/xcursor/xcursor.c
		Copyright: 2002, Keith Packard
		License: UNKNOWN
		Please fill license UNKNOWN from header of 

		###############################################################################

		License: BSD-3-clause
		The BSD License
		.
		Redistribution and use in source and binary forms, with or without
		modification, are permitted provided that the following conditions are
		met:
		.
		  * Redistributions of source code must retain the above copyright
		    notice, this list of conditions and the following disclaimer.
		.
		  * Redistributions in binary form must reproduce the above copyright
		    notice, this list of conditions and the following disclaimer in the
		    documentation and/or other materials provided with the distribution. 
		.
		  * Neither the name of the copyright holder nor the names of its
		    contributors may be used to endorse or promote products derived from
		    this software without specific prior written permission. 
		.
		THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
		IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
		TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
		PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
		CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
		EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
		PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
		PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
		LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
		NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
		SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

		License: CC0-1.0
		To the extent possible under law, the author(s) have dedicated all
		copyright and related and neighboring rights to this software to the public
		domain worldwide. This software is distributed without any warranty.
		On Debian systems, the complete text of the CC0 license, version 1.0,
		can be found in /usr/share/common-licenses/CC0-1.0.

		License: CECILL-C
		Please fill license CECILL-C from header of 

		License: Expat
		The MIT License
		.
		Permission is hereby granted, free of charge, to any person
		obtaining a copy of this software and associated
		documentation files (the "Software"), to deal in the Software
		without restriction, including without limitation the rights to
		use, copy, modify, merge, publish, distribute, sublicense,
		and/or sell copies of the Software, and to permit persons to
		whom the Software is furnished to do so, subject to the
		following conditions:
		.
		The above copyright notice and this permission notice shall
		be included in all copies or substantial portions of the
		Software.
		.
		THE SOFTWARE IS PROVIDED "AS IS", WITHOUT
		WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
		INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
		MERCHANTABILITY, FITNESS FOR A PARTICULAR
		PURPOSE AND NONINFRINGEMENT. IN NO EVENT
		SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
		LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
		LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
		TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
		CONNECTION WITH THE SOFTWARE OR THE USE OR
		OTHER DEALINGS IN THE SOFTWARE.

		License: FSFAP
		Please fill license FSFAP from header of 

		License: HPND-sell-variant
		Please fill license HPND-sell-variant or NTP~disclaimer from header of 

		License: LGPL-2.1+
		This program is free software; you can redistribute it and/or modify it
		under the terms of the GNU Lesser General Public License as published by the
		Free Software Foundation; version 2.1 of the License, or (at
		your option) any later version.
		On Debian systems, the complete text of version 2.1 of the GNU Lesser
		General Public License can be found in '/usr/share/common-licenses/LGPL-2.1'.

		License: NTP~disclaimer
		Please fill license HPND-sell-variant or NTP~disclaimer from header of 

		License: Unlicense
		Please fill license Unlicense from header of 

		License: Zlib
		The zlib License
		.
		This software is provided 'as-is', without any express or implied
		warranty. In no event will the authors be held liable for any damages
		arising from the use of this software.
		.
		Permission is granted to anyone to use this software for any purpose,
		including commercial applications, and to alter it and redistribute it
		freely, subject to the following restrictions:
		.
		  1. The origin of this software must not be misrepresented; you must
		      not claim that you wrote the original software. If you use this
		      software in a product, an acknowledgment in the product
		      documentation would be appreciated but is not required.
		.
		  2. Altered source versions must be plainly marked as such, and must
		      not be misrepresented as being the original software.
		.
		  3. This notice may not be removed or altered from any source
		      distribution.

		  License: Expat
		  Permission is hereby granted, free of charge, to any person obtaining a
		  copy of this software and associated documentation files (the "Software"),
		  to deal in the Software without restriction, including without limitation
		  the rights to use, copy, modify, merge, publish, distribute, sublicense,
		  and/or sell copies of the Software, and to permit persons to whom the
		  Software is furnished to do so, subject to the following conditions:
		  .
		  The above copyright notice and this permission notice shall be included
		  in all copies or substantial portions of the Software.
		  .
		  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
		  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
		  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
		  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
		  CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 
		  TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
		  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
	EOF
}
# conf_print_hyprland_copyright | tee debian/copyright

conf_print_hyprland_common_install() {
	cat <<-'EOF'
		usr/share/hyprland
	EOF
}
# conf_print_hyprland_common_install | tee debian/hyprland-common.install

conf_print_hyprland_common_substvars() {
	cat <<-'EOF'
		misc:Depends=
		misc:Pre-Depends=
	EOF
}
# conf_print_hyprland_common_substvars | tee debian/hyprland-common.substvars

# conf_print_hyprland_dev_install() {
#   cat <<-'EOF'
#   usr/include/hyprland/protocols/*
#   usr/include/hyprland/wlroots/*
#   usr/include/hyprland-dev.h
#   usr/share/pkgconfig/hyprland.pc
#
#   usr/lib/libwlroots.so.13032
#  EOF
# }
## conf_print_hyprland_dev_install | sudo tee debian/hyprland-dev.install

# conf_print_hyprland_dev_install() {
#   cat <<-'EOF'
#   usr/include/hyprland/protocols/*
#   usr/include/hyprland/wlroots/*
#   usr/include/hyprland-dev.h
#   usr/share/pkgconfig/hyprland.pc
#
#   usr/lib/libwlroots.so.13032
#  EOF
# }
## conf_print_hyprland_dev_install | sudo tee debian/hyprland-dev.install

conf_print_hyprland_dev_install() {
	cat <<-'EOF'
		usr/include/
		usr/share/pkgconfig
	EOF
}
# conf_print_hyprland_dev_install | sudo tee debian/hyprland-dev.install

conf_print_hyprland_dev_substvars() {
	cat <<-'EOF'
		misc:Depends=
		misc:Pre-Depends=
	EOF
}
# conf_print_hyprland_dev_substvars | tee debian/hyprland-dev.substvars

#conf_print_hyprland_install() {
#  cat <<-'EOF'
#    usr/bin/Hyprland
#    usr/bin/hyprctl
#    usr/bin/hyprpm
#    usr/share/wayland-sessions/hyprland.desktop
#    usr/share/xdg-desktop-portal/hyprland-portals.conf
# EOF
#}
## conf_print_hyprland_install | sudo tee debian/hyprland.install

conf_print_hyprland_install() {
	cat <<-'EOF'
		usr/bin/
		usr/lib/
		usr/share/man
		usr/share/wayland-sessions
		usr/share/xdg-desktop-portal
	EOF
}
# conf_print_hyprland_install | sudo tee debian/hyprland.install

conf_print_hyprland_substvars() {
	cat <<-EOF
		shlibs:Depends=hyprland (>= ${VERSION}), libc6 (>= 2.34), libcairo2 (>= 1.2.4), libdisplay-info1 (>= 0.1.1), libdrm2 (>= 2.4.119), libegl1, libeudev1 (>= 3.2.14), libgbm1 (>= 23.3.5), libgcc-s1 (>=13.2.0), libgles2, libglib2.0-0 (>=2.78.4), libhyprlang2 (>= 0.3.2), libinput10 (>=1.25.0), libliftoff0 (>= 0.4.1), libopengl0, libpango-1.0-0 (>= 1.14.0), libpangocairo-1.0-0 (>= 1.52.0), libpixman-1-0 (>= 0.42.0), libseat1 (>= 0.8.0), libstdc++6 (>= 13.2), libtomlplusplus3 (>= 3.4.0), libwayland-client0 (>= 1.20.0), libwayland-server0 (>= 1.22.0), libxcb-composite0, libxcb-dri3-0 (>= 1.13), libxcb-ewmh2 (>= 0.4.1), libxcb-icccm4 (>= 0.4.1), libxcb-present0, libxcb-render-util0, libxcb-render0, libxcb-res0 (>= 1.10), libxcb-shm0 (>= 1.10), libxcb-xfixes0 (>= 1.15), libxcb-xinput0 (>= 1.15), libxcb1, libxkbcommon0 (>= 1.6.0)
		misc:Depends=
		misc:Pre-Depends=
	EOF
}
# conf_print_hyprland_substvars | tee debian/hyprland.substvars

conf_print_hyprland_completions_install() {
	cat <<-'EOF'
		usr/share/bash-completion/completions/hyprctl
		usr/share/bash-completion/completions/hyprpm
		usr/share/fish/vendor_completions.d/hyprctl.fish
		usr/share/fish/vendor_completions.d/hyprpm.fish
		usr/share/zsh/site-functions/_hyprctl
		usr/share/zsh/site-functions/_hyprpm
	EOF
}
# conf_print_hyprland_completions_install | sudo tee debian/hyprland.install

conf_print_hyprland_completions_substvars() {
	cat <<-'EOF'
		misc:Depends=
		misc:Pre-Depends=
	EOF
}
# conf_print_hyprland_completions_substvars | tee debian/hyprland-completions.substvars

# Don't call this use deb_create_watch.py below.
conf_print_github_watch() {
	cat <<-EOF
		version=4

		opts=filenamemangle=s/.+\/v?(\d\S+)\.tar\.gz/${PROJECT}-$1\.tar\.gz/ \\
		  https://github.com/${USER}/${PROJECT}/tags .*/v?(\d\S+)\.tar\.gz
	EOF
}
# conf_print_github_watch | sudo tee debian/watch

conf_print_initial_changelog() {
	cat <<-EOF
		${PROJECT}-${VERSION} (${VERSION}) UNRELEASED; urgency=medium

		  * Initial release.

		  -- root <root@devuan> ${TIMESTAMP}
	EOF
}
# conf_print_initial_changelog | sudo tee debian/changelog

# salsa.debian.org files above this point

conf_print_hyprland_docs_install() {
	cat <<-'EOF'
		usr/share/man/man1/Hyprland.1
		usr/share/man/man1/hyprctl.1
		usr/share/man/man1/hyprpm.1
		usr/share/doc/hyprland/README.md
		usr/share/doc/hyprland/CONTRIBUTING.md
		usr/share/doc/hyprland/LICENSE
	EOF
}
# conf_print_hyprland_docs_install | sudo tee debian/hyprland-docs.install

conf_print_hyprland_preinst() {
	cat <<-'EOF'
		#!/bin/sh
		set -e

		# Define the configuration directory
		CONFIG_DIR="/etc/hyprland"

		# Check if the configuration directory exists
		if [ ! -d "$CONFIG_DIR" ]; then
		  # Create the configuration directory
		  mkdir -p "$CONFIG_DIR"
		  echo "Created configuration directory: $CONFIG_DIR"
		fi

		# Check for existing configuration files and back them up
		if [ -f "$CONFIG_DIR/hyprland.conf" ]; then
		  mv "$CONFIG_DIR/hyprland.conf" "$CONFIG_DIR/hyprland.conf.bak"
		  echo "Backed up existing configuration file to: $CONFIG_DIR/hyprland.conf.bak"
		fi

		# Additional pre-installation tasks can be added here
		# This script is a starting point. Depending on your project's needs, you 
		# might want to add more checks or actions to the preinst script. 
		# For example, you could check for specific dependencies, verify system 
		# configurations, or perform any other setup tasks required before installing Hyprland.

		Remember, the preinst script is called before the package is unpacked and installed, 
		so it cannot rely on any files included in its package. Only essential packages and 
		pre-dependencies (Pre-Depends) may be assumed to be available at this point
	EOF
}
# conf_print_hyprland_preinst | sudo tee debian/hyprland.preinst
# chmod +x debian/hyprland.preinst

conf_print_hyprland_postinst() {
	cat <<-'EOF'
		#!/bin/sh
		set -e

		# Define the binary directory
		BIN_DIR="/usr/bin"

		# Create a symbolic link for the Hyprland binary
		if [ ! -e "$BIN_DIR/hyprland" ]; then
		    ln -s "$BIN_DIR/Hyprland" "$BIN_DIR/hyprland"
		    echo "Created symbolic link for Hyprland"
		fi

		# Check if the Wayland session file exists and update it if necessary
		SESSION_FILE="/usr/share/wayland-sessions/hyprland.desktop"
		if [ -f "$SESSION_FILE" ]; then
		    # Perform any necessary updates to the session file
		    echo "Updated Wayland session file: $SESSION_FILE"
		fi

		# Additional post-installation tasks can be added here
		This script is a starting point. Depending on your project's needs, you might want 
		to add more checks or actions to the postinst script. For example, you could check 
		for specific dependencies, verify system configurations, or perform any other setup 
		tasks required after installing Hyprland.

		Remember, the postinst script is called after the package is unpacked and installed, 
		so it can rely on any files included in its package. It's a good place to perform any 
		final setup or configuration tasks that depend on the installed files
	EOF
}
# conf_print_hyprland_postinst | sudo tee debian/hyprland.postinst
# chmod +x debian/hyprland.postinst

# the install operations taken from the Makefile, is we are using the make
# file we don't need to call the from debian/rules

conf_print_hyprland_setup() {
	cat <<EOF
#!/bin/sh
set -e

PREFIX="$1"

echo "Copying files to \${PREFIX}: ${PREFIX}"
mkdir -p ${PREFIX}/share/wayland-sessions
mkdir -p ${PREFIX}/bin
mkdir -p ${PREFIX}/share/hyprland
mkdir -p ${PREFIX}/share/bash-completion/completions
mkdir -p ${PREFIX}/share/fish/vendor_completions.d
mkdir -p ${PREFIX}/share/zsh/site-functions
cp -f ${PREFIX}/build/Hyprland ${PREFIX}/bin
cp -f ${PREFIX}/build/hyprctl/hyprctl ${PREFIX}/bin
cp -f ${PREFIX}/build/hyprpm/hyprpm ${PREFIX}/bin
cp -f ${PREFIX}/hyprctl/hyprctl.bash ${PREFIX}/share/bash-completion/completions/hyprctl
cp -f ${PREFIX}/hyprctl/hyprctl.fish ${PREFIX}/share/fish/vendor_completions.d/hyprctl.fish
cp -f ${PREFIX}/hyprctl/hyprctl.zsh ${PREFIX}/share/zsh/site-functions/_hyprctl
cp -f ${PREFIX}/hyprpm/hyprpm.bash ${PREFIX}/share/bash-completion/completions/hyprpm
cp -f ${PREFIX}/hyprpm/hyprpm.fish ${PREFIX}/share/fish/vendor_completions.d/hyprpm.fish
cp -f ${PREFIX}/hyprpm/hyprpm.zsh ${PREFIX}/share/zsh/site-functions/_hyprpm
chmod 755 ${PREFIX}/bin/Hyprland
chmod 755 ${PREFIX}/bin/hyprctl
chmod 755 ${PREFIX}/bin/hyprpm
cd ${PREFIX}/bin && ln -sf Hyprland hyprland
if [ ! -f ${PREFIX}/share/wayland-sessions/hyprland.desktop ]; then cp ./example/hyprland.desktop ${PREFIX}/share/wayland-sessions; fi
cp ${PREFIX}/assets/wall* ${PREFIX}/share/hyprland
mkdir -p ${PREFIX}/share/xdg-desktop-portal
cp ${PREFIX}/assets/hyprland-portals.conf ${PREFIX}/share/xdg-desktop-portal

mkdir -p ${PREFIX}/share/man/man1
install -m644 ${PREFIX}/docs/*.1 ${PREFIX}/share/man/man1

mkdir -p ${PREFIX}/lib/
cp ${PREFIX}/subprojects/wlroots-hyprland/build/libwlroots.so.13032 ${PREFIX}/lib/
EOF
}
# conf_print_hyprland_setup | sudo tee debian/install.sh
# chmod +x debian/install.sh

conf_print_hyprland_prerm() {
	cat <<-EOF
		#!/bin/sh
		set -e
	EOF
}
# conf_print_hyprland_prerm | tee debian/hyprland.prerm
# chmod +x debian/hyprland.prerm

conf_print_hyprland_postrm() {
	cat <<-'EOF'
		#!/bin/sh
		set -e
	EOF
}
# conf_print_hyprland_postrm | tee debian/hyprland.postrm
# chmod +x debian/hyprland.postrm

conf_print_hyprland_headers() {
	cat <<-'EOF'
		#!/bin/sh
		set -e

		PREFIX="$1"

		# Copy headers from src
		find src -name '*.h*' -print0 | cpio --quiet -0dump "${PREFIX}/include/hyprland"

		# Copy headers from subprojects/wlroots-hyprland/include
		cd subprojects/wlroots-hyprland/include && find . -name '*.h*' -print0 | cpio --quiet -0dump "${PREFIX}/include/hyprland/wlroots" && cd ../../..

		# Copy headers from subprojects/wlroots-hyprland/build/include
		cd subprojects/wlroots-hyprland/build/include && find . -name '*.h*' -print0 | cpio --quiet -0dump "${PREFIX}/include/hyprland/wlroots" && cd ../../../..

		# Copy protocol headers
		cp ${PREFIX}/protocols/*-protocol.h "${PREFIX}/include/hyprland/protocols"

		# Copy pkg-config file
		cp ${PREFIX}/build/hyprland.pc "${PREFIX}/share/pkgconfig"
		if [ -d /usr/share/pkgconfig ]; then cp ${PREFIX}/build/hyprland.pc /usr/share/pkgconfig 2>/dev/null || true; fi

		  chmod -R 755 ${PREFIX}/include/hyprland
		  chmod 755 ${PREFIX}/share/pkgconfig
	EOF
}
# conf_print_hyprland_headers | sudo tee debian/install-headers.sh
# chmod +x debian/install-headers.sh

conf_print_hyprland_rules_1() {
	cat <<'EOF'
#!/usr/bin/make -f

# Uncomment and adjust the following lines as needed
#export DH_VERBOSE = 1
#export DEB_BUILD_MAINT_OPTIONS = hardening=+all
#export DEB_CFLAGS_MAINT_APPEND = -Wall -pedantic
#export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed

%:
	dh $@ --buildsystem=cmake+ninja

override_dh_auto_configure:
	dh_auto_configure -- \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_LIBRARY_ARCHITECTURE="$(DEB_HOST_MULTIARCH)" \
		-DCMAKE_BUILD_TYPE:STRING=Release \
		-DNO_SYSTEMD=TRUE

override_dh_auto_build:
	dh_auto_build -- \
		-DCMAKE_BUILD_TYPE:STRING=Release \
		-DCMAKE_INSTALL_PREFIX:STRING=$(CURDIR)/debian/hyperland \
		-DLEGACY_RENDERER:BOOL=true \
		-S . -B ./build -G Ninja
EOF
}
# conf_print_hyprland_rules_1 | sudo tee debian/rules

conf_print_hyprland_rules_2() {
	cat <<'EOF'
#!/usr/bin/make -f

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

#%: 
	dh $@ --buildsystem=cmake

override_dh_auto_test:
	if [ -f obj-x86_64-linux-gnu/build.ninja ] && grep -q 'test' obj-x86_64-linux-gnu/build.ninja; then \
		dh_auto_test; \
	else \
		echo "No tests found, skipping test phase."; \
	fi

override_dh_auto_build:
	meson setup build \
		--prefix=/usr \
		--libexecdir=lib \
		--sbindir=bin \
		--buildtype=release \
		--wrap-mode=nodownload \
		-Db_lto=true \
		-Db_pie=true \
		-Ddefault_library=shared \
		-Dxwayland=enabled \
		-Dsystemd=disabled && \
		meson compile -C build

override_dh_auto_install:
	meson install -C build \
		--destdir=$(CURDIR)/debian/hyprland \
		--skip-subprojects hyprland-protocols && \
		install -Dm0644 -t $(CURDIR)/debian/hyprland/usr/share/licenses/hyprland LICENSE && \
		./debian/install.sh $(CURDIR)
EOF
}
# conf_print_hyprland_rules_2 | sudo tee debian/rules

conf_print_hyprland_rules_3() {
	cat <<'EOF'
#!/usr/bin/make -f
%:
	dh $@ --builddirectory=build/ \
					--buildsystem=meson+ninja

override_dh_auto_configure:
#  meson subprojects update --reset

# use either -Dbuildtype=release|debug or -Doptimisation= and -Ddebug=
override_dh_auto_build:
	meson setup build \
    -Dwarning_level=2 \
    -Ddefault_library=static \
    -Dsystemd=disabled \
    -Dxwayland=enabled \
    -Dlegacy_renderer=enabled \
    -Doptimization=3 \
    -Ddebug=true
	ninja -C build

override_dh_auto_install:
	ninja -C build install
	dh_install
EOF
}
# conf_print_hyprland_rules_3 | sudo tee debian/rules

# debian/rules are Makefiles and requires tabs so '-EOF' breaks
# the output of the heredoc. So hardleft for this one.
conf_print_hyprland_rules() {
	cat <<'EOF'
#!/usr/bin/make -f

PREFIX=$(CURDIR)/tmp/usr
%:
	dh $@ --buildsystem=meson+ninja --builddirectory=build

override_dh_auto_configure:
	dh_auto_configure -- --no-warn-unused-cli -DCMAKE_BUILD_TYPE:STRING=Release -DNO_SYSTEMD:STRING=true -DCMAKE_INSTALL_PREFIX:STRING=$(PREFIX) -S .

override_dh_auto_build:
	dh_auto_build -- all

override_dh_auto_test:
	true

override_dh_auto_install:
	$(MAKE) install DESTDIR=$(CURDIR)/tmp/usr prefix=/usr
EOF
}
# conf_print_hyprland_rules | sudo tee debian/rules

# mkdir -p debian/patches
conf_print_hyprland_make_patch() {
	cat <<-'EOF'
		Description: correct installation prefix
		Forwarded: not needed
		diff --git a/Makefile b/Makefile
		index 9c5e94d..08f7ae8 100644
		--- a/Makefile
		+++ b/Makefile
		@@ -1,4 +1,4 @@
		-PREFIX = /usr/local
		+PREFIX = $(CURDIR)/debian/tmp/usr

		legacyrenderer:
		  cmake --no-warn-unused-cli -DCMAKE_BUILD_TYPE:STRING=Release -DCMAKE_INSTALL_PREFIX:STRING=${PREFIX} -DLEGACY_RENDERER:BOOL=true -S . -B ./build -G Ninja
	EOF
}
# conf_print_hyprland_make_patch | sudo tee debian/patches/00-makefile.patch

conf_print_hyprland_make_series() {
	cat <<-'EOF'
		00-makefile.patch
	EOF
}
# conf_print_hyprland_make_series | sudo tee debian/patches/series

# Function to append build dependencies to debian/control file
append_build_deps() {
	# Use name reference to access the original array passed as an argument
	local -n DEBIAN_BUILD_DEPS=$1
	local control_file=$2

	# Temporary file to hold the updated control file content
	local temp_control_file="temp_control_file"

	# Create an associative array to filter out duplicates
	declare -A unique_deps
	for dep in "${DEBIAN_BUILD_DEPS[@]}"; do
		unique_deps["$dep"]=1
	done

	# Read the existing control file and append the new unique dependencies
	while IFS= read -r line; do
		echo "$line" >>"$temp_control_file"
		if [[ $line == *"Build-Depends:"* ]]; then
			for dep in "${!unique_deps[@]}"; do
				echo " $dep," >>"$temp_control_file"
			done
		fi
	done <"$control_file"

	# Replace the original control file with the updated one
	mv "$temp_control_file" "$control_file"
}

missing=(
	"vulkan-validationlayers-dev"
	"libwayland-egl-backend-des"
	"libxcb-errors-dev"
)

DEBIAN_BUILD_DEPS=(
	"build-essential"
	"check"
	"cmake"
	"cmake-extras"
	"dh-autoreconf"
	"dh-make"
	"dh-cmake"
	"edid-decode"
	"fontconfig"
	"gettext"
	"gettext-base"
	"glslang-tools"
	"git"
	"hwdata"
	"meson"
	"ninja-build"
	"seatd"
	"wget"
	"wayland-protocols"
	"util-linux"
	"vulkan-utility-libraries-dev"
	"xdg-desktop-portal-wlr"
	"xwayland"
	"libavcodec-dev"
	"libavformat-dev"
	"libavutil-dev"
	"libblkid-dev"
	"libdrm2"
	"libdisplay-info-bin"
	"libdisplay-info-dev"
	"libdisplay-info1"
	"libliftoff-dev"
	"libliftoff0"
	"libegl1-mesa-dev"
	"libegl-dev"
	"libelogind-dev"
	"libffi-dev"
	"libfontconfig-dev"
	"libgbm1"
	"libgbm-dev"
	"libgles2"
	"libgles2-mesa-dev"
	"libgulkan-dev"
	"libinput10"
	"libinput-bin"
	"libinput-dev"
	"libpango1.0-dev"
	"libpixman-1-0"
	"libpixman-1-dev"
	"libseat-dev"
	"libtomlplusplus3"
	"libudev1"
	"libudev-dev"
	"libvkfft-dev"
	"libvulkan1"
	"libvulkan-dev"
	"libvulkan-volk-dev"
	"libwayland-dev"
	"libx11-xcb-dev"
	"libxcb1"
	"libxcb1-dev"
	"libxkbcommon0"
	"libxcb-composite0-dev"
	"libxcb-dri3-dev"
	"libxcb-ewmh2"
	"libxcb-ewmh-dev"
	"libxcb-icccm4-dev"
	"libxcb-image0-dev"
	"libxcb-present-dev"
	"libxcb-render-util0"
	"libxcb-render-util0-dev"
	"libxcb-res0-dev"
	"libxcb-util1"
	"libxcb-util-dev"
	"libxcb-xinput-dev"
	"libxkbcommon-x11-dev"
	"libxkbcommon-dev"
	"libxkbregistry-dev"
	"libxml2-dev"
)
# "libxcb-errors0"
# "libxcb-errors0-dev"

sudo apt install -y "${DEBIAN_BUILD_DEPS[@]}"

# FIXME: select the correct combination of build deps from
# above to be added to the debian/control file.
# The Build-Dep section is a multi-line content.
#
# Add build deps to the debian/control file
# convert array to comma separated list for sed.
BUILD_DEPS=$(
	IFS=','
	echo "${PACKAGES[*]}"
)
# sed -i -e '$a\' -e "Build-Depends: $BUILD_DEPS" debian/control

# Set github user and project name
USER="hyprwm"
PROJECT="Hyprland"
LICENSE="bsd" # Adjust based on the actual license
SECTION="x11/wm"
HOMEPAGE="https://github.com/hyprwm/Hyprland"
PACKAGE="hyprland"
VERSION=$(curl "https://api.github.com/repos/${USER}/${PROJECT}/tags?per_page=5" | jq -r '.[0] | .name')
VERSION=${VERSION:1} # strip the preceding 'v'
TIMESTAMP=$(date -R) # This generates the current date and time in RFC 2822 format
BUILD_HOME="/var/tmp/hyprland-${VERSION}-git_build"
DEBEMAIL="cyteen@ring-zero.co.uk"
DEBFULLNAME="Cyteen May"
# DEBIAN_BRANCH="suites/excalibur"
DEBIAN_BRANCH="suites/unstable"
DEBIAN_SUITE="${DEBIAN_BRANCH##*/}"
DEBIAN_MIRROR="http://deb.devuan.org/merged"
DEBIAN_ARCH="$(dpkg --print-architecture)"
DEBIAN_INCLUDE="ca-certificates,eatmydata,ccache,git"
DEBIAN_TAG='debian/%(version)s'
UPSTREAM_BRANCH="upstream/latest"
# UPSTREAM_TAG='v%(version)s'
UPSTREAM_TAG='upstream/v%(version)s'
TARGET_DIR="/srv/chroot/unstable-${DEBIAN_ARCH}-sbuild"
DESCRIPTION="Hyprland is a dynamic tiling Wayland compositor based on wlroots,"
LONG_DESCRIPTION="Hyprland is a highly customizable dynamic tiling Wayland compositor that doesn't sacrifice on its looks. It provides the latest Wayland features, is highly customizable, has all the eyecandy, the most powerful plugins, easy IPC, much more QoL stuff than other wlr-based compositors and more..."

# Check if the target directory exists
if [ ! -d "${TARGET_DIR}" ]; then
	echo "Target directory ${TARGET_DIR} does not exist. Creating chroot environment..."
	sudo sbuild-createchroot \
		--include="${DEBIAN_INCLUDE}" \
		--merged-usr \
		--make-sbuild-tarball=/srv/chroot/${DEBIAN_SUITE}-${DEBIAN_ARCH}.tar.gz \
		${DEBIAN_SUITE} ${TARGET_DIR} ${DEBIAN_MIRROR}
else
	echo "Target directory ${TARGET_DIR} already exists. Skipping chroot creation."
fi

# Delete the schroot
# sudo rm -r /srv/chroot/unstable-amd64-sbuild/
# sudo rm /etc/schroot/chroot.d/unstable-amd64-sbuild-* /etc/sbuild/chroot/unstable-amd64-sbuild

# Make the BUILD_DEST for git and gbp changeable from just PACKAGE
# gbp prefers no version number in the containing directory.
# BUILD_DEST="${BUILD_HOME}/${PACKAGE}_${VERSION}"
BUILD_DEST="${BUILD_HOME}/${PACKAGE}"
SUBPROJECTS_DEST="${BUILD_DEST}/subprojects"

# Make the BUILD_HOME directory and clone upstream
mkdir -p ${BUILD_HOME}

cd ${BUILD_HOME} || exit

# git clone --recurse-submodules https://github.com/${USER}/${PROJECT}.git ${BUILD_DEST}
# gbp clone https://github.com/${USER}/${PROJECT}.git ${BUILD_DEST}
# git clone https://github.com/${USER}/${PROJECT}.git ${BUILD_DEST}

# git clone --no-checkout -o ${UPSTREAM_BRANCH} https://github.com/${USER}/${PROJECT}.git ${BUILD_DEST}

gbp clone \
	--pristine-tar \
	--debian-branch=${DEBIAN_BRANCH} \
	--upstream-branch=${UPSTREAM_BRANCH} \
	--add-upstream-vcs \
	--verbose \
	https://github.com/${USER}/${PROJECT}.git ${BUILD_DEST}

cd ${BUILD_DEST} || exit
git checkout -b ${DEBIAN_BRANCH} ${VERSION}

#==============================================================================
# debianize the BUILD_HOME directory
#==============================================================================

# prepare Debian packaging from an original source archive
dh_make \
	--createorig \
	--single \
	--native \
	--copyright ${LICENSE} \
	--packagename ${PACKAGE}_${VERSION} \
	--yes

rm debian/*.ex

mkdir -p debian

# Install the conf_print heredocs
conf_print_github_watch | sudo tee debian/watch
chmod +x debian/watch
conf_print_debian_control | sudo tee debian/control
conf_print_hyprland_copyright | tee debian/copyright

conf_print_hyprland_common_install | tee debian/hyprland-common.install
conf_print_hyprland_common_substvars | tee debian/hyprland-common.substvars

conf_print_hyprland_dev_install | sudo tee debian/hyprland-dev.install
conf_print_hyprland_dev_substvars | tee debian/hyprland-dev.substvars

conf_print_hyprland_install | tee debian/hyprland.install
conf_print_hyprland_substvars | tee debian/hyprland.substvars

conf_print_hyprland_completions_install | tee debian/hyprland-completions.install
conf_print_hyprland_completions_substvars | tee debian/hyprland-completions.substvars

# conf_print_initial_changelog | sudo tee debian/changelog

conf_print_hyprland_docs_install | sudo tee debian/hyprland-docs.install

# conf_print_hyprland_preinst | sudo tee debian/hyprland.preinst
# chmod +x debian/hyprland.preinst

# conf_print_hyprland_postinst | sudo tee debian/hyprland.postinst
# chmod +x debian/hyprland.postinst

# if we are using the makefile we don't need to call this from the debian/rules
# conf_print_hyprland_setup | sudo tee debian/install.sh
# chmod +x debian/install.sh

# conf_print_hyprland_prerm | tee debian/hyprland.prerm
# chmod +x debian/hyprland.prerm

# conf_print_hyprland_postrm | tee debian/hyprland.postrm
# chmod +x debian/hyprland.postrm

conf_print_hyprland_headers | sudo tee debian/install-headers.sh
chmod +x debian/install-headers.sh

# we now remake the patch using the patch-queue branch
# conf_print_hyprland_make_patch | sudo tee debian/patches/00-makefile.patch
# conf_print_hyprland_make_series | sudo tee debian/patches/series

# conf_print_hyprland_rules | sudo tee debian/rules
# conf_print_hyprland_rules_1 | sudo tee debian/rules
# conf_print_hyprland_rules_2 | sudo tee debian/rules
conf_print_hyprland_rules_3 | sudo tee debian/rules

#==============================================================================
# Call the append function to add the build deps to the controlfile.
# append_build_deps DEBIAN_BUILD_DEPS debian/control

# Customize the debian directory values
# sed -i 's,^\(Section: \).*,\1'${SECTION}',' debian/control
# sed -i "s,^\(Maintainer: \).*,\1'${DEBFULLNAME}\ \<${DEBEMAIL}\>'," debian/control
# sed -i 's,^\(Homepage: \).*,\1'${HOMEPAGE}',' debian/control
# sed -i "s,^\(Description: \).*,\1${DESCRIPTION}," debian/control
# sed -i "s|.*insert long.*|${LONG_DESCRIPTION}|" debian/control

sed -i \
	-e "s|^\(Section: \).*|\1${SECTION}|" \
	-e "s|^\(Maintainer: \).*|\1'${DEBFULLNAME}\ \<${DEBEMAIL}\>'|" \
	-e 's|^\(Homepage: \).*|\1'${HOMEPAGE}'|' \
	-e "s|^\(Description: \).*|\1${DESCRIPTION}|" \
	-e "s|.*insert long.*|${LONG_DESCRIPTION}|" \
	debian/control

#   ./debian/install-headers.sh $(CURDIR) && \

# mkdir -p $(CURDIR)/debian/hyprland/usr/include/hyprland/wlroots && \
# mv $(CURDIR)/debian/hyprland/usr/include/wlr \
#   $(CURDIR)/debian/hyprland/usr/include/hyprland/wlroots && \
# rm -f $(CURDIR)/debian/hyprland/usr/lib/libwlroots.so && \
# rm -rf $(CURDIR)/debian/hyprland/usr/lib/pkgconfig && \
# rm -rf $(CURDIR)/debian/hyprland/usr/share/xdg-desktop-portal && \

# https://salsa.debian.org/debian/debian-package-init/-/blob/master/deb_create_watch.py
# outputs to debian/watch/ With deb_create_watch.py in your path
EXEC_PREFIX=${HOME}/.local/bin
wget -c -O ${EXEC_PREFIX}/deb_create_watch.py https://salsa.debian.org/debian/debian-package-init/-/raw/master/deb_create_watch.py?inline=false

python3 ${EXEC_PREFIX}/deb_create_watch.py https://github.com/${USER}/${PROJECT}
# version=4
#
# opts=filenamemangle=s/.+\/v?(\d\S*)\.tar\.gz/hyprlang-$1\.tar\.gz/ \
#  https://github.com/hyprwm/hyprlang/tags .*/v?(\d\S*)\.tar\.gz

#==============================================================================
# Update Changelog
#==============================================================================

#--snapshot \
#	--commit \
#	--commit-msg="Commit after debian control pruned of dh_make ex." \

# switch to the branch the you wish to contain the debian directory
git checkout -b ${DEBIAN_BRANCH}

DEBEMAIL="cyteen@ring-zero.co.uk" DEBFULLNAME="Cyteen May" \
	gbp dch \
	--auto \
	--local=devuan \
	--no-git-author \
	--force-distribution \
	--new-version=${VERSION} \
	--release \
	--upstream-branch=${UPSTREAM_BRANCH} \
	--upstream-tag=${UPSTREAM_TAG} \
	--distribution=${DEBIAN_SUITE} \
	--debian-branch=${DEBIAN_BRANCH} \
	--debian-tag=${DEBIAN_TAG}

#==============================================================================
# Commit the debian directory to the debian branch
git add debian
git commit -m "Post gbp dch commit upstream changelog"
#==============================================================================

#==============================================================================
# Patch / rebase
#==============================================================================

# This drops you back to the patch branch
# sudo gbp pq --time-machine=10 --force import

# alter the Makefile to install to the debian directpry
# sed -i '1s|^PREFIX = /usr/local|PREFIX = $(CURDIR)/debian/tmp/usr|' Makefile
# git add Makefile

# Export the Patches and Commit the Changes:
# sudo gbp pq export --commit -v
#==============================================================================

# Post patch / rebase commit.
# git add debian/patches
# git commit -m "Alter Makefile for Debian directory installation"

# AUR patch https://pastebin.com/7WPZ7ewq

# we ask gbp buildpackage to deal with dubmodules.
# git submodule update --init --recursive

# git config submodule.subprojects/wlroots-hyprland.url "${SUBPROJECTS_DEST}/wlroots-hyprland"
# git config submodule.subprojects/hyprland-protocols.url "${SUBPROJECTS_DEST}/hyprland-protocols"
# git config submodule.subprojects/udis86.url "${SUBPROJECTS_DEST}/udis86"
# git config submodule.subprojects/tracy.url "${SUBPROJECTS_DEST}/tracy"

# git -C subprojects/wlroots-hyprland reset --hard
# sed -E -i -e "s/(soversion = .*$)/soversion = 13032/g" subprojects/wlroots-hyprland/meson.build

# hyprland/subprojects submodules.
# git+https://github.com/canihavesomecoffee/udis86.git
# git+https://github.com/hyprwm/hyprland-protocols.git
# git+https://github.com/hyprwm/Hyprland.git
# git+https://github.com/hyprwm/wlroots-hyprland.git
# git+https://github.com/wolfpld/tracy.git

#==============================================================================
## dpkg-buildpackage -us -uc -b
#gbp buildpackage     \
#  --git-pristine-tar \  # Use pristine-tar to create orig tarball
#  --git-submodules   \  # Transparently handle submodules in the upstream tree
#  --git-ignore-new   \  # Build with uncommitted changes in the source tree
#  --git-debian-branch=${DEBIAN_BRANCH} \  # Branch the Debian package is being developed on
#  --git-upstream-branch=${UPSTREAM_BRANCH} \
#  --git-export-dir="../build-area" \      # before building the package export the source into EXPORT_DIR
#  --git-tag             # create a tag after a successful build

# git checkout -b ${DEBIAN_BRANCH}

# Adding gitea package registry - https://docs.gitea.com/usage/packages/debian
# "deb [signed-by=/etc/apt/keyrings/gitea-{owner}.asc] https://{username}:{your_password_or_token}@gitea.example.com/api/packages/{owner}/debian {distribution} {component}"
# sudo curl https://gitea.example.com/api/packages/{owner}/debian/repository.key -o /etc/apt/keyrings/gitea-{owner}.asc￼
￼
# upload dev to gitea registry
# curl --user your_username:your_password_or_token \
#      --upload-file path/to/file.deb \
#      https://gitea.example.com/api/packages/testuser/debian/pool/bionic/main/upload￼

EXTRA_PACKAGES="/space/code_repositories/hyprland/" # directory containing extra debian packages (.deb)
GITEA_OWNER="cyteen"
GITEA_PASS="mlpfinsonik"
GITEA_URL="git2.ring-zero.co.uk"
# GITEA_TOKEN="e844ba5fa9f57645eb1e32cfcb67a60d650dfbf0"
# GITEA_TOKEN="25f01d539f3eea5edf2625f825fb84050af080e4"

# the gitea asc key
# GITEA_KEY="$(curl https://${GITEA_URL}/api/packages/${GITEA_OWNER}/debian/repository.key)"

# GITEA_KEY="/etc/apt/trusted.gpg.d/gitea-cyteen.gpg"
# GITEA_KEY="../gitea-cyteen.gpg"
# GITEA_KEY="$(curl -fsSL https://${GITEA_URL}/api/packages/${GITEA_OWNER}/debian/repository.key | gpg --dearmor -q)"
# sudo curl -fsSL https://${GITEA_URL}/api/packages/${GITEA_OWNER}/debian/repository.key | gpg --dearmor -q -o ${GITEA_KEY}

# sbuild called for an asc key file which is what gitea provides, and puts it in:
# /etc/apt/trusted.gpg.d/sbuild-extra-repository-IDqCM94OpA.gpg
# GITEA_KEY="/etc/apt/keyrings/gitea-cyteen.asc"

GITEA_KEY="${BUILD_HOME}/gitea-cyteen.asc"
sudo curl https://${GITEA_URL}/api/packages/${GITEA_OWNER}/debian/repository.key | sudo tee ${GITEA_KEY}

GITEA_DIST="ceres"
GITEA_COMP="main"
GITEA_LIST="deb http://deb.debian.org/debian experimental main"

# echo "deb https://gitea.example.com/api/packages/{owner}/debian {distribution} {component}" | sudo tee -a /etc/apt/sources.list.d/gitea.list
# GITEA_LIST="deb [trusted=yes check-date=no] https://${GITEA_URL}/api/packages/${GITEA_OWNER}/debian ${GITEA_DIST} ${GITEA_COMP}"

# we should use apt-secure - see automate/020_gitea-dpr.sh
GITEA_LIST="deb https://${GITEA_OWNER}:${GITEA_PASS}@${GITEA_URL}/api/packages/${GITEA_OWNER}/debian ${GITEA_DIST} ${GITEA_COMP}"
# GITEA_LIST="deb [signed-by=${GITEA_KEY}] https://${GITEA_URL}/api/packages/${GITEA_OWNER}/debian ${GITEA_DIST} ${GITEA_COMP}"

# full url  https://${GITEA_OWNER}:${GITEA_PASS}@git2.ring-zero.co.uk/api/packages/${GITEA_OWNER}/debian/pool/${GITEA_DIST}/main/upload/api/packages/${GITEA_OWNER}/debian ${GITEA_DIST} ${GITEA_COMP}"
# echo "deb https://{username}:{your_password_or_token}@gitea.example.com/api/packages/{owner}/debian {distribution} {component}" | sudo tee -a /etc/apt/sources.list.d/gitea.list
# GITEA_LIST="deb https://${GITEA_OWNER}:${GITEA_PASS}@git2.ring-zero.co.uk/api/packages/${GITEA_OWNER}/debian ${GITEA_DIST} ${GITEA_COMP}"

# mimic the resolver behaviour used by the official buildds:

# add experimental to unstable = see: https://wiki.debian.org/sbuild#CA-4db577cbb51eedbbe4205d14af637002f7fbb9c0_1
ASPCUD_CRITERIA='-count(solution,APT-Release:=/a=experimental/),-removed,-changed,-new'
# --extra-repository='deb http://deb.debian.org/debian experimental main' \
# --build-dep-resolver=aspcud \
# --aspcud-criteria ${ASPCUD_CRITEREA}

DEPENDS="hyprland-protocols, \
  debhelper-compat, \
  edid-decode, \
  hyprwayland-scanner >=0.3.1, \
	jq, \
  pkg-config, \
  util-linux, \
  vulkan-utility-libraries-dev, \
  wget, \
  libblkid-dev, \
  libgbm1, \
	libhyprlang2, \
	libhyprlang-dev \
  libinput10, \
  libudev1, \
  libvulkan1, \
  libcap-dev, \
  libcairo2-dev, \
  libdisplay-info1, \
  libdisplay-info-bin, \
  libpng-dev, \
  libxcb-util1, \
  libxcb-util-dev, \
  libxcb-render0-dev, \
  libxcb-shm0-dev, \
  libxcb-xfixes0-dev, \
  "
# libwlroots-dev, \

# all options to gbp not starting with --git- to the builder, or add to GIT_BUIDLER
GIT_BUILDER='sbuild --arch-all --source --source-only-changes --verbose --dist=unstable'

#	--extra-package="${EXTRA_PACKAGES}" \
#	devuan specific https://git.devuan.org/devuan/documentation/src/branch/master/maintainers/PackagingGuide.md:
#	gbp buildpackage \
#	--git-pbuilder \
#	--git-dist=ceres \
#	create \
#	--mirror http://pkgmaster.devuan.org/merged
#
sudo gbp buildpackage \
	--add-depends="${DEPENDS}" \
	--extra-repository="${GITEA_LIST}" \
	--extra-repository-key="${GITEA_KEY}" \
	--build-dep-resolver=aptitude \
	--git-ignore-new \
	--git-ignore-branch \
	--git-pristine-tar \
	--git-submodules \
	--git-builder="${GIT_BUILDER}" \
	--git-debian-branch="${DEBIAN_BRANCH}" \
	--git-debian-tag="${DEBIAN_TAG}" \
	--git-upstream-tag="${UPSTREAM_TAG}" \
	--git-upstream-branch="${UPSTREAM_BRANCH}" \
	--git-export-dir="../build-area"

# sudo dpkg -i ../${PACKAGE}_${VERSION}_amd64.deb

# Prevent hanging schroot sessions.
# list all sessions with:  schroot -l --all
#
# Re-enter the session
# sudo schroot -c session:unstable-amd64-sbuild-c57f64a1-ea97-4627-bbc4-2f9b03f45015

# schroots are mounted under:  /var/run/schroot/mount/

# Stop the session
# sudo schroot --end-session $(schroot -l --all | grep 'session:unstable-amd64-sbuild-' | awk '{print $1}')
