You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
2.0 KiB
Bash

#!/usr/bin/env bash
APP=bat
VERSION=${VERSION:-v0.17.1}
DATA_DIR="${XDG_DATA_HOME}/apps/releases"
DEST_DIR="${DATA_DIR}/${APP}/${VERSION}"
OS=
ARCH=
case $(uname -s) in
Linux*) OS=unknown-linux-gnu;;
Darwin*) OS=apple-darwin;;
CYGWIN_NT*|Windows_NT*) OS=pc-windows-msvc;;
*) >&2 echo "unsupported os"; return;
esac
case $(uname -m) in
amd64*|x86_64*) ARCH=x86_64;;
i686*|i386*) ARCH=i686;;
arm|armv7*|armv6*) ARCH=arm;;
aarch64*|armv8*) ARCH=arm64;;
*) >&2 echo "unsupported architecture: $(uname -m)"; exit 1;;
esac
function bail () {
echo "unexpected error $1"
exit 1
}
function do_link () {
local dir="$1"
local link="$2"
if [[ ! -d "${dir}" ]] ; then
return
fi
if [[ -L "${dir}/$(basename "${link}")" ]] ; then
return
fi
cd "${dir}" || bail "failed to change directory: ${dir}"
ln --symbolic "${link}" "$(basename "${link}")"
}
URL="https://github.com/sharkdp/bat/releases/download/${VERSION}/bat-${VERSION}-${ARCH}-${OS}.tar.gz"
TMP_DIR=
if [[ ! -d "${DEST_DIR}" ]] ; then
set -x
TMP_DIR=$(mktemp --directory)
TAR_GZ="${TMP_DIR}/release.tar.gz"
echo "start download: ${URL}"
curl --location --silent --output "${TAR_GZ}" "${URL}"
echo "stop download: ${URL}"
mkdir -p "${DEST_DIR}"
tar xzf "${TAR_GZ}" --strip-components 1 --directory "${DEST_DIR}"
rm -rf "${TMP_DIR}"
fi
unset TMP_DIR
cd "${DATA_DIR}/${APP}" || bail "directory change failed: ${DATA_DIR}/${APP}"
[[ -s current ]] && unlink current
ln -s "${VERSION}" current
ZSH_CONFIG_D="${XDG_CONFIG_HOME}/zsh/zshrc.d"
FISH_CONFIG_D="${XDG_CONFIG_HOME}/fish/functions"
MAN_DIR="${XDG_DATA_HOME}/man/man1"
[[ -d ${MAN_DIR} ]] || mkdir -p "${MAN_DIR}"
do_link "${XDG_BIN_HOME}" "${DATA_DIR}/${APP}/current/bat"
do_link "${ZSH_CONFIG_D}" "${DATA_DIR}/${APP}/current/autocomplete/bat.zsh"
do_link "${FISH_CONFIG_D}" "${DATA_DIR}/${APP}/current/autocomplete/bat.fish"
do_link "${MAN_DIR}" "${DATA_DIR}/${APP}/current/bat.1"