Skip to content

Instantly share code, notes, and snippets.

@pjz
Last active August 31, 2021 20:02
Show Gist options
  • Save pjz/968d9b5e46d5ac5bd657e7c5a8bc3526 to your computer and use it in GitHub Desktop.
Save pjz/968d9b5e46d5ac5bd657e7c5a8bc3526 to your computer and use it in GitHub Desktop.
#!/bin/bash
VERINDEX_URL="https://ziglang.org/download/index.json"
VERINDEX=/tmp/zig-download-index.json
fetch_index() {
FLAGFILE=/tmp/index-refetch.$$
touch -d '1 day ago' $FLAGFILE
if [ -f "$VERINDEX" ]; then
if [ $VERINDEX -nt $FLAGFILE ]; then
rm -f $FLAGFILE
return
fi
fi
rm -f $VERINDEX
wget -q -O $VERINDEX $VERINDEX_URL
rm -f $FLAGFILE
}
FORCE=/bin/false
QUIET=/bin/false
if [ "$1" = "-q" ]; then
QUIET=/bin/true
shift
fi
if [ "$1" = "-f" ]; then
FORCE=/bin/true
shift
fi
VER=$1
shift
if [ "$VER" = "latest" ]; then
VER="master"
fi
#if [ "$VER" = "master" ]; then
# FORCE=/bin/true
#fi
if [ -z "$VER" ]; then
echo "This script installs zig into ~/.local/share/zig/<version>"
echo "(if its not there already)"
echo ""
echo "USAGE: $0 [-f] VERSION [ARCH]"
echo " -f, if supplied, forces a full reinstall"
echo " VERSION is a zig release version (eg. 0.6.0) or 'latest'"
echo " ARCH is a supported architecture. defaults to x86_64-linux"
echo ""
fetch_index
echo "Valid VERSIONs:"
#jq -r 'keys|reverse|join(", ")' $VERINDEX
jq -r 'keys|reverse|.[]' $VERINDEX | while read v; do echo " $v"; done
echo ""
echo "Valid ARCHs:"
jq -r 'map_values(keys)' $VERINDEX |grep [-] |sort -u | sed 's/[",]//g;'
echo ""
echo "NOTE: Not all VERSIONs support all ARCHs"
exit 1
fi
ARCH=${1:-x86_64-linux}
shift
DESTDIR=~/.local/share/zig/$VER
fetch_index
URL=`jq -r ".[\"$VER\"][\"$ARCH\"].tarball" $VERINDEX`
if $FORCE ; then
# force set, so nuke existing install
rm -rf "$DESTDIR"
fi
mkdir -p "$DESTDIR"
TGZ="${DESTDIR}/ziglang-${VER}.tar.xz"
if [ ! -f $TGZ ]; then
$QUIET || echo -n "fetching tarball.."
wget -q "$URL" -O $TGZ
else
$QUIET || echo -n "using existing tarball..."
fi
if [ ! -f "${DESTDIR}/zig" ]; then
$QUIET || echo -n "unpacking tarball..."
TMPDIR=/tmp/inst_zig.$$
mkdir $TMPDIR
tar -xJf $TGZ -C $TMPDIR
cd $TMPDIR
mv zig*/* "${DESTDIR}/"
rmdir zig-*
rm -rf $TMPDIR
else
$QUIET || echo -n "tarball already unpacked..."
fi
$QUIET || echo "zig now in $DESTDIR"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment