version: "3"

vars:
    PYTHON:
        sh: echo "${PYTHON:-python}"
    NPM:
        sh: echo "${NPM:-pnpm}"
    LEGACY_ELECTRON_VERSION:
        sh: echo "${LEGACY_ELECTRON_VERSION:-30.0.8}"
    DOCKER_COMPOSE_CMD:
        sh: echo "${DOCKER_COMPOSE_CMD:-docker compose}"
    DOCKER_COMPOSE_FILE:
        sh: echo "${DOCKER_COMPOSE_FILE:-docker-compose.yml}"
    DOCKER_IMAGE:
        sh: echo "${DOCKER_IMAGE:-reticulum-meshchatx:local}"
    DOCKER_BUILDER:
        sh: echo "${DOCKER_BUILDER:-meshchatx-builder}"
    DOCKER_PLATFORMS:
        sh: echo "${DOCKER_PLATFORMS:-linux/amd64}"
    DOCKER_BUILD_FLAGS:
        sh: echo "${DOCKER_BUILD_FLAGS:---load}"
    DOCKER_BUILD_ARGS:
        sh: echo "${DOCKER_BUILD_ARGS:-}"
    DOCKER_CONTEXT:
        sh: echo "${DOCKER_CONTEXT:-.}"
    DOCKERFILE:
        sh: echo "${DOCKERFILE:-Dockerfile}"
    ANDROID_DIR:
        sh: echo "${ANDROID_DIR:-android}"
    PYTHON_SRC_DIR:
        sh: echo "${PYTHON_SRC_DIR:-${ANDROID_DIR}/app/src/main/python}"
    JNI_LIBS_DIR:
        sh: echo "${JNI_LIBS_DIR:-${ANDROID_DIR}/app/src/main/jniLibs}"
    RETICULUM_RNS_SRC:
        sh: echo "${RETICULUM_RNS_SRC:-./misc/RNS}"
    RETICULUM_LXMF_SRC:
        sh: echo "${RETICULUM_LXMF_SRC:-./misc/LXMF}"
    RETICULUM_LXST_SRC:
        sh: echo "${RETICULUM_LXST_SRC:-./misc/LXST}"
    SIDEBAND_CODEC2_WHL:
        sh: echo "${SIDEBAND_CODEC2_WHL:-./misc/pycodec2-3.0.1-cp311-cp311-linux_aarch64.whl}"
    SIDEBAND_LIB_ARM64:
        sh: echo "${SIDEBAND_LIB_ARM64:-./misc/libcodec2-arm64-v8a.so}"
    SIDEBAND_LIB_ARMEABI:
        sh: echo "${SIDEBAND_LIB_ARMEABI:-./misc/libcodec2-armeabi-v7a.so}"

tasks:
    default:
        desc: Show available tasks
        cmds:
            - task --list

    install:
        desc: Install all dependencies (syncs version, installs node modules and python deps)
        deps: [sync-version, node_modules, python]

    node_modules:
        desc: Install Node.js dependencies
        cmds:
            - "{{.NPM}} install"

    python:
        desc: Install Python dependencies using Poetry
        cmds:
            - "{{.PYTHON}} -m poetry install"

    run:
        desc: Run the application
        deps: [install]
        cmds:
            - "{{.PYTHON}} -m poetry run meshchat"

    develop:
        desc: Run the application in development mode
        cmds:
            - task: run

    build:
        desc: Build the application (frontend and backend)
        deps: [install]
        cmds:
            - "{{.NPM}} run build"

    build-frontend:
        desc: Build only the frontend
        deps: [node_modules]
        cmds:
            - "{{.NPM}} run build-frontend"

    wheel:
        desc: Build Python wheel package
        deps: [install]
        cmds:
            - "{{.PYTHON}} -m poetry build -f wheel"
            - "{{.PYTHON}} scripts/move_wheels.py"

    build-appimage:
        desc: Build Linux AppImage
        deps: [build]
        cmds:
            - "{{.NPM}} run electron-postinstall"
            - "{{.NPM}} run dist -- --linux AppImage"

    build-exe:
        desc: Build Windows portable executable
        deps: [build]
        cmds:
            - "{{.NPM}} run electron-postinstall"
            - "{{.NPM}} run dist -- --win portable"

    dist:
        desc: Build distribution (defaults to AppImage)
        cmds:
            - task: build-appimage

    electron-legacy:
        desc: Install legacy Electron version
        cmds:
            - "{{.NPM}} install --no-save electron@{{.LEGACY_ELECTRON_VERSION}}"

    build-appimage-legacy:
        desc: Build Linux AppImage with legacy Electron version
        deps: [build, electron-legacy]
        cmds:
            - "{{.NPM}} run electron-postinstall"
            - "{{.NPM}} run dist -- --linux AppImage"
            - "./scripts/rename_legacy_artifacts.sh"

    build-exe-legacy:
        desc: Build Windows portable executable with legacy Electron version
        deps: [build, electron-legacy]
        cmds:
            - "{{.NPM}} run electron-postinstall"
            - "{{.NPM}} run dist -- --win portable"
            - "./scripts/rename_legacy_artifacts.sh"

    clean:
        desc: Clean build artifacts and dependencies
        cmds:
            - rm -rf node_modules
            - rm -rf build
            - rm -rf dist
            - rm -rf python-dist
            - rm -rf meshchatx/public
            - task: android-clean

    sync-version:
        desc: Sync version numbers across project files
        cmds:
            - "{{.PYTHON}} scripts/sync_version.py"

    build-docker:
        desc: Build Docker image using buildx
        cmds:
            - |
                if ! docker buildx inspect {{.DOCKER_BUILDER}} >/dev/null 2>&1; then
                  docker buildx create --name {{.DOCKER_BUILDER}} --use >/dev/null
                else
                  docker buildx use {{.DOCKER_BUILDER}}
                fi
            - |
                docker buildx build --builder {{.DOCKER_BUILDER}} --platform {{.DOCKER_PLATFORMS}} \
                  {{.DOCKER_BUILD_FLAGS}} \
                  -t {{.DOCKER_IMAGE}} \
                  {{.DOCKER_BUILD_ARGS}} \
                  -f {{.DOCKERFILE}} \
                  {{.DOCKER_CONTEXT}}

    build-demo-docker:
        desc: Build Frontend-only Demo Docker image
        cmds:
            - task: build-docker
              vars:
                  DOCKERFILE: Dockerfile.demo
                  DOCKER_IMAGE: reticulum-meshchatx-demo:local

    run-docker:
        desc: Run Docker container using docker-compose
        cmds:
            - 'MESHCHAT_IMAGE="{{.DOCKER_IMAGE}}" {{.DOCKER_COMPOSE_CMD}} -f {{.DOCKER_COMPOSE_FILE}} up --remove-orphans --pull never reticulum-meshchatx'

    run-demo-docker:
        desc: Run Frontend-only Demo Docker container
        cmds:
            - 'MESHCHAT_DEMO_IMAGE="reticulum-meshchatx-demo:local" {{.DOCKER_COMPOSE_CMD}} -f docker-compose.demo.yml up --remove-orphans'

    android-init:
        desc: Initialize Gradle wrapper for Android project
        cmds:
            - |
                if [ ! -f "{{.ANDROID_DIR}}/gradle/wrapper/gradle-wrapper.jar" ]; then
                    echo "Downloading Gradle wrapper jar..."
                    mkdir -p "{{.ANDROID_DIR}}/gradle/wrapper"
                    curl -L -o "{{.ANDROID_DIR}}/gradle/wrapper/gradle-wrapper.jar" \
                        https://raw.githubusercontent.com/gradle/gradle/v8.12.1/gradle/wrapper/gradle-wrapper.jar || \
                        echo "Failed to download. Please run: cd {{.ANDROID_DIR}} && gradle wrapper --gradle-version 8.12.1"
                else
                    echo "Gradle wrapper already initialized."
                fi

    android-prepare:
        desc: Prepare Android build (copy meshchatx package and assets)
        deps: [build-frontend, android-init]
        cmds:
            - |
                echo "Copying meshchatx package and dependencies to Android project..."
                mkdir -p "{{.PYTHON_SRC_DIR}}"
                # Remove old copies to ensure fresh build
                rm -rf "{{.PYTHON_SRC_DIR}}/meshchatx"
                rm -rf "{{.PYTHON_SRC_DIR}}/RNS"
                rm -rf "{{.PYTHON_SRC_DIR}}/LXMF"
                rm -rf "{{.PYTHON_SRC_DIR}}/LXST"

                # Copy MeshChatX
                cp -r meshchatx "{{.PYTHON_SRC_DIR}}/"

                # Vendor RNS, LXMF, and LXST from ./misc/ and ./src/
                cp -r ./misc/RNS "{{.PYTHON_SRC_DIR}}/"
                cp -r ./misc/LXMF "{{.PYTHON_SRC_DIR}}/"
                cp -r ./misc/LXST "{{.PYTHON_SRC_DIR}}/"
                cp -r ./src/RNS "{{.PYTHON_SRC_DIR}}/" || true
                cp -r ./src/LXMF "{{.PYTHON_SRC_DIR}}/" || true
                cp -r ./src/LXST "{{.PYTHON_SRC_DIR}}/" || true

                # Copy pycodec2 wheel from ./misc
                cp "./misc/pycodec2-3.0.1-cp311-cp311-linux_aarch64.whl" "{{.PYTHON_SRC_DIR}}/" || true

                # Copy native libraries from ./misc
                mkdir -p "{{.JNI_LIBS_DIR}}/arm64-v8a"
                mkdir -p "{{.JNI_LIBS_DIR}}/armeabi-v7a"
                cp "./misc/libcodec2-arm64-v8a.so" "{{.JNI_LIBS_DIR}}/arm64-v8a/" || true
                cp "./misc/libcodec2-armeabi-v7a.so" "{{.JNI_LIBS_DIR}}/armeabi-v7a/" || true

                # Cleanup vendored packages (remove utilities/tests etc if needed, similar to Sideband)
                rm -rf "{{.PYTHON_SRC_DIR}}/RNS/Utilities/RNS"
                rm -rf "{{.PYTHON_SRC_DIR}}/LXMF/Utilities/LXMF"
                rm -rf "{{.PYTHON_SRC_DIR}}/LXST/Utilities/LXST"
            - |
                echo "Android build prepared. Don't forget to:"
                echo "1. Add Chaquopy license to {{.ANDROID_DIR}}/local.properties"
                echo "2. Open {{.ANDROID_DIR}}/ in Android Studio or run: task android-build"

    android-build:
        desc: Build Android APK (requires Android SDK and Chaquopy license)
        deps: [android-prepare]
        cmds:
            - cd "{{.ANDROID_DIR}}" && ./gradlew assembleDebug

    android-build-release:
        desc: Build Android APK (release, requires signing config)
        deps: [android-prepare]
        cmds:
            - cd "{{.ANDROID_DIR}}" && ./gradlew assembleRelease

    android-clean:
        desc: Clean Android build artifacts
        cmds:
            - cd "{{.ANDROID_DIR}}" && ./gradlew clean
            - rm -rf "{{.PYTHON_SRC_DIR}}/meshchatx"
