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

# https://github.com/WillPower3309/swayfx/
# https://github.com/WillPower3309/swayfx/blob/master/INSTALL-deb.md
sudo apt install \
	meson \
	wayland-protocols \
	wayland \
	libpcre2-dev \
	libjson-c-dev \
	libpango-1.0-0 \
	libcairo2-dev \
	wget

conf_print_debian_control() {
	cat <<EOF
Source: swayfx
Section: x11
Priority: optional
Maintainer: root <root@devuan>
Rules-Requires-Root: no
Build-Depends:
 debhelper-compat (= 13),
Standards-Version: 4.7.0
Homepage: https://github.com/WillPower3309/swayfx/
#Vcs-Browser: https://github.com/WillPower3309/swayfx/
#Vcs-Git: https://github.com/WillPower3309/swayfx.git

Package: swayfx
Architecture: any
Multi-Arch: foreign
Depends:
 ${shlibs:Depends},
 ${misc:Depends},
Description: "swayfx is the sway compositor with fancy GLES2 effects"
 "Swayfx is a fork of sway window manager. This fork ditches the simple wlr_renderer, and replaces it with our fx_renderer, capable of rendering with fancy GLES2 effects."
EOF
}
# conf_print_debian_control | sudo tee debian/control

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

# Include debhelper.mk
include /usr/share/dpkg/default.mk

# Define the build directory
BUILDDIR = $(CURDIR)/build

# Define the build and install targets for wlroots
override_dh_auto_build:
	meson $(WLROOTS_BUILD)
	ninja -C $(WLROOTS_BUILD)

# Define the build and install targets for swayfx
override_dh_auto_build_swayfx:
	meson $(SWAYFX_BUILD)
	ninja -C $(SWAYFX_BUILD)

override_dh_auto_install_swayfx:
	ninja -C $(SWAYFX_BUILD) install

# Define the clean target
clean:
	rm -rf $(BUILDDIR)
EOF
}
# conf_print_debian_rules | sudo tee debian/rules >/dev/null

# Set github user and project name
USER="WillPower3309"
PROJECT="swayfx"
LICENSE="expat" # Adjust based on the actual license
SECTION="x11/wm"
HOMEPAGE="https://github.com/WillPower3309/swayfx"
PACKAGE="swayfx"
TAG=$(curl "https://api.github.com/repos/${USER}/${PROJECT}/tags?per_page=5" | jq -r '.[0] | .name')
VERSION=${TAG:1}     # strip the preceding 'v'
TIMESTAMP=$(date -R) # This generates the current date and time in RFC 2822 format
BUILD_HOME="/var/tmp/swayfx-${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="swayfx is the sway compositor with fancy GLES2 effects"
LONG_DESCRIPTION="Swayfx is a fork of sway window manager. This fork ditches the simple wlr_renderer, and replaces it with our fx_renderer, capable of rendering with fancy GLES2 effects."

# 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

# 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}-${TAG##v}"
SUBPROJECTS_DEST="${BUILD_DEST}/subprojects"

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

cd ${BUILD_HOME} || exit

# fetch wlroots
# WLROOTS_RELEASE=$(lastversion --at gitlab wlroots/wlroots)
# WLROOTS_RELEASE="0.17.3"
WLROOTS_RELEASE="0.16.2"
git clone -b ${WLROOTS_RELEASE} --depth 1 https://gitlab.freedesktop.org/wlroots/wlroots.git wlroots-${WLROOTS_RELEASE}

git clone -b ${TAG} --depth 1 https://github.com/${USER}/${PROJECT}.git ${BUILD_DEST}

# https://github.com/WillPower3309/swayfx.git
# 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
cd ${BUILD_DEST}
dh_make \
	--createorig \
	--single \
	--native \
	--copyright ${LICENSE} \
	--packagename ${PACKAGE}_${VERSION} \
	--yes

rm debian/*.ex

mv debian ..
cd ..

#==============================================================================
# edit the debian files
#==============================================================================

# conf_print function calls go here:
conf_print_debian_control | sudo tee debian/control
conf_print_debian_rules | sudo tee debian/rules

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

# 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}

#==============================================================================
# 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"

GITEA_OWNER="cyteen"
GITEA_PASS="mlpfinsonik"
GITEA_URL="git2.ring-zero.co.uk"

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"

GITEA_LIST="deb https://${GITEA_OWNER}:${GITEA_PASS}@${GITEA_URL}/api/packages/${GITEA_OWNER}/debian ${GITEA_DIST} ${GITEA_COMP}"

DEPENDS=""

# 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}" \
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"
