watch +args='lcheck':
  cargo watch --clear --exec '{{ args }}'

clippy: (watch 'lclippy --tests --all --all-targets -- --deny warnings')

check: (watch 'lcheck --tests --all --all-targets')

test *args: (watch f'ltest --all --all-targets {{args}}')

lib *args: (watch f'ltest --lib {{args}}')

ci: lint
  cargo ltest --workspace

build:
  cargo lbuild

serve: build
  ./target/debug/filepack serve --address localhost --http-port 8000

open:
  open http://localhost:8000

install:
  cargo install --path .

deploy branch='master' remote='casey/filepack' domain='filepack.com':
  ssh root@{{domain}} '\
    export DEBIAN_FRONTEND=noninteractive \
    && mkdir -p deploy \
    && apt-get update --yes \
    && apt-get upgrade --yes \
    && apt-get install --yes git rsync'
  rsync -avz deploy/checkout root@{{domain}}:deploy/checkout
  ssh root@{{domain}} 'cd deploy && ./checkout {{branch}} {{remote}} {{domain}}'

log unit='filepack' domain='filepack.com':
  ssh root@{{domain}} 'journalctl -fu {{unit}}'

lint:
  cargo lclippy --workspace --all-targets -- --deny warnings
  ./bin/forbid
  cargo fmt --all -- --check

outdated:
  cargo outdated --workspace --root-deps-only

unused:
  cargo +nightly udeps --workspace

coverage:
  cargo +nightly llvm-cov --branch --html
  open target/llvm-cov/html/index.html

update-changelog:
  echo >> CHANGELOG.md
  git log --pretty='format:- %s' >> CHANGELOG.md

update-contributors:
  cargo run --release --package update-contributors

doc package='':
  #!/usr/bin/env zsh
  set -euo pipefail
  if [ {{ package }} ]; then
    cargo doc --package {{ package }} --open
  else
    cargo doc --all --open
  fi

tmp:
  rm -rf tmp
  mkdir tmp

example: tmp
  cp README.md tmp
  mkdir tmp/src
  cp src/main.rs tmp/src
  cargo run create tmp
  cargo run sign --time tmp
  cat tmp/manifest.filepack | jq | pbcopy

publish: tmp
  #!/usr/bin/env bash
  set -euxo pipefail
  git clone git@github.com:casey/filepack.git tmp
  cd tmp
  VERSION=`bin/version`
  git tag -a $VERSION -m "Release $VERSION"
  git push origin $VERSION
  cargo publish

test-release-workflow:
  -git tag -d test-release
  -git push origin :test-release
  git tag test-release
  git push origin test-release

docs:
  open docs/index.html

shellcheck-install-script:
  shellcheck docs/install.sh

test-install-script:
  rm -f tmp/filepack
  ./docs/install.sh --to tmp

list-package:
  cargo package --list --allow-dirty

commit-release:
  git commit --edit --file release-commit-message.txt

test-progress-bar: tmp
  #!/usr/bin/env bash
  head -c 1073741824 /dev/urandom > tmp/data0
  for i in {1..9}; do
    cp tmp/data0 tmp/data$i
  done
  cargo run --release create tmp
  rm tmp/data*

sign-release: tmp
  #!/usr/bin/env bash
  set -euxo pipefail
  VERSION=`bin/version`
  gh release download \
    --repo casey/filepack \
    --pattern manifest.filepack \
    --dir tmp \
    $VERSION
  cargo run sign tmp/manifest.filepack
  gh release upload \
    --clobber \
    --repo casey/filepack \
    $VERSION \
    tmp/manifest.filepack

verify-release: tmp
  #!/usr/bin/env bash
  set -euxo pipefail
  VERSION=`bin/version`
  gh release download \
    --repo casey/filepack \
    --pattern '*' \
    --dir tmp \
    $VERSION
  cargo run verify tmp --key 3c977ea3a31cd37f0b540f02f33eab158f2ed7449f42b05613c921181aa95b79

profile:
  cargo flamegraph --test integration --profile dev

languages:
  brew install iso-codes
  jq -r \
    '.["639-3"][] \
    | select(.["alpha_2"] != null) \
    | "(\"\( .["alpha_2"] )\", \"\( .name | gsub(" \\([^)]*\\)"; "") )\"),"' \
    /opt/homebrew/share/iso-codes/json/iso_639-3.json

upload:
  rm -rf tmp/upload
  git clone . tmp/upload
  rm -rf tmp/upload/.git
  cargo build --release
  ./target/release/filepack create tmp/upload
  ./target/release/filepack upload tmp/upload/manifest.filepack --server https://filepack.com --auth master

download:
  #!/usr/bin/env bash
  set -euxo pipefail
  rm -rf tmp/download
  cargo build --release
  fingerprint=`./target/release/filepack fingerprint tmp/upload/manifest.filepack`
  ./target/release/filepack download --server https://filepack.com --package $fingerprint tmp/download

clear-server:
  rm -r ~/.filepack/{database.redb,files}
