#!/bin/bash
set -e

repo="dlvhdr/gh-prs"

extensionPath="$(dirname "$0")"
arch="$(uname -m)"

exe=""

if uname -a | grep Msys > /dev/null; then
  if [ $arch = "x86_64" ]; then
    exe="windows-x86_64"
  elif [ $arch = "i686" ]; then
    exe="windows-i386"
  elif [ $arch = "i386" ]; then
    exe="windows-i386"
  fi
elif uname -a | grep Darwin > /dev/null; then
  if [ $arch = "x86_64" ]; then
    exe="darwin-x86_64"
  elif [ $arch = "arm64" ]; then
    exe="darwin-arm64"
  fi
elif uname -a | grep Linux > /dev/null; then
  if [ $arch = "x86_64" ]; then
    exe="linux-x86_64"
  elif [ $arch = "i686" ]; then
    exe="linux-i386"
  elif [ $arch = "i386" ]; then
    exe="linux-i386"
  fi
fi

if [ "${exe}" == "" ]; then
  if [ "$(which go)" = "" ]; then
    echo "go must be installed to use this gh extension on this platform"
    exit 1
  fi

  exe="cmd.out"

  cd "${extensionPath}" > /dev/null
  go build -o "${exe}"
  cd - > /dev/null
else
  if [[ ! -x "${extensionPath}/bin/${exe}" ]]; then
    mkdir -p bin
    rm -f "${extensionPath}/bin/*"
    gh release -R"${repo}" download -p "${exe}" --dir="${extensionPath}/bin"
    chmod +x "${extensionPath}/bin/${exe}"
  fi
fi

exec "${extensionPath}/bin/${exe}" "$@"
