#!/bin/sh -ex
# Prepare deps/ directory with non-go-get-able dependencies and
# add them to initialised go.work

command -v fetch >/dev/null && DL="fetch -o" || DL="wget -O"
command -v sha512 >/dev/null && SHA512="sha512" || SHA512="sha512sum --binary"

[ -d cmd/pp ] || {
    echo must be in keks/go/ >&2
    exit 1
}
if [ -d deps ] ; then
    echo deps/ already exists >&2
    exit 1
fi
if [ -r go.work ] ; then
    echo go.work already exists >&2
    exit 1
fi

mkdir deps
cd deps

dl() {
    fn=$1
    url="$2"
    hsh=$3
    [ -s "$fn" ] || {
        $DL $fn.tmp "$url"
        mv $fn.tmp $fn
    }
    our=$($SHA512 <$fn | { read our rem ; echo $our ; })
    [ "$hsh" = "$our" ]
}

dl tai64n-v4.1.1.zip http://proxy.go.cypherpunks.su/go.cypherpunks.su/tai64n/v4/@v/v4.1.1.zip 3833d458d380c1058f37f99666fa5e3dec00392fda4975a18bd77d0784c911a23a7e8be2f9428b8c3a47a75392bf9ca0ca8685e33a288d1c4a321a0f6d804af0
dl balloon-v3.0.0.zip http://proxy.go.cypherpunks.su/go.cypherpunks.su/balloon/v3/@v/v3.0.0.zip 2273c463432fa85ca5bee321f7f0a189b51ec0bfe7eb7cfced28ce94420528915300ef87d76c4f91e0986e42862d44af511112763f2775d0b096fa82ebe41e0e
dl gogost-6.1.0.tar.zst http://www.gogost.cypherpunks.su/gogost-6.1.0.tar.zst 80eb41675833b7476b99423bba23f431e48644dfc05510aa6c70efb2bdd10ce8cce56666404daf8dd4b7d748cd2d016b323db9a492ec82c79aab83f4f52553f1
unzip tai64n-v4.1.1.zip
unzip balloon-v3.0.0.zip
rm -fr gogost-6.1.0
tar xf gogost-6.1.0.tar.zst
mkdir -p go.cypherpunks.su/gogost
mv gogost-6.1.0 go.cypherpunks.su/gogost/v6@v6.1.0

( cd .. ; go work init )
( cd .. ; go work use . )
( cd ../cmd/pp ; go work use . )
( cd ../cm ; go work use . )
( cd go.cypherpunks.su/tai64n/v4@v4.1.1 ; go work use . )
( cd go.cypherpunks.su/balloon/v3@v3.0.0 ; go work use . )
( cd go.cypherpunks.su/gogost/v6@v6.1.0 ; go work use . )
