#!/bin/sh -e

[ -d cmd/cmkeytool ] || {
    echo must be in keks/go/cm/ >&2
    exit 1
}

GO_LDFLAGS="${GO_LDFLAGS:--s}"
root="$(dirname "$(realpath -- "$0")")"
cd "$root/.."
[ -d vendor ] && mod_vendor="-mod=vendor" || redo-ifchange \
    enc/encrypted.schema.keks \
    hash/prehash.schema.keks \
    sign/pub.schema.keks \
    sign/signed.schema.keks
mkdir -p bin
for cmd in enc hsh key sig ; do
    cmd=cm${cmd}tool
    go build $mod_vendor -o bin/$cmd -ldflags "$GO_LDFLAGS" ./cmd/$cmd
done
