# SPDX-FileCopyrightText: 2023 Gustavo Coutinho de Souza <dev@onemoresuza.mailer.me>
#
# SPDX-License-Identifier: ISC

DOCDIR := "doc" 
PREFIX := "/usr/local"
ARCHIVE := ```
  printf 'aba-%s-%s-linux' \
    "$(cargo pkgid | cut -d'#' -f2 | cut -d'@' -f2)" \
    "$(uname -m)"
```

# List available recipes
default:
  @just --version
  @just --list --unsorted --justfile {{justfile()}}

# Generate man pages
doc:
  #!/bin/sh
  set -eu
  for f in "{{DOCDIR}}/"*.[[:digit:]].scd; do
    manpage="${f%.scd}"
    scdoc <"${f}" 1>"${manpage}"
    printf 'doc: Turn \033[1m%s\033[m into \033[1m%s\033[m\n' "${f}" "${manpage}"
  done

# Install documentation
install-doc: doc
  #!/bin/sh
  set -eu
  mandir="{{PREFIX}}/share/man"
  for f in "{{DOCDIR}}/"*.[[:digit:]]; do
    nmandir="${mandir}/man${f##*.}"
    mkdir -p "${nmandir}"
    cp "${f}" "${nmandir}"
    printf 'install-doc: Install \033[1m%s\033[m at \033[1m%s\033[m\n' \
      "${f}" "${nmandir}/${f##*/}"
  done

# Compile binary
build:
  cargo build --release

# Install compiled binary
install-bin: build
  cargo install --root "{{PREFIX}}" --path .

# Install the program
install: install-bin install-doc

# Distribute the binary and compiled manpages (to be used for staticly linked musl binaries)
dist-bin:
  mkdir -p "{{ARCHIVE}}"
  just --set PREFIX "{{ARCHIVE}}" install
  sha256sum "{{ARCHIVE}}/bin/aba" 1>"{{ARCHIVE}}.SHA256"
  rm "{{ARCHIVE}}/."[!.]*
  tar cvf "{{ARCHIVE}}.tar" "{{ARCHIVE}}"
  gzip "{{ARCHIVE}}.tar"

# Clean the dist-bin generated files
clean-dist:
  rm -rf {{ARCHIVE}}*
