# https://taskfile.dev

version: '3'

dotenv:
  - '.env'

vars:
  PROJECT_NAME: quilibrium
  SERVICE_NAME: node

tasks:
  up:
    desc: Run a new Quilibrium and related containers, through docker compose.
    cmds:
      - docker compose up -d

  down:
    desc: Take down the Quilibrium containers, through docker compose.
    cmds:
      - docker compose down

  pull:
    desc: Pull new Docker images corresponding to the Quilibrium containers, through docker compose.
    cmds:
      - docker compose pull

  update:
    desc: Pull new Docker images corresponding to the Quilibrium containers, then restart all containers.
    cmds:
      - task: pull
      - task: down
      - task: up

  shell:
    desc: Drop into a shell inside the running container.
    cmds:
      - docker compose exec -it {{.SERVICE_NAME}} sh

  logs:
    desc: Print the logs of the running Quilibrium container.
    cmds:
      - docker compose logs -f

  logs-folder:
    desc: Show where Docker stores the logs for the Quilibrium node. You need root permissions to access the folder.
    cmds:
      - "docker container inspect {{.PROJECT_NAME}}-{{.SERVICE_NAME}}-1 | grep LogPath | cut -d : -f 2 | cut -d '\"' -f 2 | xargs dirname"

  node-info:
    desc: Displays node related info for a running node.
    cmds:
      - docker compose exec node node -node-info

  backup:
    desc: Create a backup file with the critical configuration files.
    prompt: You will be prompted for root access. Make sure you verify the generated backup file. Continue?
    preconditions:
      - sh: 'test -d .config'
        msg: '.config does not exists!'
      - sh: 'test -f .config/config.yml'
        msg: '.config/config.yml does not exists!'
      - sh: 'test -f .config/keys.yml'
        msg: '.config/keys.yml does not exists!'
      - sh: '! test -f backup.tar.gz'
        msg: 'A previous backup.tar.gz found in the current folder!'
    sources:
      - '.config/config.yml'
      - '.config/keys.yml'
    generates:
      - 'backup.tar.gz'
    cmds:
      - |
        export TMP_DIR=$(mktemp -d)
        export TASK_DIR=$(pwd)
        sudo cp .config/config.yml $TMP_DIR
        sudo cp .config/keys.yml $TMP_DIR
        sudo chown $(whoami):$(id -gn) $TMP_DIR/*
        cd $TMP_DIR
        tar -czf $TASK_DIR/backup.tar.gz *
        cd $TASK_DIR
        sudo rm -rf $TMP_DIR
        echo "Backup saved to: backup.tar.gz"
        echo "Do not assume you have a backup unless you verify it!!!"
    silent: true

  restore:
    desc: Restores a backup file with the critical configuration files.
    preconditions:
      - sh: '! test -d .config'
        msg: '.config already exists, restore cannot be performed safely!'
      - sh: 'test -f backup.tar.gz'
        msg: 'backup.tar.gz not found in the current folder!'
    sources:
      - 'backup.tar.gz'
    generates:
      - '.config/config.yml'
      - '.config/keys.yml'
    cmds:
      - |
        mkdir .config
        tar -xzf backup.tar.gz -C .config
        echo "Backup restored from: backup.tar.gz"
    silent: true

  test:port:
    desc: Test if the P2P port is visible to the world.
    preconditions:
      - sh: 'test -x "$(command -v nc)"'
        msg: 'nc is not installed, install with "sudo apt install netcat"'
      - sh: 'test -n "$NODE_PUBLIC_NAME"'
        msg: 'The public DNS name or IP address of the server must be set in NODE_PUBLIC_NAME.'
    cmds:
      - 'nc -vzu ${NODE_PUBLIC_NAME} ${QUILIBRIUM_P2P_PORT:=8336}'
