#!/bin/bash

set -eE -o pipefail
. "$(dirname "$(readlink -e "$0")")/../config"
. "$(dirname "$(readlink -e "$0")")/../db-functions"

clean_pkg() {
	local pkg
	local target

	if [[ $CLEANUP_DRYRUN != true ]]; then
		for pkg in "$@"; do
			if [[ -h $pkg ]]; then
				rm -f "$pkg" "$pkg.sig"
			else
				mv_acl "$pkg" "$CLEANUP_DESTDIR/${pkg##*/}"
				if [[ -e $pkg.sig ]]; then
					mv_acl "$pkg.sig" "$CLEANUP_DESTDIR/${pkg##*/}.sig"
				fi
				touch "${CLEANUP_DESTDIR}/${pkg##*/}"
			fi
		done
	fi
}

script_lock

for repo in "${PKGREPOS[@]}"; do
	for arch in "${ARCHES[@]}"; do
		repo_lock "${repo}" "${arch}" || exit 1
	done
done

[[ $CLEANUP_DRYRUN = true ]] && warning 'dry run mode is active'

for repo in "${PKGREPOS[@]}"; do
	for arch in "${ARCHES[@]}"; do
		if [[ ! -f ${FTP_BASE}/${repo}/os/${arch}/${repo}${DBEXT} ]]; then
			continue
		fi
		# get a list of actual available package files
		for f in "${FTP_BASE}/${repo}/os/${arch}"/*${PKGEXTS}; do
			if [[ -f $f ]]; then
				printf '%s\n' "${f##*/}"
			fi
		done | sort > "${WORKDIR}/repo-${repo}-${arch}"
		# get a list of package files defined in the repo db
		arch_expac "$repo" "$arch" '%f' | sort > "${WORKDIR}/db-${repo}-${arch}"

		missing_pkgs=($(comm -13 "${WORKDIR}/repo-${repo}-${arch}" "${WORKDIR}/db-${repo}-${arch}"))
		if (( ${#missing_pkgs[@]} >= 1 )); then
			error "Missing packages in [%s] (%s)..." "$repo" "$arch"
			for missing_pkg in "${missing_pkgs[@]}"; do
				msg2 '%s' "${missing_pkg}"
			done
		fi

		old_pkgs=($(comm -23 "${WORKDIR}/repo-${repo}-${arch}" "${WORKDIR}/db-${repo}-${arch}"))
		if (( ${#old_pkgs[@]} >= 1 )); then
			msg "Removing old packages from [%s] (%s)..." "$repo" "$arch"
			for old_pkg in "${old_pkgs[@]}"; do
				msg2 '%s' "${old_pkg}"
				clean_pkg "${FTP_BASE}/${repo}/os/${arch}/${old_pkg}"
			done
		fi
	done
done

# get a list of all available packages in the package pool
for f in "$FTP_BASE/${PKGPOOL}"/*${PKGEXTS}; do
	printf '%s\n' "${f##*/}"
done | sort > "${WORKDIR}/pool"
# create a list of packages in our db
cat "${WORKDIR}"/db-* 2>/dev/null | sort -u > "${WORKDIR}/db"

old_pkgs=($(comm -23 "${WORKDIR}/pool" "${WORKDIR}/db"))
if (( ${#old_pkgs[@]} >= 1 )); then
	msg "Removing old packages from package pool..."
	for old_pkg in "${old_pkgs[@]}"; do
		msg2 '%s' "${old_pkg}"
		clean_pkg "$FTP_BASE/${PKGPOOL}/${old_pkg}"
	done
fi

unset old_pkgs
touch -d "${CLEANUP_KEEP} days ago"  "${WORKDIR}/cleanup_timestamp"
for f in "${CLEANUP_DESTDIR}"/**/*${PKGEXTS}; do
	if [[ ${WORKDIR}/cleanup_timestamp -nt $f ]]; then
		old_pkgs+=("${f##*/}")
	fi
done
if (( ${#old_pkgs[@]} >= 1 )); then
	msg "Removing old packages from the cleanup directory..."
	for old_pkg in "${old_pkgs[@]}"; do
		msg2 '%s' "${old_pkg}"
		if [[ $CLEANUP_DRYRUN != true ]]; then
			rm -f "${CLEANUP_DESTDIR}/${old_pkg}"
			rm -f "${CLEANUP_DESTDIR}/${old_pkg}.sig"
		fi
	done
fi

for repo in "${PKGREPOS[@]}"; do
	for arch in "${ARCHES[@]}"; do
		repo_unlock "${repo}" "${arch}"
	done
done

script_unlock
