#!/usr/bin/env sh
# This script attempts to install sq via apt, yum, apk, or brew.
# Parts of the script are inspired by the get-docker.sh
# script at https://get.docker.com


get_distribution() {
	lsb_dist=""
	# Every Linux system that we officially support has /etc/os-release
	if [ -r /etc/os-release ]; then
	  # shellcheck disable=SC1091
		lsb_dist="$(. /etc/os-release && echo "$ID")"
	fi
	# Returning an empty string here should be alright since the
	# case statements don't act unless you provide an actual value
	echo "$lsb_dist"
}

# Use command exists to test if a command is present on the system. E.g.
#
#   if command_exists lsb_release; then
command_exists() {
	command -v "$@" > /dev/null 2>&1
}

get_distribution

# Void Linux / xbps
if command_exists xbps-install; then
  set -e
  printf "Using xbps-install to install sq...\n\n"

  (xbps-install -Syu || true) && xbps-install -yu xbps
  xbps-install -yu sq
  exit
fi

# apt / deb
if [ -r /etc/debian_version ] && command_exists apt; then
  set -e
  printf "Using apt to install sq...\n\n"

  apt update -y && apt install -y --no-upgrade curl gpg

  curl -fsSL https://apt.fury.io/neilotoole/gpg.key | gpg --dearmor -o /usr/share/keyrings/sq.gpg

  echo "deb [signed-by=/usr/share/keyrings/sq.gpg] https://apt.fury.io/neilotoole/ * *" > /etc/apt/sources.list.d/sq.list

  cat <<EOF > /etc/apt/preferences.d/sq
Package: sq
Pin: origin apt.fury.io
Pin-Priority: 501
EOF

  apt update -y && apt install -y sq

  printf "\n"
  sq version
  printf "\n"
  exit
fi


# Yum / rpm
if command_exists yum; then
  set -e
  set +x
  printf "Using yum to install sq...\n\n"

  cat <<EOF > /etc/yum.repos.d/sq.repo
[sq]
name=sq
baseurl=https://yum.fury.io/neilotoole/
enabled=1
gpgcheck=0
gpgkey=https://apt.fury.io/neilotoole/gpg.key
EOF

  yum install -y sq

  printf "\n"
  sq version
  printf "\n"
  exit
fi


# apk / alpine
if command_exists apk; then
  set -e
  printf "Using apk to install sq...\n\n"
  apk update

  # sq isn't published to an Alpine repo yet, so we download the
  # file from GitHub, and execute "apk add" with the local apk file.

  # e.g. "v1.0.0"
  semver=$(wget -qO- "https://api.github.com/repos/neilotoole/sq/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')

  # e.g. "1.0.0"
  ver=$(echo "$semver" | sed -e "s/^v//")

  # Should be "x86_64" for amd64, and "aarch64" for arm64
  arch=$(uname -m)

  if [ "$arch" = "x86_64" ]; then
    arch="amd64"
  elif [ "$arch" = "aarch64" ]; then
    arch="arm64"
  else
    printf "sq install package not available for architecture: %s\n" "$arch"
    exit 1
  fi

  # e.g. "sq_0.18.1_linux_arm64.apk"
  file_name=$(printf "sq_%s_linux_%s.apk" "$ver" $arch)
  file_path="/tmp/$file_name"

  # https://github.com/neilotoole/sq/releases/download/v0.18.1/sq_0.18.1_linux_amd64.apk
  # https://github.com/neilotoole/sq/releases/download/v0.18.1/sq_0.18.1_linux_arm64.apk
  download_url=$(printf "https://github.com/neilotoole/sq/releases/download/%s/%s" "$semver" "$file_name")

  echo "Downloading apk from: $download_url"
  wget  "$download_url" -O "$file_path"

  apk add --allow-untrusted "$file_path"
  rm "$file_path"

  printf "\n"
  sq version
  printf "\n"
  exit
fi

# Arch Linux
if command_exists pacman; then
    if [ "$(id -u)" -eq 0 ]; then
        echo "AUR packages shouldn't be installed as root"
        exit 1
    fi

    # First check if there's an AUR helper available instead
    # of downloading and using pacman

    if command_exists yay; then
        echo "Installing via yay..."
        yay -S --noconfirm sq-bin
        exit
    fi

    if command_exists paru; then
        echo "Installing via paru..."
        paru -S --noconfirm sq-bin
        exit
    fi

    # Fall back to pacman
    echo "Installing via pacman..."
    cd /tmp
    curl -sO https://aur.archlinux.org/cgit/aur.git/snapshot/sq-bin.tar.gz
    tar -xf sq-bin.tar.gz
    cd sq-bin
    makepkg -sri --noconfirm
    rm -rf /tmp/sq-bin*
    exit
fi


# brew
if command_exists brew; then
  set -e
  printf "Using brew to install sq...\n\n"

  # sq is now a core brew formula ("brew install sq"). If it's already installed
  # via the old neilotoole/sq tap, uninstall that first.
  brew uninstall neilotoole/sq/sq 2> /dev/null || true

  brew install sq

  printf "\n"
  sq version
  printf "\n"
  exit
fi

# nix
if command_exists nix-shell; then
  set -e
  printf "Using nix to install sq...\n\n"

  nix-shell -p sq
  exit
fi

printf "\nCould not find a suitable install mechanism to install sq.\n"
printf "\nVisit https://github.com/neilotoole/sq for more installation options.\n"
exit 1




