#!/bin/sh

# SPDX-FileCopyrightText: 2023 Benjamin Grande M. S. <ben.grande.b@gmail.com>
#
# SPDX-License-Identifier: AGPL-3.0-or-later

## Should be called by git-remote-qrexec.
set -eu

usage(){
  echo "Usage: ${helper} git-upload-pack|git-receive-pack <qube>/<path>"
  echo "Note: ${helper} is supposed to be called by ${parent_helper}"
  exit 1
}

die(){
  echo "Error: ${1}" >&2
  exit 1
}

helper="${0##*/git-}"
parent_helper="${helper%-*}"

case "${1-}" in
  -h|--?help|"") usage;;
  *) arg="${1}";;
esac
case "${2-}" in
  "") usage;;
  *) url="${2}";;
esac

case "${arg}" in
  git-upload-pack) rpc=GitFetch;;
  git-receive-pack) rpc=GitPush;;
  "") die "Argument can't be empty";;
  *) die "Unsupported argument: '${arg}'";;
esac

qube="$(echo "${url}" | cut -d "/" -f1)"
repo="$(echo "${url}" | cut -d "/" -f2-)"
test -n "${repo}" || die "Repository name can't be empty"
vendor="qusal"
default_qube="sys-git"
rpc_cmd="${vendor}.${rpc}+${repo}"

if command -v qrexec-client-vm >/dev/null; then
  exec qrexec-client-vm -- "${qube}" "${rpc_cmd}"
elif command -v qrexec-client >/dev/null; then
  qubes_version="$(awk -F '=' '/^VERSION_ID=/{print $2}' /etc/os-release)"
  if test "$(echo "${qubes_version}" | tr -d ".")" -le 41; then
    if test "${qube}" = "@default"; then
      qube="${default_qube}"
    fi
  else
    policy="$(qrexec-policy --assume-yes-for-ask dom0 "${qube}" "${rpc_cmd}")"
    qube="$(echo "${policy}" | awk -F '=' '/^target=/{print $2}')"
  fi
  exec qrexec-client -d "${qube}" -- "DEFAULT:QUBESRPC ${rpc_cmd} dom0"
else
  die "Qrexec programs not found: qrexec-client-vm, qrexec-client"
fi
