#!/usr/bin/env bash

# librerelease - uploads packages to the repo server and publishes them
#
# Copyright (C) 2010-2012  Joshua Ismael Haase Hernández (xihh) <hahj87@gmail.com>
# Copyright (C) 2010-2013  fauno <fauno@parabola.nu>
# Copyright (C) 2013  Michał Masłowski <mtjm@mtjm.eu>
# Copyright (C) 2013-2014, 2017-2018, 2024  Luke T. Shumaker <lukeshu@parabola.nu>
# Copyright (C) 2019-2020, 2022-2024  Bill Auger <bill-auger@programmer.net>
#
# For just the create_signature() function:
#   Copyright (C) 2006-2013  Pacman Development Team <pacman-dev@lists.archlinux.org>
#   Copyright (C) 2002-2006  Judd Vinet <jvinet@zeroflux.org>
#   Copyright (C) 2005  Aurelien Foret <orelien@chez.com>
#   Copyright (C) 2006  Miklos Vajna <vmiklos@frugalware.org>
#   Copyright (C) 2005  Christian Hamar <krics@linuxforum.hu>
#   Copyright (C) 2006  Alex Smith <alex@alex-smith.me.uk>
#   Copyright (C) 2006  Andras Voroskoi <voroskoi@frugalware.org>
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# This file is part of Parabola Libretools.
#
# Libretools is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Libretools is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Libretools.  If not, see <http://www.gnu.org/licenses/>.

# create_signature() is taken from pacman:makepkg, which is GPLv2+,
# so we take the '+' to combine it with our GPLv3+.

set -euE

source "$(librelib conf)"     # LIBREUSER, load_conf()
source "$(librelib messages)" # setup_traps(), msg(), msg2() error(), print(), prose(), flag()

declare -ri STAGING_LOCK=8
declare -ra RSYNC_FLAGS=(
	--no-group
	--no-perms
	--copy-links
	--hard-links
	--partial
	--human-readable
	--progress
)
DRY_RUN=''       # main()
UPLOAD_ONLY=''   # main()
TIER0_HOST=''    # main()
TIER0_STAGING='' # main()
TIER0_LOGIN=''   # main()
TIER0_PORT=''    # main()
SSH_CMD=()       # main()
RSYNC_DEST=''    # main()

## helpers ##

lock_staging() {
	lock $STAGING_LOCK "${WORKDIR}/staging.lock" \
		"Waiting for an exclusive lock on the staging directory"
}

unlock_staging() {
	lock_close $STAGING_LOCK
}

list0_files() {
	find -L "${WORKDIR}/staging" -type f -not -name '*.lock' \
		-exec realpath -z --relative-to="${WORKDIR}/staging" {} + | sort -z
}

# This function is taken almost verbatim from makepkg
create_signature() {
	local filename="$1"
	msg "Signing package..."

	local SIGNWITHKEY=()
	if [[ -n $GPGKEY ]]; then
		SIGNWITHKEY=(-u "${GPGKEY}")
	fi

	if gpg --detach-sign --use-agent "${SIGNWITHKEY[@]}" \
		--no-armor "$filename" &>/dev/null; then
		msg2 "Created signature file %s." "$filename.sig"
		return $EXIT_SUCCESS
	else
		error "Failed to sign package file."
		return $EXIT_FAILURE
	fi
}

sign_packages() {
	IFS=$'\n'
	local files=($(find "${WORKDIR}/staging/" -type f -not -iname '*.sig' -print))
	local file
	for file in "${files[@]}"; do
		if [[ -f "${file}.sig" ]]; then
			msg2 "File signature found, verifying..."

			# Verify that the signature is correct, else remove for re-signing
			if ! gpg --quiet --verify "${file}.sig" >/dev/null 2>&1; then
				error "Failed!  Re-signing..."
				rm -f "${file}.sig"
			fi
		fi

		if ! [[ -f "${file}.sig" ]]; then
			                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      