set dotenv-load

ctr := "maple"
bin := "bash"

ls:
    just --list

up:
    docker compose -f compose.yaml up --remove-orphans

down:
    docker compose -f compose.yaml down -v --remove-orphans

build:
    docker compose -f compose.yaml build

run:
    docker compose -f compose.yaml run {{ctr}} {{bin}}

exec:
    docker compose -f compose.yaml exec {{ctr}} {{bin}}

dl trg:
    docker compose -f compose.yaml run \
        --remove-orphans \

link plist dir:
    python3 maple-py/link.py {{plist}} {{dir}}

# Dump tracks from Spotify playlists (ids, urls, or spotify: uris)
spotify ids="7f5tPIfj21UJI4zfDmH4rx 6sAl8LhU6ediFNJRuQQIQX":
    cd maple-py && uv run python -m maple.spotify {{ids}} --csv

# Download via sockseek (csv path, url, search string, ...) using sldl.tpl.conf settings
sockseek input:
    cd maple-py && uv run src/sockseek.py {{input}}

# Sync data/collection with one or more Spotify playlists (download + m3u8s).
collection ids="7f5tPIfj21UJI4zfDmH4rx 6sAl8LhU6ediFNJRuQQIQX":
    cd maple-py && uv run python -m maple.collection {{ids}}

# Build a rekordbox USB layout at <dst> from the collection at <src>.
# Converts FLAC→MP3 V0, extracts artwork, writes ANLZ (with PWAV/PWV3
# blue/white waveforms) and PIONEER/rekordbox/export.pdb.
export src="data/collection" dst="data/usb":
    cd maple-py && uv run python -m maple "{{justfile_directory() / src}}" "{{justfile_directory() / dst}}"

sync args:
    s5cmd {{args}}

# Sync data/usb/ → mounted USB stick (default /media/ntr/MAPLE), then
# safely unmount so the stick can be pulled without risk to the FAT
# write-back cache.
usb dst="/media/ntr/MAPLE":
    @test -d {{dst}} || (echo "USB not mounted at {{dst}}"; exit 1)
    @# -c (checksum), NOT --size-only: maple re-exports keep the SAME file size
    @# (ANLZ section lengths are deterministic) but change content (waveforms,
    @# pdb), so a size-only check silently skips them and leaves stale data.
    rsync -rtvh --delete -c --no-perms --no-owner --no-group --modify-window=2 "{{justfile_directory()}}/data/usb/" "{{dst}}/"
    sync
    @# Try udisksctl first (no sudo, handles the udev event); fall back to
    @# plain `umount` if udisks isn't available. Either way the recipe
    @# exits 0 even if the unmount fails (e.g. someone has a file open).
    @udisksctl unmount -b "$(findmnt -no SOURCE {{dst}})" 2>/dev/null && echo "unmounted {{dst}}" \
        || (umount {{dst}} 2>/dev/null && echo "unmounted {{dst}}") \
        || echo "warning: could not unmount {{dst}} — close any file browsers + retry"

# Build a FAT32 image of data/usb/ — ready to dd onto a stick.
# `size` must exceed the data/usb/ tree (rough rule: data size + ~50 MB FAT overhead).
usb-image size="2G" img="data/maple.img":
    @command -v mcopy >/dev/null || (echo "mtools not installed (apt install mtools)"; exit 1)
    @test -d "{{justfile_directory()}}/data/usb" || (echo "no data/usb (run \`just export\` first)"; exit 1)
    rm -f "{{justfile_directory()}}/{{img}}"
    truncate -s {{size}} "{{justfile_directory()}}/{{img}}"
    mkfs.vfat -F 32 -n MAPLE "{{justfile_directory()}}/{{img}}"
    cd "{{justfile_directory()}}/data/usb" && mcopy -i "{{justfile_directory()}}/{{img}}" -s -Q -p -m * ::/
    @echo "image ready:" && ls -lh "{{justfile_directory()}}/{{img}}"

# DESTRUCTIVE: dd data/maple.img onto a block device (e.g. /dev/sdb).
# Double-check `lsblk` first — wrong device wipes that drive.
[confirm("usb-flash will OVERWRITE the entire device. Proceed?")]
usb-flash dev img="data/maple.img":
    @test -b {{dev}} || (echo "{{dev}} is not a block device"; exit 1)
    @test -f "{{justfile_directory()}}/{{img}}" || (echo "no image at {{img}}; run \`just usb-image\` first"; exit 1)
    @lsblk -no NAME,SIZE,LABEL,MODEL {{dev}}
    sudo dd if="{{justfile_directory()}}/{{img}}" of={{dev}} bs=4M status=progress oflag=direct conv=fsync
    sudo sync
    @echo "done — safe to remove (try: udisksctl power-off -b {{dev}})"

usb-pdb dst="/Volumes/MAPLE":
    @test -d {{dst}}/PIONEER/rekordbox || (echo "no rekordbox dir at {{dst}}/PIONEER"; exit 1)
    COPYFILE_DISABLE=1 cp maple-py/data/MAPLE/PIONEER/rekordbox/export.pdb {{dst}}/PIONEER/rekordbox/export.pdb

usb-reset dst="/Volumes/MAPLE":
    @test -d {{dst}} || (echo "USB not mounted at {{dst}}"; exit 1)
    rm -rf {{dst}}/Contents {{dst}}/PIONEER
    COPYFILE_DISABLE=1 rsync -rtvh --no-perms --no-owner --no-group --modify-window=2 maple-py/data/MAPLE/Contents/ {{dst}}/Contents/
    COPYFILE_DISABLE=1 rsync -rtvh --no-perms --no-owner --no-group --modify-window=2 maple-py/data/MAPLE/PIONEER/ {{dst}}/PIONEER/

maple-setup:
    bash ./etc/maple_setup.sh

maple trg:
    sldl -c ./etc/sldl-macos.yaml {{trg}}
