#!/bin/bash
# This file is part of hey.
#
# hey is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# hey is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with hey. If not, see .
#
# For further information about hey you can visit
# https://cdelord.fr/hey
PREFIX=~/.local
TARGET=
CACHE=~/.local/var/cache/hey
REPOS=$CACHE/repos
TAGS=$CACHE/tags
cmd-help()
{
cat </dev/null && sudo dnf install "$rpm_packet" && return
hash apt 2>/dev/null && sudo apt install "$apt_packet" && return
if $required
then
echo "hey requires $name"
exit 1
fi
}
cmd-install()
{
for package in "$@"
do
case "$package" in
(all) install-all ;;
(bang) install-bang ;;
(calculadoira) install-calculadoira ;;
(ditaa) install-ditaa ;;
(luax) install-luax ;;
(lsvg) install-lsvg ;;
(panda) install-panda ;;
(pandoc) install-pandoc ;;
(pandoc-latex-template) install-pandoc-latex-template ;;
(pandoc-panam-css) install-pandoc-panam-css ;;
(plantuml) install-plantuml ;;
(tagref) install-tagref ;;
(typst) install-typst ;;
(ypp) install-ypp ;;
(*) echo "$package: unknown installation method"; exit 1 ;;
esac
done
}
gitclone()
{
local url="$1"
local branch="$2"
local dir
dir=$(basename "$url")
mkdir -p $REPOS
if [ -d "$REPOS/$dir" ]
then
( cd "$REPOS/$dir" && git fetch origin && git reset --hard origin/"${branch:-master}" && git rebase )
else
git clone -b "${branch:-master}" "$url" "$REPOS/$dir"
fi
}
target()
{
case "$TARGET" in
("") [ -x $REPOS/luax/.build/bin/luax ] || compile-luax 1>&2
$REPOS/luax/.build/bin/luax -e 'print(sys.os.."-"..sys.arch)'
;;
(linux-x86_64*) echo "linux-x86_64" ;;
(linux-aarch64*) echo "linux-aarch64" ;;
(windows-x86_64*) echo "windows-x86_64" ;;
(macos-x86_64*) echo "macos-x86_64" ;;
(macos-aarch64*) echo "macos-aarch64" ;;
(*) echo "$TARGET" # the caller shall report an error
;;
esac
}
tag_name()
{
local repo="$1"
local request="https://api.github.com/repos/$repo/releases/latest"
local tag_file
tag_file="$TAGS/$(basename "$repo")"
mkdir -p "$TAGS"
local age=$(( $(date +%s) - $(stat -c %Y -- "$tag_file" 2>/dev/null || echo 0) ))
if [ "$age" -gt 7200 ]
then
curl -sSL "$request" | jq -r .tag_name > "$tag_file"
touch "$tag_file"
fi
cat "$tag_file"
}
title()
{
figlet -t "$*"
}
declare -A ALREADY_INSTALLED=()
once()
{
if [ -z "${ALREADY_INSTALLED["$1"]}" ]
then
ALREADY_INSTALLED["$1"]=true
true
else
false
fi
}
install-all()
{
# shellcheck disable=SC2046
cmd-install $(
grep "^install-.*()$" "$0" |
grep -v "^install-all()$" |
sed 's/^install-//' |
tr -d '()'
)
}
install-bang()
{
once bang || return
compile-luax
title Bang
gitclone https://github.com/CDSoft/bang
ninja clean -C $REPOS/bang
PREFIX=$PREFIX ninja install -C $REPOS/bang
}
install-calculadoira()
{
once calculadoira || return
compile-luax
title Calculadoira
gitclone https://github.com/CDSoft/calculadoira
ninja clean -C $REPOS/calculadoira
PREFIX=$PREFIX ninja install -C $REPOS/calculadoira
}
install-ditaa()
{
once ditaa || return
title ditaa
local DITAA_VERSION
DITAA_VERSION=$(tag_name stathissideris/ditaa)
mkdir -p $REPOS/ditaa $PREFIX/bin
[ -f "$REPOS/ditaa/ditaa-${DITAA_VERSION#v}-standalone.jar" ] \
|| wget "https://github.com/stathissideris/ditaa/releases/download/${DITAA_VERSION}/ditaa-${DITAA_VERSION#v}-standalone.jar" \
-O "$REPOS/ditaa/ditaa-${DITAA_VERSION#v}-standalone.jar"
cp -v -f "$REPOS/ditaa/ditaa-${DITAA_VERSION#v}-standalone.jar" $PREFIX/bin/ditaa.jar
}
install-lsvg()
{
once lsvg || return
compile-luax
title lsvg
gitclone https://github.com/CDSoft/lsvg
ninja clean -C $REPOS/lsvg
PREFIX=$PREFIX ninja install -C $REPOS/lsvg
}
compile-luax()
{
once compile-luax || return
title "LuaX compilation"
gitclone https://github.com/CDSoft/luax
( cd $REPOS/luax && ./bootstrap.sh )
ninja compile -C $REPOS/luax
eval "$($REPOS/luax/.build/bin/luax env)"
}
install-luax()
{
compile-luax
once luax || return
title LuaX
mkdir -p "$PREFIX"/{bin,lib}
if [ -n "$TARGET" ]
then
cp -f "$REPOS/luax/.build/dist/$TARGET/bin"/* "$PREFIX/bin/"
cp -f "$REPOS/luax/.build/dist/$TARGET/lib"/* "$PREFIX/lib/"
else
cp -f "$REPOS/luax/.build/bin"/* "$PREFIX/bin/"
cp -f "$REPOS/luax/.build/lib"/* "$PREFIX/lib/"
fi
}
install-panda()
{
once panda || return
compile-luax
install-pandoc-latex-template
install-pandoc-panam-css
title Panda
gitclone https://github.com/CDSoft/panda
ninja clean -C $REPOS/panda
PREFIX=$PREFIX ninja install -C $REPOS/panda
}
install-pandoc()
{
once pandoc || return
title Pandoc
mkdir -p "$REPOS/pandoc/$TARGET" "$PREFIX/bin"
local PANDOC_VERSION
PANDOC_VERSION=$(tag_name jgm/pandoc)
case "$(target)" in
(linux-x86_64)
local PANDOC_URL=https://github.com/jgm/pandoc/releases/download/${PANDOC_VERSION}/pandoc-${PANDOC_VERSION}-linux-amd64.tar.gz
[ -f "$REPOS/pandoc/$TARGET/$(basename "$PANDOC_URL")" ] || wget -c "$PANDOC_URL" -O "$REPOS/pandoc/$TARGET/$(basename "$PANDOC_URL")"
tar -C "$REPOS/pandoc/$TARGET/" -xzf "$REPOS/pandoc/$TARGET/$(basename "$PANDOC_URL")"
cp -f -av "$REPOS/pandoc/$TARGET/pandoc-${PANDOC_VERSION}/bin"/* "$PREFIX/bin/"
;;
(linux-aarch64)
local PANDOC_URL=https://github.com/jgm/pandoc/releases/download/${PANDOC_VERSION}/pandoc-${PANDOC_VERSION}-linux-arm64.tar.gz
[ -f "$REPOS/pandoc/$TARGET/$(basename "$PANDOC_URL")" ] || wget -c "$PANDOC_URL" -O "$REPOS/pandoc/$TARGET/$(basename "$PANDOC_URL")"
tar -C "$REPOS/pandoc/$TARGET/" -xzf "$REPOS/pandoc/$TARGET/$(basename "$PANDOC_URL")"
cp -f -av "$REPOS/pandoc/$TARGET/pandoc-${PANDOC_VERSION}/bin"/* "$PREFIX/bin/"
;;
(windows-x86_64)
local PANDOC_URL=https://github.com/jgm/pandoc/releases/download/${PANDOC_VERSION}/pandoc-${PANDOC_VERSION}-windows-x86_64.zip
[ -f "$REPOS/pandoc/$TARGET/$(basename "$PANDOC_URL")" ] || wget -c "$PANDOC_URL" -O "$REPOS/pandoc/$TARGET/$(basename "$PANDOC_URL")"
unzip -o -q "$REPOS/pandoc/$TARGET/$(basename "$PANDOC_URL")" -d "$REPOS/pandoc/$TARGET/"
cp -f -av "$REPOS/pandoc/$TARGET/pandoc-${PANDOC_VERSION}"/*.exe "$PREFIX/bin/"
;;
(macos-x86_64)
local PANDOC_URL=https://github.com/jgm/pandoc/releases/download/${PANDOC_VERSION}/pandoc-${PANDOC_VERSION}-x86_64-macOS.zip
[ -f "$REPOS/pandoc/$TARGET/$(basename "$PANDOC_URL")" ] || wget -c "$PANDOC_URL" -O "$REPOS/pandoc/$TARGET/$(basename "$PANDOC_URL")"
unzip -o -q "$REPOS/pandoc/$TARGET/$(basename "$PANDOC_URL")" -d "$REPOS/pandoc/$TARGET/"
cp -f -av "$REPOS/pandoc/$TARGET/pandoc-${PANDOC_VERSION}-x86_64/bin"/* "$PREFIX/bin/"
;;
(macos-aarch64)
local PANDOC_URL=https://github.com/jgm/pandoc/releases/download/${PANDOC_VERSION}/pandoc-${PANDOC_VERSION}-arm64-macOS.zip
[ -f "$REPOS/pandoc/$TARGET/$(basename "$PANDOC_URL")" ] || wget -c "$PANDOC_URL" -O "$REPOS/pandoc/$TARGET/$(basename "$PANDOC_URL")"
unzip -o -q "$REPOS/pandoc/$TARGET/$(basename "$PANDOC_URL")" -d "$REPOS/pandoc/$TARGET/"
cp -f -av "$REPOS/pandoc/$TARGET/pandoc-${PANDOC_VERSION}-arm64/bin"/* "$PREFIX/bin/"
;;
(*) echo "$(target): unsupported platform for Pandoc"; exit 1 ;;
esac
}
install-pandoc-latex-template()
{
once pandoc-latex-template || return
title Pandoc LaTeX template
#gitclone https://github.com/Wandmalfarbe/pandoc-latex-template
gitclone https://github.com/jvsteiner/pandoc-latex-template update-pandoc # temporary, until the patch is merged
mkdir -p $PREFIX/share/pandoc
cp -f -v $REPOS/pandoc-latex-template/eisvogel.tex $PREFIX/share/pandoc
}
install-pandoc-panam-css()
{
once pandoc-panam-css || return
title Pandoc Pan-Am CSS
gitclone https://github.com/CDSoft/pan-am
mkdir -p $PREFIX/share/pandoc
cp -f -v $REPOS/pan-am/styling.css $PREFIX/share/pandoc/panam.css
}
install-plantuml()
{
once plantuml || return
title PlantUML
local PLANTUML_VERSION
PLANTUML_VERSION=$(tag_name plantuml/plantuml)
mkdir -p $REPOS/plantuml $PREFIX/bin
[ -f "$REPOS/plantuml/plantuml-pdf-${PLANTUML_VERSION#v}.jar" ] \
|| wget "https://github.com/plantuml/plantuml/releases/download/${PLANTUML_VERSION}/plantuml-pdf-${PLANTUML_VERSION#v}.jar" \
-O "$REPOS/plantuml/plantuml-pdf-${PLANTUML_VERSION#v}.jar"
cp -f -v "$REPOS/plantuml/plantuml-pdf-${PLANTUML_VERSION#v}.jar" $PREFIX/bin/plantuml.jar
}
install-tagref()
{
once tagref || return
compile-luax
title tagref
gitclone https://github.com/CDSoft/tagref
ninja clean -C $REPOS/tagref
PREFIX=$PREFIX ninja install -C $REPOS/tagref
}
install-typst()
{
once typst || return
title Typst
local TYPST_VERSION
TYPST_VERSION=$(tag_name typst/typst)
mkdir -p "$REPOS/typst/$TARGET/$TYPST_VERSION" "$PREFIX/bin"
case "$(target)" in
(linux-x86_64)
TYPST_URL=https://github.com/typst/typst/releases/download/${TYPST_VERSION}/typst-x86_64-unknown-linux-musl.tar.xz
[ -f "$REPOS/typst/$TARGET/$TYPST_VERSION/$(basename "$TYPST_URL")" ] || wget -c "$TYPST_URL" -O "$REPOS/typst/$TARGET/$TYPST_VERSION/$(basename "$TYPST_URL")"
tar -C "$REPOS/typst/$TARGET/$TYPST_VERSION" -xJf "$REPOS/typst/$TARGET/$TYPST_VERSION/$(basename "$TYPST_URL")"
cp -f -av "$REPOS/typst/$TARGET/$TYPST_VERSION/typst-x86_64-unknown-linux-musl/typst" "$PREFIX/bin/"
;;
(linux-aarch64)
TYPST_URL=https://github.com/typst/typst/releases/download/${TYPST_VERSION}/typst-aarch64-unknown-linux-musl.tar.xz
[ -f "$REPOS/typst/$TARGET/$TYPST_VERSION/$(basename "$TYPST_URL")" ] || wget -c "$TYPST_URL" -O "$REPOS/typst/$TARGET/$TYPST_VERSION/$(basename "$TYPST_URL")"
tar -C "$REPOS/typst/$TARGET/$TYPST_VERSION" -xJf "$REPOS/typst/$TARGET/$TYPST_VERSION/$(basename "$TYPST_URL")"
cp -f -av "$REPOS/typst/$TARGET/$TYPST_VERSION/typst-aarch64-unknown-linux-musl/typst" "$PREFIX/bin/"
;;
(windows-x86_64)
TYPST_URL=https://github.com/typst/typst/releases/download/${TYPST_VERSION}/typst-x86_64-pc-windows-msvc.zip
[ -f "$REPOS/typst/$TARGET/$TYPST_VERSION/$(basename "$TYPST_URL")" ] || wget -c "$TYPST_URL" -O "$REPOS/typst/$TARGET/$TYPST_VERSION/$(basename "$TYPST_URL")"
unzip -o -q "$REPOS/typst/$TARGET/$TYPST_VERSION/$(basename "$TYPST_URL")" -d "$REPOS/typst/$TARGET/$TYPST_VERSION/"
cp -f -av "$REPOS/typst/$TARGET/$TYPST_VERSION/typst-x86_64-pc-windows-msvc/typst.exe" "$PREFIX/bin/"
;;
(macos-x86_64)
TYPST_URL=https://github.com/typst/typst/releases/download/${TYPST_VERSION}/typst-x86_64-apple-darwin.tar.xz
[ -f "$REPOS/typst/$TARGET/$TYPST_VERSION/$(basename "$TYPST_URL")" ] || wget -c "$TYPST_URL" -O "$REPOS/typst/$TARGET/$TYPST_VERSION/$(basename "$TYPST_URL")"
tar -C "$REPOS/typst/$TARGET/$TYPST_VERSION" -xJf "$REPOS/typst/$TARGET/$TYPST_VERSION/$(basename "$TYPST_URL")"
cp -f -av "$REPOS/typst/$TARGET/$TYPST_VERSION/typst-x86_64-apple-darwin/typst" "$PREFIX/bin/"
;;
(macos-aarch64)
TYPST_URL=https://github.com/typst/typst/releases/download/${TYPST_VERSION}/typst-aarch64-apple-darwin.tar.xz
[ -f "$REPOS/typst/$TARGET/$TYPST_VERSION/$(basename "$TYPST_URL")" ] || wget -c "$TYPST_URL" -O "$REPOS/typst/$TARGET/$TYPST_VERSION/$(basename "$TYPST_URL")"
tar -C "$REPOS/typst/$TARGET/$TYPST_VERSION" -xJf "$REPOS/typst/$TARGET/$TYPST_VERSION/$(basename "$TYPST_URL")"
cp -f -av "$REPOS/typst/$TARGET/$TYPST_VERSION/typst-aarch64-apple-darwin/typst" "$PREFIX/bin/"
;;
(*) echo "$(target): unsupported platform for Typst"; exit 1 ;;
esac
}
install-ypp()
{
once ypp || return
compile-luax
title ypp
gitclone https://github.com/CDSoft/ypp
ninja clean -C $REPOS/ypp
PREFIX=$PREFIX ninja install -C $REPOS/ypp
}
main()
{
local args=()
while [ -n "$1" ]
do
case "$1" in
(-p) PREFIX="$2"; shift 2;;
(-t) TARGET="$2"; shift 2;;
(-*) echo "$1: unknown option"; exit 1 ;;
(*) args+=("$1"); shift 1 ;;
esac
done
PREFIX=$(realpath "$PREFIX")
local cmd="${args[0]}"
unset "args[0]"
#set -x
case "$cmd" in
(""|help) cmd-help ;;
(list) cmd-list ;;
(clean) cmd-clean "${args[@]}" ;;
(install) echo "Installation prefix: $PREFIX"
check-prerequisites
cmd-install "${args[@]}"
;;
(*) echo "$cmd: unknown command"; exit 1 ;;
esac
set +x
}
main "$@"