#!/bin/sh

source /etc/makepkg.conf
if [ -e "${XDG_CONFIG_HOME}/pacman/makepkg.conf" ]; then
	source "${XDG_CONFIG_HOME}/pacman/makepkg.conf"
elif [ -e ~/.makepkg.conf ]; then
	source ~/.makepkg.conf
fi
source /etc/conf.d/eworm-repo
source /usr/lib/eworm/noroot
source /usr/lib/eworm/nice

# There may be some implications where gpg-agent starts X11 pinentry. So
# unset DISPLAY and have ncurses pinentry.
unset DISPLAY

function help() {
	echo "usage: ${0} [OPTIONS]"
	echo
	echo "where OPTIONS are:"
	echo " -h          show this help"
	echo " -p PACKAGE  remove package PACKAGE from repositories"
}

PACKAGE=""

while getopts "p:h" opt; do
	case ${opt} in
		h)
			help
			exit 0
			;;
		p)
			PACKAGE="${OPTARG}"
			;;
	esac
done

if [ -z "${PACKAGE}" ]; then
	echo "No package given." >&2
	help
	exit 1
fi

for ARCH in x86_64; do
	repo-remove --key "${GPGKEY}" --sign ${REPODIR}/${ARCH}/${REPONAME}.db.tar.gz ${PACKAGE}

	[ -s ${BASEDIR}/aur.${ARCH} ] && grep -E -q " ${PACKAGE}( |$)" ${BASEDIR}/aur.${ARCH} && sed -i "/ ${PACKAGE}( |$)/d" ${BASEDIR}/aur.${ARCH}
	[ -s ${BASEDIR}/import.${ARCH} ] && grep -E -q "^${PACKAGE}$" ${BASEDIR}/import.${ARCH} && sed -i "/^${PACKAGE}$/d" ${BASEDIR}/import.${ARCH}
	[ -s ${BASEDIR}/testing.${ARCH} ] && grep -E -q "^${PACKAGE}$" ${BASEDIR}/testing.${ARCH} && sed -i "/^${PACKAGE}$/d" ${BASEDIR}/testing.${ARCH}

	if [ -d ${LOCALDIR}/${PACKAGE}/ ]; then
		echo "Directory ${LOCALDIR}/${PACKAGE}/ still exists and I will not remove it for you."
	fi
done

date +"%s" > ${REPODIR}/lastupdate

repo-sync
