---
version: '3'

vars:
  VENTOY_DOWNLOAD_CONFIG_URL: https://gitlab.com/megabyte-labs/jumpusb/-/raw/master/local/distros.json
  VENTOY_USB_PATH:
    sh: |
      if [ -d /run/media/user/Ventoy ]; then
        echo "/run/media/user/Ventoy"
      else
        echo "./jumpusb-fs"
      fi

tasks:
  clone:
    cmds:
      - |
        mkdir -p {{.VENTOY_USB_PATH}}
        cd {{.VENTOY_USB_PATH}}
        git init
        git remote add origin https://gitlab.com/megabyte-labs/jumpusb.git
        git fetch
        git pull origin master

  download:
    deps:
      - :install:software:axel
      - :install:software:p7zip
    todo: Add validation of md5/sha256 and add update functionality to VENTOY_DOWNLOAD_CONFIG_URL
    cmds:
      - task: setup:persistence
      - curl -sSL '{{.VENTOY_DOWNLOAD_CONFIG_URL}}' > .ventoy.json
      - |
        jq -c '.distros[]' .ventoy.json | while read VENTOY_OS; do
          MIRROR_COUNT="$(jq -r -n --argjson in "$VENTOY_OS" '$in.mirrors | length')"
          VENTOY_URL="$(jq -r -n --argjson in "$VENTOY_OS" '$in.mirrors[]')"
          if [ "$MIRROR_COUNT" == '1' ]; then
            VENTOY_URL_AXEL="$VENTOY_URL"
          else
            VENTOY_URL_AXEL="$(jq -r -n --argjson in "$VENTOY_OS" '$in.mirrors | join(" ")')"
          fi
          VENTOY_MD5="$(jq -r -n --argjson in "$VENTOY_OS" '$in.md5')"
          VENTOY_PATH="$(jq -r -n --argjson in "$VENTOY_OS" '$in.path')"
          VENTOY_PER_BASE="$(jq -r -n --argjson in "$VENTOY_OS" '$in.persistence_base')"
          VENTOY_PER_FILE="$(jq -r -n --argjson in "$VENTOY_OS" '$in.persistence_file')"
          mkdir -p "$(dirname ./jumpusb-fs/$VENTOY_PATH)"
          if [ ! -f "./jumpusb-fs/$VENTOY_PATH" ] || [ -f "./jumpusb-fs/${VENTOY_PATH}.st" ]; then
            mkdir -p "$(dirname ./jumpusb-fs/${VENTOY_PATH})"
            axel -o "./jumpusb-fs/${VENTOY_PATH}" -n 14 $VENTOY_URL_AXEL
            if [[ "$VENTOY_URL" == *.gz ]]; then
              mv "./jumpusb-fs/${VENTOY_PATH}" "./jumpusb-fs/${VENTOY_PATH}.gz"
              gzip -d "./jumpusb-fs/${VENTOY_PATH}.gz"
            elif [[ "$VENTOY_URL" == *.bz2 ]]; then
              mv "./jumpusb-fs/${VENTOY_PATH}" "./jumpusb-fs/${VENTOY_PATH}.bz2"
              bzip2 -d "./jumpusb-fs/${VENTOY_PATH}.bz2"
            fi
          fi
          if [ "$VENTOY_PER_FILE" != 'null' ] && [ ! -f "./jumpusb-fs/persistence/$VENTOY_PER_FILE" ]; then
            7z -y x "./jumpusb-fs/persistence/$VENTOY_PER_BASE"
            UNCOMPRESSED_FILE="$(echo $VENTOY_PER_BASE | sed 's/.7z$//')"
            mv "$UNCOMPRESSED_FILE" "./jumpusb-fs/persistence/$VENTOY_PER_FILE"
          fi
        done

  install:
    cmds:
      - task: install:disk

  install:detect:usb:
    summary: |
      # Detects the Drive Path of USB Last Inserted

      This task detects the drive path of the last USB that was inserted. After
      it detects the drive path, it writes the path to the file that is passed in
      as the ANSWER_FILE Go template variable.
    cmds:
      - |
        # TODO: Add bash script / darwin script that detects the drive path of

  install:disk:
    vars:
      TARGET_DRIVE: '{{if .CLI_ARGS}}{{.CLI_ARGS}}{{else}}{{.TARGET_DRIVE}}{{end}}'
    cmds:
      - curl -sSL https://github.com/ventoy/Ventoy/releases/download/v1.0.74/ventoy-1.0.74-linux.tar.gz > ventoy.tar.gz
      - tar -xzvf ventoy.tar.gz
      - .config/log info 'Running sudo command `sudo sh ventoy/Ventoy2Disk.sh -ui {{.TARGET_DRIVE}}`'
      - sudo sh ventoy/Ventoy2Disk.sh -ui {{.TARGET_DRIVE}}
      - rm -rf ventoy
    status:
      - '[ -z "{{.TARGET_DRIVE}}" ]'

  setup:persistence:
    cmds:
      - curl -sSL https://github.com/ventoy/backend/releases/download/v5.0/images.zip > images.zip
      - unzip images.zip
      - rm images.zip
      - mkdir -p {{.VENTOY_USB_PATH}}/persistence
      - mv images/* {{.VENTOY_USB_PATH}}/persistence
      - rm -rf images