# To install just on a per-project basis
# 1. Activate your virtual environemnt
# 2. uv add --dev rust-just
# 3. Use just within the activated environment

drive_uuid := "77688511-78c5-4de3-9108-b631ff823ef4"
user :=  file_stem(home_dir())
def_drive := join("/media", user, drive_uuid)
project := file_stem(justfile_dir())
local_env := join(justfile_dir(), ".env")

# list all recipes
default:
    just --list

# Install tools globally
tools:
    uv tool install twine
    uv tool install ruff

# Add conveniente development dependencies
dev:
    uv add --dev pytest

# Build the package
build:
    rm -fr dist/*
    uv build

# Publish the package to PyPi
publish pkg="zptess": build
    twine upload -r pypi dist/*
    uv run --no-project --with {{pkg}} --refresh-package {{pkg}} \
        -- python -c "from {{pkg}} import __version__; print(__version__)"

# Publish to Test PyPi server
test-publish pkg="zptess": build
    twine upload --verbose -r testpypi dist/*
    uv run --no-project  --with {{pkg}} --refresh-package {{pkg}} \
        --index-url https://test.pypi.org/simple/ \
        --extra-index-url https://pypi.org/simple/ \
        -- python -c "from {{pkg}} import __version__; print(__version__)"

# Adds lica source library as dependency. 'version' may be a tag or branch
lica-dev version="main":
    #!/usr/bin/env bash
    set -exuo pipefail
    echo "Removing previous LICA dependency"
    uv add aiohttp pyserial-asyncio aioserial tabulate
    uv remove lica || echo "Ignoring non existing LICA library";
    if [[ "{{ version }}" =~ [0-9]+\.[0-9]+\.[0-9]+ ]]; then
        echo "Adding LICA source library --tag {{ version }}"; 
        uv add git+https://github.com/guaix-ucm/lica --tag {{ version }};
    else
        echo "Adding LICA source library --branch {{ version }}";
        uv add git+https://github.com/guaix-ucm/lica --branch {{ version }};
    fi

# Adds lica release library as dependency with a given version
lica-rel version:
    #!/usr/bin/env bash
    set -exuo pipefail
    echo "Removing previous LICA dependency"
    uv remove lica
    echo "Adding release version of LICA library";
    uv add --refresh-package lica lica[photometer,tabular];
    uv remove aiohttp aioserial pyserial-asyncio tabulate

# Backup .env to storage unit
env-bak drive=def_drive: (check_mnt drive) (env-backup join(drive, "env", project))

# Restore .env from storage unit
env-rst drive=def_drive: (check_mnt drive) (env-restore join(drive, "env", project))


# ========================= #
# QUCK COMMAND LINE TESTING #
# ========================= #

# Writes new zero point to photometer
test-write zp verbose="" trace="":
    #!/usr/bin/env bash
    set -euxo pipefail
    uv run zp-write --console {{verbose}} {{trace}} test -z {{zp}}

# Reads test/ref/both photometers
test-read verbose="" trace="" which="test" N="10" :
    #!/usr/bin/env bash
    set -euxo pipefail
    uv run zp-read --console {{verbose}} {{trace}} {{which}} -N {{N}}

# Calibrate photometer
test-calib verbose="" trace="" persist="":
    #!/usr/bin/env bash
    set -euxo pipefail
    uv run zp-calib --console {{verbose}} {{trace}} test -b 9 -R 3 -P 5 {{persist}}

# Open a new batch
test-open  verbose="" trace="":
    #!/usr/bin/env bash
    set -euxo pipefail
    uv run zp-batch --console {{verbose}} {{trace}} begin

# Close current open batch
test-close  verbose="" trace="":
    #!/usr/bin/env bash
    set -euxo pipefail
    uv run zp-batch --console {{verbose}} {{trace}} end

# Close current open batch
test-purge  verbose="" trace="":
    #!/usr/bin/env bash
    set -euxo pipefail
    uv run zp-batch --console {{verbose}} {{trace}} purge

# See orphan calibrations not within a batch
test-orphan  verbose="" trace="":
    #!/usr/bin/env bash
    set -euxo pipefail
    uv run zp-batch --console {{verbose}} {{trace}} orphan

# Close current open batch
test-view  verbose="" trace="":
    #!/usr/bin/env bash
    set -euxo pipefail
    uv run zp-batch --console {{verbose}} {{trace}} view

# Export latest batch
test-export-latest  verbose="" trace="":
    #!/usr/bin/env bash
    set -euxo pipefail
    uv run zp-batch --console {{verbose}} {{trace}} export --latest

# Export latest batch and send email
test-export-email  verbose="" trace="":
    #!/usr/bin/env bash
    set -euxo pipefail
    uv run zp-batch --console {{verbose}} {{trace}} export --latest --email

# Export all summaries
test-export-all  verbose="" trace="":
    #!/usr/bin/env bash
    set -euxo pipefail
    uv run zp-batch --console {{verbose}} {{trace}} export --all

# =======================================================================
# DAY TO DAY CALIBRATION MANAGEMENT RECIPES
#
# Copy these recipes into a separate justfile and run all 
# calibration-related task from there
# ======================================================================= 

#export how many calibrations have been made in a period, regardless of batches
count start="" end="":
    #!/usr/bin/env bash
    set -euxo pipefail
    since={{start}}
    until={{end}}
    if [ -n "$since" ]; then
        since="--since $since"
    fi
    if [ -n "$until" ]; then
        until="--until $until"
    fi
    uv run zp-tools --console --log-file zptool.log --trace count --detailed --mode AUTO $since $until 


#export single calibration data
single date:
    #!/usr/bin/env bash
    set -euxo pipefail
    uv run zp-tools --console --log-file zptool.log --trace single --session {{date}}

# Open a new batch
open:
    #!/usr/bin/env bash
    set -euxo pipefail
    uv run zp-batch --console --log-file zptool.log --trace begin

# Close current open batch
close  verbose="" trace="":
    #!/usr/bin/env bash
    set -euxo pipefail
    uv run zp-batch --console --log-file zptool.log --trace end

# Reads [test|ref|both] photometers N times
read which="both" N="10" :
    #!/usr/bin/env bash
    set -euxo pipefail
    uv run zp-read --console --log-file zptess.log --trace {{which}} -N {{N}} --histo

# manually write a new zero point to a photometer
write zp dry_run="":
    #!/usr/bin/env bash
    set -euxo pipefail
    uv run zp-write --console  --log-file zptess.log --trace  test -z {{zp}} {{dry_run}}

# Display photometer info and quit
info:
    #!/usr/bin/env bash
    set -euxo pipefail
    uv run zp-calib --console --log-file zptess.log --trace test --info

# Calibrate a new photometer, but don't write new ZP nor update database
dry-run buffer="25":
    #!/usr/bin/env bash
    set -euxo pipefail
    uv run zp-calib --console --log-file zptess.log --trace test --buffer {{buffer}} --plot-both

# Calibrate a new photometer and stores results in database
calib:
    #!/usr/bin/env bash
    set -euxo pipefail
    uv run zp-calib --console --log-file zptess.log --trace test --update --persist

# Export all summaries
summary:
    #!/usr/bin/env bash
    set -euxo pipefail
    uv run zp-batch --console --log-file zptool.log --trace export --all

# Export latest batch and send email
export:
    #!/usr/bin/env bash
    set -euxo pipefail
    uv run zp-batch --console --log-file zptool.log --trace export --latest --email

# See orphan calibrations not within a batch
orphan  verbose="" trace="":
    #!/usr/bin/env bash
    set -euxo pipefail
    uv run zp-batch --console --trace orphan

# Close current open batch
view  verbose="" trace="":
    #!/usr/bin/env bash
    set -euxo pipefail
    uv run zp-batch --console --trace view

# Close current open batch
purge:
    #!/usr/bin/env bash
    set -euxo pipefail
    uv run zp-batch --console --log-file zptool.log --trace purge

plot session="2025-07-08T10:45:12":
    #!/usr/bin/env bash
    set -euxo pipefail
    uv run zp-plot --trace --console session --session {{session}} --both

# Backup zptess database and log file
backup drive=def_drive: (check_mnt drive)
    #!/usr/bin/env bash
    set -exuo pipefail
    bak_dir={{ join(drive, "zptess") }}
    [ ! -d "${bak_dir}"  ] && mkdir -p ${bak_dir}
    cp .env ${bak_dir}
    cp zptess.db ${bak_dir}
    cp zptess.log ${bak_dir}
    cp zptool.log ${bak_dir}
    cp zpdbase.log ${bak_dir}
    cp justfile ${bak_dir}

# Restore zptess database and log file
restore drive=def_drive: (check_mnt drive)
    #!/usr/bin/env bash
    set -exuo pipefail
    bak_dir={{ join(drive, "zptess") }}
    cp .env ${bak_dir}/.env .
    cp ${bak_dir}/zptess.db .
    cp ${bak_dir}/zptool.log  .
    cp ${bak_dir}/zpdbase.log  .
    cp ${bak_dir}/justfile .

# =======================================================================

[private]
db-restore date:
    #!/usr/bin/env bash
    set -exuo pipefail
    cp {{ def_drive }}/zptess/zptess-{{date}}.db zptess.prod.db
    

[private]
check_mnt mnt:
    #!/usr/bin/env bash
    set -euo pipefail
    if [[ ! -d  {{ mnt }} ]]; then
        echo "Drive not mounted: {{ mnt }}"
        exit 1 
    fi

[private]
env-backup bak_dir:
    #!/usr/bin/env bash
    set -exuo pipefail
    if [[ ! -f  {{ local_env }} ]]; then
        echo "Can't backup: {{ local_env }} doesn't exists"
        exit 1 
    fi
    mkdir -p {{ bak_dir }}
    cp {{ local_env }} {{ bak_dir }}
    cp zptess.prod.db {{ bak_dir }}
    cp zptess.db {{ bak_dir }}
  
[private]
env-restore bak_dir:
    #!/usr/bin/env bash
    set -euxo pipefail
    if [[ ! -f  {{ bak_dir }}/.env ]]; then
        echo "Can't restore: {{ bak_dir }}/.env doesn't exists"
        exit 1 
    fi
    cp {{ bak_dir }}/.env {{ local_env }}
    cp {{ bak_dir }}/zptess.prod.db .
    cp {{ bak_dir }}/zptess.db .
