#!/usr/bin/env bash

set -exuo pipefail

# Run examples.
go install ./cmd/addchain

for example in ./internal/examples/*; do
    cd "${example}"
    for script in *.sh; do
        root="${script%.sh}"
        bash "${script}" > "${root}.out" 2>&1
    done
    cd -
done

# go generate
go generate -x ./...

# Bibliography.
bib generate -bib doc/references.bib -type markdown -output doc/bibliography.md

# Documentation.
go run ./internal/tools/docgen -type readme -tocmax 3 -output README.md
go run ./internal/tools/docgen -type results -output doc/results.md
go run ./internal/tools/docgen -type gen -tocmax 3 -output doc/gen.md
go run ./internal/tools/docgen -type cff -output CITATION.cff
go run ./internal/tools/docgen -type bibtex -output CITATION.bib
go run ./internal/tools/docgen -type zenodo -output .zenodo.json

# Install script. (Post-processed to remove timestamp for repeatability.)
godownloader --repo=mmcloughlin/addchain .goreleaser.yml \
    | sed 's/\(Code generated by godownloader\).*\(\. DO NOT EDIT\.\)/\1\2/' > install.sh
chmod +x install.sh
