POSTGRES_URL := "postgresql://postgres:passw0rd@localhost:5432/postgres?sslmode=disable&search_path=public"
DYLD_FALLBACK_LIBRARY_PATH := "/Library/Developer/CommandLineTools/usr/lib"

install-toolchain:
    rustup install

update:
    cargo update

check:
    DYLD_FALLBACK_LIBRARY_PATH="{{ DYLD_FALLBACK_LIBRARY_PATH }}" \
    cargo check

set-up:
    mkcert -cert-file certificates/development.pem -key-file certificates/development-key.pem localhost 127.0.0.1 ::1
    docker run --publish=0.0.0.0:5432:5432 --name=hm-postgres --env=POSTGRES_PASSWORD=passw0rd --detach docker.io/supabase/postgres:17.6.1.040

migrate-up:
    migrate -database "{{ POSTGRES_URL }}" -path migrations up

migrate-down:
    migrate -database "{{ POSTGRES_URL }}" -path migrations down

dev:
    DYLD_FALLBACK_LIBRARY_PATH="{{ DYLD_FALLBACK_LIBRARY_PATH }}" \
    ENVIRONMENT=development cargo run

test:
    cargo test --all-features

lint-rust-clippy:
    DYLD_FALLBACK_LIBRARY_PATH="{{ DYLD_FALLBACK_LIBRARY_PATH }}" \
    cargo clippy

lint-rust-clippy-fix:
    DYLD_FALLBACK_LIBRARY_PATH="{{ DYLD_FALLBACK_LIBRARY_PATH }}" \
    cargo clippy --fix --allow-dirty --allow-staged

lint-rust-rustfmt:
    cargo fmt --all -- --check

lint-rust-rustfmt-fix:
    cargo fmt --all

docker-build:
    cd ../.. && \
    docker build --file=api/api-rust/Dockerfile --tag=ghcr.io/hongbo-miao/hm-api-rust:latest .

docker-push:
    docker push ghcr.io/hongbo-miao/hm-api-rust:latest

docker-run:
    docker run --publish=0.0.0.0:36147:36147 --name=hm-api-rust --rm ghcr.io/hongbo-miao/hm-api-rust:latest

brew-install-opencv:
    brew install opencv

listen-police-audio-stream:
    websocat --binary 'ws://localhost:36147/ws/police-audio-stream?police_stream_id=chicago.police' \
        | sox --type=raw --rate=16000 --encoding=signed --bits=16 --channels=1 - --default-device

listen-emergency-audio-stream:
    websocat --binary 'ws://localhost:36147/ws/emergency-audio-stream?emergency_stream_id=lincoln.fire' \
        | perl -e 'while (read(STDIN, $len, 4)) { $n = unpack("V", $len); read(STDIN, $buf, $n); print $buf; }' \
        | sox --type=raw --rate=16000 --encoding=signed --bits=16 --channels=1 - --default-device
