set windows-powershell := true

export ANDROID_MIC_COMMIT := `git rev-parse --short HEAD`
rootdir := ''
prefix := ''
debug := '0'
name := 'android-mic'
appid := 'io.github.teamclouday.AndroidMic'
cargo-target-dir := env('CARGO_TARGET_DIR', 'target')
bin-src := cargo-target-dir / if debug == '1' { 'debug' / name } else { 'release' / name }
base-dir := absolute_path(clean(rootdir / prefix))
share-dst := base-dir / 'share'
bin-dst := base-dir / 'bin' / name
desktop-dst := share-dst / 'applications' / appid + '.desktop'
metainfo-dst := share-dst / 'metainfo' / appid + '.metainfo.xml'
icon-dst := share-dst / 'icons/hicolor/scalable/apps' / appid + '.svg'

default: build-release

pull: prettier fix test

build-debug *args:
    cargo build {{ args }}

build-release *args:
    cargo build --release {{ args }}

install:
    install -Dm0755 {{ bin-src }} {{ bin-dst }}
    install -Dm0644 res/linux/desktop_entry.desktop {{ desktop-dst }}
    install -Dm0644 res/linux/metainfo.xml {{ metainfo-dst }}
    install -Dm0644 res/linux/app_icon.svg {{ icon-dst }}

uninstall:
    rm -f {{ bin-dst }}
    rm -f {{ desktop-dst }}
    rm -f {{ metainfo-dst }}
    rm -f {{ icon-dst }}

nsis:
    cargo packager --release --formats nsis --verbose

test:
    cargo test --workspace --all-features

fix: fmt fmt-just
    cargo clippy --workspace --all-features --fix --allow-dirty --allow-staged

fmt:
    cargo fmt --all

fmt-just:
    just --fmt --unstable

prettier:
    # install on Debian: sudo snap install node --classic
    # npx is the command to run npm package, node is the runtime
    npx prettier -w .

metainfo-check:
    appstreamcli validate --pedantic --explain --strict res/linux/metainfo.xml

install_nsis: build-release nsis
    Start-Process (Get-ChildItem .\target\release\android-mic_*_x64-setup.exe | Sort-Object LastWriteTime -Descending | Select-Object -First 1).FullName
