#!/bin/bash

. "$(dirname "$(readlink -e "$0")")/config"
. "$(dirname "$(readlink -e "$0")")/db-functions"

shopt -s nullglob

if [ $# -ge 1 ]; then
	warning "Calling %s with a specific repository is no longer supported" "${0##*/}"
	exit 1
fi

# Find repos with packages to release
repos=($(find  "${STAGING}" -mindepth 1 -maxdepth 1 -type d ! -empty -printf '%f ' 2>/dev/null))
if [ $? -ge 1 ]; then
	die "Could not read %s" "${STAGING}"
fi

# TODO: this might lock too much (architectures)
for repo in "${repos[@]}"; do
	for pkgarch in "${ARCHES[@]}"; do
		repo_lock "${repo}" "${pkgarch}" || exit 1
	done
done

# check if packages are valid
for repo in "${repos[@]}"; do
	pkgs=($(getpkgfiles "${STAGING}/${repo}/"*${PKGEXT}))
	if [ $? -eq 0 ]; then
		if [ ${#pkgs[@]} -gt 0 ] && ! check_repo_permission "${repo}"; then
			die "You don't have permission to update packages in %s" "${repo}"
		fi
		for pkg in "${pkgs[@]}"; do
			if [ -h "${pkg}" ]; then
				die "Package %s is a symbolic link" "${repo}/${pkg##*/}"
			fi
			if ! check_pkgfile "${pkg}"; then
				die "Package %s is not consistent with its meta data" "${repo}/${pkg##*/}"
			fi
			if ! check_pkgrepos "${pkg}"; then
				die "Package %s already exists in another repository" "${repo}/${pkg##*/}"
			fi
		done
		# This is fucking obnoxious
		#if ! check_splitpkgs ${repo} "${pkgs[@]}"; then
		#	die "Missing split packages for %s" "${repo}"
		#fi
	else
		die "Could not read %s" "${STAGING}"
	fi
done

for repo in "${repos[@]}"; do
	msg "Updating [%s]..." "${repo}"
	any_pkgs=($(getpkgfiles "${STAGING}/${repo}/"*-any${PKGEXT} 2>/dev/null))
	for pkgarch in "${ARCHES[@]}"; do
		add_pkgs=()
		arch_pkgs=($(getpkgfiles "${STAGING}/${repo}/"*-"${pkgarch}"${PKGEXT} 2>/dev/null))
		for pkg in "${arch_pkgs[@]}" "${any_pkgs[@]}"; do
			pkgfile="${pkg##*/}"
			msg2 "%s (%s)" "${pkgfile}" "${pkgarch}"
			# any packages might have been moved by the previous run
			if [ -f "${pkg}" ]; then
				mv "${pkg}" "$FTP_BASE/${PKGPOOL}"
			fi
			ln -s "../../../${PKGPOOL}/${pkgfile}" "$FTP_BASE/$repo/os/${pkgarch}"
			# also move signatures
			if [ -f "${pkg}.sig" ]; then
				mv "${pkg}.sig" "$FTP_BASE/${PKGPOOL}"
			fi
			if [ -f "$FTP_BASE/${PKGPOOL}/${pkgfile}.sig" ]; then
				ln -s "../../../${PKGPOOL}/${pkgfile}.sig" "$FTP_BASE/$repo/os/${pkgarch}"
			fi
			add_pkgs+=("${pkgfile}")
		done
		if [ ${#add_pkgs[@]} -ge 1 ]; then
			arch_repo_add "${repo}" "${pkgarch}" "${add_pkgs[@]}"
		fi
	done
done

for repo in "${repos[@]}"; do
	for pkgarch in "${ARCHES[@]}"; do
		repo_unlock "${repo}" "${pkgarch}"
	done
done

cd "${STAGING}"
while read -r file; do
	pub="${FTP_BASE}/${file}"
	if [[ -f $pub ]]; then
		warning "file already exists: %s" "${file}"
	else
		mkdir -p -- "${pub%/*}"
		mv -vn "$file" "$pub"
	fi
done < <(find other sources -type f)