#!/usr/bin/env bash

# This script trys to download the correct version of binary from github.
# You can download it manually and put it(i.e. `sk`) under `bin/`.
#
# If you know rust or have rust installed, you can build it with
# `cargo build --release`

set -u

cd "$(dirname "${BASH_SOURCE[0]}")"
skim_base="$(pwd)"

version=$(curl -s "https://api.github.com/repos/skim-rs/skim/releases/latest" | grep tag_name | grep -o "[.0-9]*")

check_binary() {
  echo -n "  - Checking skim executable ... "
  local output
  output=$("$skim_base"/bin/sk --version | grep -o "[.0-9]*" 2>&1)
  if [ $? -ne 0 ]; then
    echo "Error: $output"
  elif [ "$version" != "$output" ]; then
    echo "$output != $version"
  else
    echo "Existing version is already the latest: $output"
    exit 0
  fi
  rm -f "$skim_base"/bin/sk
  return 1
}


# download the latest skim
download() {
  echo "Downloading bin/sk ..."
  mkdir -p "$skim_base"/bin && cd "$skim_base"/bin
  if [ $? -ne 0 ]; then
    binary_error="Failed to create bin directory"
    return
  fi

  check_binary

  local url=https://github.com/skim-rs/skim/releases/download/v$version/${1}.tgz
  echo "Downloading: $url"
  if command -v curl > /dev/null; then
    curl -fL $url | tar xz
  elif command -v wget > /dev/null; then
    wget -O - $url | tar xz
  else
    binary_error="curl or wget not found"
    return
  fi

  if [ ! -f $1 ]; then
    binary_error="Failed to download ${1}"
    return
  fi
}

archi=$(uname -sm)
case "$archi" in
  Darwin\ x86_64) download skim-${binary_arch:-x86_64}-apple-darwin;;
  Darwin\ arm64) download skim-${binary_arch:-aarch64}-apple-darwin;;
  Linux\ x86_64)  download skim-${binary_arch:-x86_64}-unknown-linux-musl;;
  Linux\ armv7l) download skim-${binary_arch:-armv7}-unknown-linux-musleabi;;
  Linux\ aarch64) download skim-${binary_arch:-aarch64}-unknown-linux-musl;;
  *) echo "No binaries available for '$archi' yet. Try: 'cargo install skim'";;
esac

echo "Done :)"
