#!/bin/bash -e # Haybarn Linux/OSX installer script # Haybarn is an independent derived distribution of DuckDB by Query Farm # Releases: https://github.com/Query-farm-haybarn/haybarn/releases GITHUB_REPO="Query-farm-haybarn/haybarn" GITHUB_API="https://api.github.com/repos/${GITHUB_REPO}/releases/latest" GITHUB_DOWNLOAD="https://github.com/${GITHUB_REPO}/releases/latest/download" main () { OS=$(uname -s) ARCH=$(uname -m) command -v curl >/dev/null 2>&1 || { echo >&2 "Required tool curl could not be found. Aborting."; exit 1; } command -v zcat >/dev/null 2>&1 || { echo >&2 "Required tool zcat could not be found. Hint: install the gzip package. Aborting."; exit 1; } # Fetch latest release tag from GitHub API LATEST_TAG=$(curl -sf "${GITHUB_API}" | grep '"tag_name"' | sed 's/.*"tag_name": *"\([^"]*\)".*/\1/') if [ -z "${LATEST_TAG}" ]; then echo "Failed to fetch latest release tag from GitHub." 1>&2 exit 1 fi # Allow override via environment variable if [ -z "${HAYBARN_VERSION}" ]; then TAG="${LATEST_TAG}" else TAG="${HAYBARN_VERSION}" fi eval PREFIX="~/.haybarn/cli" INST="${PREFIX}/${TAG}" LATEST="${PREFIX}/latest" DIST= if [ "${OS}" = "Linux" ] then if [ "${ARCH}" = "x86_64" ] || [ "${ARCH}" = "amd64" ] then DIST=linux-amd64 elif [ "${ARCH}" = "aarch64" ] || [ "${ARCH}" = "arm64" ] then DIST=linux-arm64 fi elif [ "${OS}" = "Darwin" ] then if [ "${ARCH}" = "x86_64" ] then DIST=osx-amd64 elif [ "${ARCH}" = "arm64" ] then DIST=osx-arm64 fi fi if [ -z "${DIST}" ] then echo "Operating system '${OS}' / architecture '${ARCH}' is unsupported." 1>&2 exit 1 fi URL="https://github.com/${GITHUB_REPO}/releases/download/${TAG}/haybarn_cli-${DIST}.gz" echo echo "*** Haybarn Linux/MacOS installation script, tag ${TAG} ***" echo " (Haybarn — an independent derived distribution of DuckDB by Query Farm)" echo " (Not affiliated with or endorsed by the DuckDB Foundation)" echo test_installed_haybarn() { "${INST}/haybarn" -noheader -init /dev/null -csv -batch -s "SELECT 2*3*7" 2>/dev/null } if [ -f "${INST}/haybarn" ] && [ "$(test_installed_haybarn)" = "42" ]; then echo "Destination binary ${INST}/haybarn already exists and seems to work" else mkdir -p "${INST}" if [ ! -d "${INST}" ]; then echo "Failed to create install directory ${INST}." 1>&2 exit 1 fi echo "Downloading ${URL} ..." curl --fail --location --progress-bar "${URL}" > "${INST}/haybarn.gz" || exit 1 cat "${INST}/haybarn.gz" | zcat > "${INST}/haybarn" rm -f "${INST}/haybarn.gz" chmod a+x "${INST}/haybarn" if [ ! -f "${INST}/haybarn" ]; then echo "Failed to download/unpack binary at ${INST}/haybarn" 1>&2 exit 1 fi # Test the binary works if [ ! "$(test_installed_haybarn)" = "42" ]; then echo "Failed to execute installed binary :/ ${INST}." 1>&2 exit 1 fi echo echo "Successfully installed Haybarn ${TAG} to ${INST}/haybarn" fi if [ "${TAG}" = "${LATEST_TAG}" ]; then # Update symlink to latest rm -f "${LATEST}" || exit 1 ln -s "${INST}" "${LATEST}" || exit 1 echo "Updated symlink from ${LATEST}/haybarn to" echo " ${INST}/haybarn" echo echo "Hint: Append the following line to your shell profile:" echo 'export PATH="'"${LATEST}"'":$PATH' else echo echo "Hint: Append the following line to your shell profile:" echo 'export PATH="'"${INST}"'":$PATH' fi # Symlink into ~/.local/bin if it exists, is writable, and doesn't already have haybarn eval LOCALBIN="${HOME}/.local/bin" if [ "${TAG}" = "${LATEST_TAG}" ] && [ -d "${LOCALBIN}" ] && [ -w "${LOCALBIN}" ] && [ ! -f "${LOCALBIN}/haybarn" ]; then ln -s "${LATEST}/haybarn" "${LOCALBIN}/haybarn" || exit 1 echo "Also created a symlink from ${LOCALBIN}/haybarn" echo " to ${LATEST}/haybarn" fi echo echo "To launch Haybarn ${TAG} now, type:" if [ "${TAG}" = "${LATEST_TAG}" ]; then echo "${LATEST}/haybarn" else echo "${INST}/haybarn" fi } main