VERSION='7.3.13'
PYVER='3.9'

# https://www.pypy.org/checksums.html
aarch64_hash=317d7876c5825a086f854253648b967a432b993ce87695d2895d3ad6ed0d2716
linux32_hash=ac695238b4a3635ac6b482e74e04e2ea78b31acca0decd5de601dfd2f4ebf35a
linux64_hash=323b05a9f607e932cda1995cbe77a96e4ea35994631aa6d734c8035e8479b74e
osarm64_hash=a07b17a790a1952b551e69d47d77a5546ad5e666ed1bd90b9ad60baaca6aa51e
osx64_hash=180802aa0122d4a05ec480bf3130c78591ba88fdde25d8e65a92d4a798b318a3
s390x_hash=213c88f652a99c4dc4e8e00b4b5b58f381c7f7e9ea1a9b65801fc0eb1e50df0a

### end of manual settings - following lines same for every download

function err_no_binary {
    local archmsg="${1}"
    local ver="pypy${PYVER}-v${VERSION}-src"
    local url="https://downloads.python.org/pypy/${ver}.tar.bz2"
    { echo
      colorize 1 "ERROR"
      echo ": The binary distribution of PyPy is not available for ${archmsg}."
      echo "try '${url}' to build from source."
      echo
    } >&2
    exit 1
}

function pypy_pkg_data {
    # pypy architecture tag
    local ARCH="${1}"

    # defaults
    local cmd='install_package'  # use bz2
    local pkg="${ARCH}" # assume matches
    local ext='tar.bz2'
    local hash='' # undefined

    # select the hash, fix pkg if not match ARCH
    case "${ARCH}" in
    'linux-aarch64' )
      hash="${aarch64_hash}"
      pkg='aarch64'
      ;;
    'linux' )
      hash="${linux32_hash}"
      pkg='linux32'
      ;;
    'linux64' )
      hash="${linux64_hash}"
      ;;
    'osarm64' )
      hash="${osarm64_hash}"
      pkg='macos_arm64'
      ;;
    'osx64' )
      if require_osx_version "10.13"; then
        hash="${osx64_hash}"
        pkg='macos_x86_64'
      else
        err_no_binary "${ARCH}, OS X < 10.13"
      fi
      ;;
    's390x' )
      hash="${s390x_hash}"
      ;;
    * )
      err_no_binary "${ARCH}"
      ;;
    esac

    local basever="pypy${PYVER}-v${VERSION}"
    local baseurl="https://downloads.python.org/pypy/${basever}"

    # result - command, package dir, url+hash
    echo "${cmd}" "${basever}-${pkg}" "${baseurl}-${pkg}.${ext}#${hash}"
}

# determine command, package directory, url+hash
declare -a pd="$(pypy_pkg_data "$(pypy_architecture 2>/dev/null || true)")"

# install
${pd[0]} "${pd[1]}" "${pd[2]}" 'pypy' "verify_py${PYVER//./}" 'ensurepip'
