#!/bin/bash
# set -e
# set -x

# Function to display usage information
usage() {
	echo "Usage: $(basename $0) -p <project> -b <builddir>"
	echo " -p <project>   Project name (e.g., kislyuk/yq)" or
	echo " -d <dsc> file"
	echo " -b <builddir> Build directory"
	exit 1
}

# Parse command-line options
while getopts ":p:b:" opt; do
	case ${opt} in
	p)
		PROJECT_NAME=$OPTARG
		;;
	b)
		BUILDDIR=$OPTARG
		;;
	d)
		DSC_FILE=$OPTARG
		;;
	\?)
		usage
		;;
	:)
		echo "Option -$OPTARG requires an argument." >&2
		usage
		;;
	esac
done
shift $((OPTIND - 1))

# Check if project name or dsc file and build directory are provided
# if [[ -z "$PROJECT_NAME" ] || [ -z "$BUILDDIR" ]] | [[ -z "$DSC_FILE" ] || [ -z "$BUILDDIR" ]] ; then
# 	usage
# fi

# Install necessary tools
# sudo apt install -y git-buildpackage equivs
# use apt-get -y for non-interactive operation
# apt-get install apt-cudf-get
apt-get -y install libdistro-info-perl sbuild

# Prepare the sbuild chroot for gbp buildpackage
# DEBIAN_MIRROR="http://httpredir.debian.org/debian"
DEBIAN_MIRROR="http://deb.devuan.org/merged"
DEBIAN_ARCH=$(dpkg --print-architecture)
DEBIAN_SUITE="unstable" # stable, testing, unstable || daedalus, excalibur, ceres
DEBIAN_INCLUDE="eatmydata,ccache"
CHROOT_PREFIX="experimental"
TARGET_DIR="/srv/chroot/unstable-${DEBIAN_ARCH}-sbuild"
# TARGET_DIR="$(mktemp -d)"
# EXTRA_REPO_KEY="/etc/apt/trusted.gpg.d/local.gpg"
# EXTRA_REPO="deb [signed-by=${KEYRING}] https://${USERNAME}:${PASSWORD}@${URL}/api/packages/${OWNER}/debian ${DISTRIBUTION} ${COMPONENT}"

# --chroot-mode=schroot \  # default
# --chroot-prefix="${CHROOT_PREFIX}" \

# 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

# cp /etc/apt/sources.list.d/local.list ${TARGET_DIR}/etc/apt/sources.list.d/
# cp /etc/apt/trusted.gpg.d/local.gpg ${TARGET_DIR}/etc/apt/trusted.gpg.d/
# schroot -c buster-amd64 -- apt-get update
# schroot -c buster-amd64 -- apt-get upgrade
# sbuild -d buster-amd64 -A amd64 package_name

# Function to preprocess version strings
preprocess_version() {
	local version="$1"
	# Remove everything up to and including the first underscore
	version=${version#*_}
	# Remove .dsc extension from the end
	version=${version%.dsc}
	echo "$version"
}

find_newest_version() {
	local versions=("$@") # Use "$@" to correctly handle all arguments as an array
	local newest_version=""

	for i in "${versions[@]}"; do
		local preprocessed_version=$(preprocess_version "$i")
		if [[ -z "$newest_version" ]] || dpkg --compare-versions "$preprocessed_version" gt "$newest_version"; then
			newest_version="$preprocessed_version"
		fi
	done

	echo "$newest_version" # Ensure the variable is correctly referenced
}

# Get the latest version of the project
# LATEST_VERSION=$(lastversion --verbose $PROJECT_NAME)
# LATEST_VERSION="3.3.0"
UPSTREAM_VERSION=$(curl "https://api.github.com/repos/${PROJECT_NAME}/tags?per_page=5" | jq -r '.[0] | .name')
LATEST_VERSION=${UPSTREAM_VERSION#v}
echo "Latest version: $LATEST_VERSION"

# Extract the program name from the project name
PROGRAM_NAME=$(echo $PROJECT_NAME | cut -d '/' -f 2)
echo "Program name: $PROGRAM_NAME"

# Construct the URL to find the Debian package
# Use the first letter of the program name to navigate to the correct directory
BASE_URL="https://deb.debian.org/debian/pool/main/$(echo ${PROGRAM_NAME} | cut -c1)/${PROGRAM_NAME}/"
echo "Base URL: $BASE_URL"

# Fetch the HTML content of the package's directory
# HTML_CONTENT=$(curl --silent $BASE_URL)
# echo "HTML content: $HTML_CONTENT"

## Use the Python script to fetch and filter .dsc file names
DSC_FILES=$(python3 ~/bin/html-to-json.py $BASE_URL | jq -r '.[] | select(.Name | endswith(".dsc")) | .Name')
echo "DSC files: "
echo $DSC_FILES
echo ""

# Assuming $DSC_FILES contains the version strings separated by spaces
IFS=' ' read -d '' -r -a VERSIONS <<<"$DSC_FILES"

# echo "Print VERSIONS array: "
# for i in ${VERSIONS[@]};do
#   preprocess_version $i
# done
# echo ""

# Replace '3.3.0' with the version you want to compare against
NEWEST_VERSION=$(find_newest_version ${VERSIONS[@]})
echo "Newest version: $NEWEST_VERSION"

# Construct the URL for the nearest .dsc file
DSC_FILE_URL="${BASE_URL}${PROGRAM_NAME}_${NEWEST_VERSION}.dsc"
echo "DSC file URL: $DSC_FILE_URL"

# Create a timestamped build directory
FILESTAMP=$(date '+%Y-%m-%d-%H-%M')-$(uuidgen -t)
echo "Filestamp: $FILESTAMP"

BUILD_DIR=${BUILDDIR}/build-${FILESTAMP}
echo "Build directory: $BUILD_DIR"

echo "Building in ${BUILD_DIR}"
mkdir -p ${BUILD_DIR}
cd ${BUILD_DIR}

# Download the .dsc file
# echo "Downloading .dsc file from ${DSC_FILE_URL}"
# curl -O ${DSC_FILE_URL}

# Initialize the git repo
# git init ${PROGRAM_NAME}

# Import the .dsc file
echo "Importing .dsc file"
# sudo gbp import-dsc -v --allow-unauthenticated ${PROGRAM_NAME}_${NEWEST_VERSION}.dsc
UPSTREAM_BRANCH="upstream"
# DEBIAN_BRANCH="suites/experimental"
DEBIAN_BRANCH="master"
# gbp import-dsc [options] /path/to/package.dsc [target]
# sudo gbp import-dsc -v \
# 	--allow-unauthenticated \
# 	--create-missing-branches \
# 	${DSC_FILE_URL}

echo "###############################################################################"
echo "gbp import-dsc starts..."
echo "###############################################################################"
sudo gbp import-dsc -v \
	--allow-unauthenticated \
	--create-missing-branches \
	--debian-branch=${DEBIAN_BRANCH} \
	--upstream-branch=${UPSTREAM_BRANCH} \
	--pristine-tar \
	${DSC_FILE_URL}

# https://deb.debian.org/debian/pool/main/y/yq/yq_3.2.3-1.dsc

if [ $? -ne 0 ]; then
	echo "gbp import-dsc failed to connect."
	exit 1
fi

cd ${PROGRAM_NAME}

# Apply Debian patch
# echo "Applying Debian patch"
#
# We will import, export, add and commit to bring in the quilt patch series and
# convert them to gbp format to get into a consistant state.
#
# NB. gbp pq import will put you on the patch-queue/${DEBIAN_BRANCH} branch automatically
# Read the debian/patches/series file and applies each patch listed in
# the series to the patch queue branch.

# Import Current Patches onto the Patch-Queue Branch:
sudo gbp pq import

# Export the Patches and Commit the Changes:
# gbp pq export will return you to the ${DEBIAN_BRANCH}
sudo gbp pq export --commit

# The export will generate patches and diffs that need to be committed
# sudo git add -u
# sudo git commit -m "refresh patches"

# Import Current Patches onto the Patch-Queue Branch:
# sudo gbp pq import
# Update upstream branch
echo "###############################################################################"
echo "gbp import-orig starts..."
echo "###############################################################################"
UPSTREAM_TAG='v%(version)s'
sudo gbp import-orig \
	--pristine-tar \
	--debian-branch=${DEBIAN_BRANCH} \
	--upstream-branch=${UPSTREAM_BRANCH} \
	--import-msg="New upstream version v%(version)s" \
	--upstream-tag=${UPSTREAM_TAG} \
	--uscan
#==============================================================================
# Patch / rebase
#==============================================================================

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

# example interactively issue 'git rebase --skip'
# git rebase --interactive ${DEBIAN_BRANCH}
sudo gbp pq rebase
# Export the Patches and Commit the Changes:
sudo gbp pq export --commit

# Now you can do your work/add addition patches
# git checkout patch-queue/${DEBIAN_BRANCH}
# gbp pq import

# gbp pq import --force --time-machine=10

# Update within the git repo
# git remote add upstream git@github.com:${PROJECT_NAME}.git
# git fetch upstream
# git cherry-pick ${CHERRY}
# gbp pq rebase --upstream-tag=${UPSTREAM_TAG}

# sudo git add -u
# sudo git commit -m "new upstream release ${UPSTREAM_TAG}"
# sudo git pq export

# Update the changelog and add and commit
# DEBMAIL=cyteen@ring-zero.co.uk
# sudo DEBMAIL=${DEBMAIL} gbp dch \
echo "###############################################################################"
echo "gbp dch starts..."
echo "###############################################################################"
sudo DEBEMAIL=cyteen@ring-zero.co.uk DEBFULLNAME="Cyteen May" \
	gbp dch \
	--commit \
	--commit-msg="Update changelog for %(version)s release" \
	--local=devuan \
	--no-git-author \
	--new-version=${LATEST_VERSION} \
	--release \
	--distribution=unstable

# handled by --commit above
# sudo git add debian/
# sudo git commit -m "Update debian/changelog"

# Install the build dependencies
echo "###############################################################################"
echo "Installing build dependencies"
echo "###############################################################################"
echo "mk-build-deps starts..."
# https://unix.stackexchange.com/a/608811
TOOL='apt-get -y'
# TOOL='apt-cudf-get --solver aspcud -o APT::Get::Assume-Yes=1 -o Debug::pkgProblemResolver=0 -o APT::Install-Recommends=0'
sudo mk-build-deps \
	--install \
	--remove \
	--root-cmd sudo \
	--tool="${TOOL}" \
	debian/control

# Update outside the git repo to latest via uscan and debian/watch
# uscan should call
# sudo uscan --verbose
# sudo uupdate --find --upstream-version ${LATEST_VERSION}
# sudo uupdate --upstream-version ${LATEST_VERSION} ../${PROGRAM_NAME}_${LATEST_VERSION}.orig.tar.gz
# cd ../${PROGRAM_NAME}-${LATEST_VERSION}

# Build the packages
echo "###############################################################################"
echo "Building the package"
echo "###############################################################################"
echo "gbp buildpackage starts..."
# sudo dpkg-buildpackage -us -uc -b

# to use gbp you must be in a git repo, using uscan/uupdate
# to create the new source dir does not create a new git repo.
#
#
#   gbp buildpackage \
#    --git-ignore-branch \
#    --git-ignore-new \
#    --git-no-create-orig \
#    --git-export-dir=${WORKING_DIR} \
#    --no-check-builddeps \
#    --git-builder=/bin/true \
#    --git-no-pbuilder \
#    --git-no-hooks \
#    --git-no-purge \
#    --git-overlay

sudo gbp buildpackage \
	--git-pristine-tar \
	--git-submodules \
	--git-debian-branch=${DEBIAN_BRANCH} \
	--git-export-dir="../build-area"

# # Build the source package
# sudo gbp buildpackage \
# 	--git-builder='debuild -S' \
# 	--git-pristine-tar \
# 	--git-submodules \
# 	--git-debian-branch=${DEBIAN_BRANCH}

# # Install the built package
# if [ -f ../${PROGRAM_NAME}_${LATEST_VERSION}*.deb ]; then
# 	echo "Installing the package"
# 	sudo dpkg -i ../${PROGRAM_NAME}_${LATEST_VERSION}*.deb
# 	sudo apt install -y -f
# else
#	 echo "No .deb files to install, did the package build complete successfully?"
# fi
