#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'

PKGNAME=$(sed -n '/^name = / { s/name = "\(.*\)"/\1/ ;p;}' pyproject.toml)
PKGVERSION=$(sed -n '/^version = / { s/version = "\(.*\)"/\1/ ;p;}' pyproject.toml)
AUTHOR_EMAIL="$(git config --get user.email)"
AUTHOR_NAME="$(git config --get user.name)"
RELEASE=1
image_name=${PKGNAME}-aur-builder
finalaction="git push origin master"
gitdir=$(git rev-parse --show-toplevel)
cd "${gitdir}"

while getopts "d" o; do
  case "${o}" in
  d)
    finalaction="echo done"
    ;;
  *)
    echo "Invalid option"
    exit 1
    ;;
  esac
done
shift $((OPTIND - 1))

VERSION=${1:-${PKGVERSION}}

sudo docker build -f ./packaging/aur/Dockerfile -t ${image_name} .

sudo docker run --rm \
  -v ~/.config/copr:/home/builder/.config/copr \
  -v "${gitdir}":/src \
  -v $SSH_AUTH_SOCK:/ssh-agent --env SSH_AUTH_SOCK=/ssh-agent \
  --name ${PKGNAME}-builder \
  -it ${image_name} \
  /bin/bash -c "set -x;mkdir -p ~/.ssh/;chmod 0700 ~/.ssh && \
                         ssh-keyscan aur.archlinux.org >> ~/.ssh/known_hosts && \
                         git clone --depth=1 ssh://aur@aur.archlinux.org/${PKGNAME} /tmp/${PKGNAME} && \
                         cd /tmp/${PKGNAME} && \
                         cp /src/packaging/aur/PKGBUILD . && \
                         git config --global user.email '${AUTHOR_EMAIL}' && \
                         git config --global user.name '${AUTHOR_NAME}' && \
                         sed -E 's#(pkgver=).*#\1$VERSION#' -i PKGBUILD && \
                         sed -E 's#(pkgrel=).*#\1$RELEASE#' -i PKGBUILD && \
                         updpkgsums && \
                         makepkg --printsrcinfo > .SRCINFO && \
                         git clean -f && \
                         git commit -v -m 'Update to ${VERSION}' .SRCINFO PKGBUILD && \
    ${finalaction}"
