---
version: '3'

env:
  GITHUB_TOKEN:
    sh: |
      if [ -n "$GITHUB_TOKEN" ]; then
        echo "$GITHUB_TOKEN"
      else
        # Public repository view PAT (user: TimeTravelersHackedMe)
        # The gh CLI used to download assets needs a PAT or the user to login so this prevents that requirement
        # Split into two variables so GitHub's detect/remove does not mess with anything
        FIRST_PART="github_pat_"
        SECOND_PART="11ABLI5FQ0RnibLOzu4kY0_SKzixo0dQRKPrniu0vMVabt"
        THIRD_PART="mlwrDNBzDaxsWptk3QRA2OIMLFQWKgEGBCgR"
        echo "${FIRST_PART}${SECOND_PART}${THIRD_PART}"
      fi

tasks:
  act:
    cmds:
      - task: install:gh
        vars:
          BIN_NAME: act
          PACKAGE: nektos/act

  azurefunctions:
    cmds:
      - task: azurefunctions:{{OS}}

  azurefunctions:darwin:
    cmds:
      - echo 'Install azure-functions-core-tools using Homebrew on MacOS'

  azurefunctions:linux:
    cmds:
      - gh release download -R Azure/azure-functions-core-tools -p "*linux-x64*.zip"
      - unzip -q -d azure-functions-cli Azure.Functions.Cli.linux-x64*.zip
      - mv azure-functions-cli /opt
      - ln -s /opt/azure-functions-cli/func /usr/local/bin/func
      - ln -s /opt/azure-functions-cli/gozip /usr/local/bin/gozip
    status:
      - '[ -f /usr/local/bin/func ]'

  azurefunctions:windows:
    cmds:
      - echo 'Install azure-functions-core-tools using Chocolatey on Windows'

  bat:
    cmds:
      - task: install:gh
        vars:
          BIN_NAME: bat
          PACKAGE: sharkdp/bat
          TAR_OPTIONS: ' --strip-components 1'

  betwixt:
    cmds:
      - task: betwixt:{{OS}}

  betwixt:darwin:
    cmds:
      - gh release download --clobber -R kdzwinel/betwixt -p "*darwin-x64.zip"
      - unzip -q Betwixt-darwin-x64.zip
      - mv Betwixt-darwin-x64/Betwixt.app /Applications
      - rm -f Betwixt-darwin-x64.zip
      - rm -rf Betwixt-darwin-x64
    status:
      - '[ -d /Applications/Betwixt.app ]'

  betwixt:linux:
    cmds:
      - gh release download -R kdzwinel/betwixt -p "*linux-x64.zip"
      - unzip -q Betwixt-linux-x64.zip
      - mv Betwixt-linux-x64 /opt
      - ln -s /opt/Betwixt-linux-x64/Betwixt /usr/local/bin/betwixt
    status:
      - '[ -f /opt/Betwixt-linux-x64/Betwixt ]'

  betwixt:windows:
    cmds:
      - gh release download -R kdzwinel/betwixt -p "*win32-x64.zip"
      - cmd /C powershell 'md "C:\Program Files\Betwixt" -Force'
      - tar -xf Betwixt-win32-x64.zip -C "C:\Program Files\Betwixt" --strip-components 1
      - cmd /C del Betwixt-win32-x64.zip
      - cmd /C powershell 'New-Item -Value "C:\Program Files\Betwixt\Betwixt.exe" -Path "C:\Users\Public\Desktop\Betwixt" -ItemType SymbolicLink -Force'
    status:
      - '[ -f "C:\Program Files\Betwixt\Betwixt.exe" ]'

  bitwarden:
    cmds:
      - task: bw:{{OS}}
    status:
      - '[ -f /usr/local/bin/bw ]'

  bivac:
    cmds:
      - task: install:gh
        vars:
          BIN_NAME: bivac
          PACKAGE: camptocamp/bivac

  bw:darwin:
    cmds:
      - gh release download -R bitwarden/cli -p "bw-macos*.zip"
      - mkdir -p /usr/local/bin && unzip -q bw-macos*.zip -d /usr/local/bin && rm -f bw-macos*.zip
      - chmod +x /usr/local/bin/bw

  bw:linux:
    cmds:
      - gh release download -R bitwarden/cli -p "bw-linux*.zip"
      - mkdir -p /usr/local/bin && unzip -q bw-linux*.zip -d /usr/local/bin && rm -f bw-linux*.zip
      - chmod +x /usr/local/bin/bw

  bw:windows:
    cmds:
      - gh release download -R bitwarden/cli -p "bw-windows*.zip"
      - powershell 'md C:/ProgramData/bin -Force; tar -xf bw-windows*.zip -C C:/ProgramData/bin; del bw-windows*.zip -Force'

  cerebro:
    cmds:
      - task: cerebro:{{OS}}

  cerebro:darwin:
    cmds:
      - echo 'Install Cerebro using Homebrew.'

  cerebro:linux:
    cmds:
      - task: install:appimage
        vars:
          DEST_APPIMAGE_NAME: Cerebro
          PACKAGE: cerebroapp/cerebro

  cerebro:windows:
    cmds:
      - echo 'Cerebro is not available for Windows.'

  consul-cli:
    cmds:
      - task: install:gh
        vars:
          BIN_NAME: consul-cli
          PACKAGE: mantl/consul-cli
          TAR_OPTIONS: ' --strip-components 1'

  croc:
    cmds:
      - task: install:gh
        vars:
          BIN_NAME: croc
          PACKAGE: schollz/croc

  ctop:
    cmds:
      - task: install:gh
        vars:
          BIN_NAME: ctop
          PACKAGE: bcicen/ctop

  cumulus:
    cmds:
      - echo 'Cumulus is available only for MacOS, install using Homebrew on MacOS.'

  dasel:
    cmds:
      - task: install:gh
        vars:
          BIN_NAME: dasel
          PACKAGE: TomWright/dasel

  dat:
    cmds:
      - task: install:gh
        vars:
          BIN_NAME: dat
          PACKAGE: dat-ecosystem-archive/dat
          TAR_OPTIONS: ' --strip-components 1'

          ZIP_OPTIONS: ' -j'
  delta:
    cmds:
      - task: install:gh
        vars:
          BIN_NAME: delta
          PACKAGE: dandavison/delta
          TAR_OPTIONS: ' --strip-components 1'

  deno:
    cmds:
      - task: install:gh
        vars:
          BIN_NAME: deno
          PACKAGE: denoland/deno

  deta:
    cmds:
      - task: install:gh
        vars:
          BIN_NAME: deta
          PACKAGE: deta/deta-cli

  direnv:
    cmds:
      - task: install:gh
        vars:
          BIN_NAME: direnv
          PACKAGE: direnv/direnv

  dive:
    cmds:
      - task: install:gh
        vars:
          BIN_NAME: dive
          PACKAGE: wagoodman/dive

  docker-pushrm:
    cmds:
      - task: install:gh
        vars:
          BIN_NAME: docker-pushrm
          PACKAGE: christian-korneck/docker-pushrm

  dockle:
    cmds:
      - task: install:gh
        vars:
          BIN_NAME: dockle
          PACKAGE: goodwithtech/dockle

  doctl:
    cmds:
      - task: install:gh
        vars:
          BIN_NAME: doctl
          PACKAGE: digitalocean/doctl

  dog:
    cmds:
      - task: install:gh
        vars:
          BIN_NAME: dog
          PACKAGE: ogham/dog

  duf:
    cmds:
      - task: install:gh
        vars:
          BIN_NAME: duf
          PACKAGE: muesli/duf

  dust:
    cmds:
      - task: install:gh
        vars:
          BIN_NAME: dust
          PACKAGE: bootandy/dust
          TAR_OPTIONS: ' --strip-components 1'

  fd:
    cmds:
      - task: install:gh
        vars:
          BIN_NAME: fd
          PACKAGE: sharkdp/fd
          TAR_OPTIONS: ' --strip-components 1'
  # Fails with the error HTTP 404: Not Found (https://api.github.com/repos/henryboldi/felony/releases/latest) because there is no 'latest' release
  # felony:
  #   cmds:
  #     - task: install:gh
  #       vars:
  #         BIN_NAME: felony
  #         PACKAGE: henryboldi/felony

  ffsend:
    cmds:
      - task: install:gh
        vars:
          BIN_NAME: ffsend
          PACKAGE: timvisee/ffsend

  filebrowser:
    cmds:
      - task: install:gh
        vars:
          BIN_NAME: filebrowser
          PACKAGE: filebrowser/filebrowser

  fq:
    cmds:
      - task: install:gh
        vars:
          BIN_NAME: fq
          PACKAGE: wader/fq

  fuego:
    cmds:
      - task: install:gh
        vars:
          BIN_NAME: fuego
          PACKAGE: sgarciac/fuego

  fusion:
    cmds:
      - task: install:gh
        vars:
          BIN_NAME: fusion
          PACKAGE: edgelaboratories/fusion

  ganache:
    cmds:
      - task: ganache:{{OS}}

  ganache:darwin:
    cmds:
      - task: install:dmg
        vars:
          DMG_NAME: Ganache
          PACKAGE: trufflesuite/ganache-ui

  ganache:linux:
    cmds:
      - task: install:appimage
        vars:
          DEST_APPIMAGE_NAME: Ganache
          PACKAGE: trufflesuite/ganache-ui

  ganache:windows:
    cmds:
      - task: install:gh
        vars:
          BIN_NAME: Ganache
          EXE_PATTERN: '-p *setup.exe'

          PACKAGE: trufflesuite/ganache-ui
  gh:
    cmds:
      - task: install:gh
        vars:
          BIN_NAME: gh
          PACKAGE: cli/cli
          TAR_OPTIONS: ' --strip-components 1'

  gitify:
    cmds:
      - task: gitify:{{OS}}

  gitify:darwin:
    cmds:
      - task: install:dmg
        vars:
          DMG_NAME: gitify
          PACKAGE: manosim/gitify

  gitify:linux:
    cmds:
      - task: install:appimage
        vars:
          DEST_APPIMAGE_NAME: Gitify
          PACKAGE: manosim/gitify

  gitify:windows:
    cmds:
      - task: install:gh
        vars:
          BIN_NAME: gitify
          EXE_PATTERN: '-p *Setup*.exe'

          PACKAGE: manosim/gitify
  gitleaks:
    cmds:
      - task: install:gh
        vars:
          BIN_NAME: gitleaks
          PACKAGE: zricethezav/gitleaks

  gitomatic:
    cmds:
      - task: install:gh
        vars:
          BIN_NAME: gitomatic
          PACKAGE: muesli/gitomatic

  glab:
    cmds:
      - task: install:gh
        vars:
          BIN_NAME: glab
          PACKAGE: profclems/glab
          TAR_OPTIONS: ' --strip-components 1'

  gojq:
    cmds:
      - task: install:gh
        vars:
          BIN_NAME: gojq
          PACKAGE: itchyny/gojq
          TAR_OPTIONS: ' --strip-components 1'

  hey:
    cmds:
      - task: hey:{{OS}}

  hey:darwin:
    cmds:
      - curl https://hey-release.s3.us-east-2.amazonaws.com/hey_darwin_amd64 -o /usr/local/bin/hey
      - chmod +x /usr/local/bin/hey
    status:
      - '[ -f /usr/local/bin/hey ]'

  hey:linux:
    cmds:
      - curl https://hey-release.s3.us-east-2.amazonaws.com/hey_linux_amd64 -o /usr/local/bin/hey
      - chmod +x /usr/local/bin/hey
    status:
      - '[ -f /usr/local/bin/hey ]'

  hey:windows:
    cmds:
      - curl https://hey-release.s3.us-east-2.amazonaws.com/hey_windows_amd64 -o C:/ProgramData/bin/hey.exe
    status:
      - '[ -f C:/ProgramData/bin/hey.exe ]'

  install:appimage:
    cmds:
      - gh release download -R '{{.PACKAGE}}' -p "*.AppImage"
      - mv *.AppImage /opt/{{.DEST_APPIMAGE_NAME}}.AppImage
      - chmod +x /opt/{{.DEST_APPIMAGE_NAME}}.AppImage
      - ln -s /opt/{{.DEST_APPIMAGE_NAME}}.AppImage /usr/local/bin/{{.DEST_APPIMAGE_NAME}}
    status:
      - '[ -f /opt/{{.DEST_APPIMAGE_NAME}}.AppImage ]'

  install:dmg:
    vars:
      LOG_SCRIPT:
        sh: echo "${PWD}/.config/log"
    cmds:
      - gh release download --clobber -R '{{.PACKAGE}}' -p "*64*.dmg"  -p "*.dmg"
      - |
        if (type sudo &> /dev/null && sudo -n true); then
          COUNT=0
          while read FILE; do
            COUNT=$((COUNT + 1))
            echo $COUNT
            if [ "$COUNT" == "1" ]; then
              hdiutil attach "$FILE" -nobrowse -mountpoint /tmp/{{.DMG_NAME}}
              cp -r /tmp/{{.DMG_NAME}}/*.app /Applications/
              hdiutil detach /tmp/{{.DMG_NAME}}
              rm "$FILE"
            else
              {{.LOG_SCRIPT}} warn 'Multiple releases have possibly been downloaded'
            fi
          done < <(find . -type f -iname "*.dmg")
        else
          {{.LOG_SCRIPT}} warn 'Run with sudo privileges to install DMG files'
        fi
    status:
      - '[ -d /Applications/{{.DMG_NAME}}.app ]'

  install:gh:
    deps:
      - :install:software:gh
    cmds:
      - task: install:gh:{{OS}}
        vars:
          BIN_NAME: '{{.BIN_NAME}}'
          PACKAGE: '{{.PACKAGE}}'
          TAR_OPTIONS: '{{.TAR_OPTIONS}}'
          ZIP_OPTIONS: '{{.ZIP_OPTIONS}}'

  install:gh:darwin:
    deps:
      - :install:software:gh
    vars:
      BIN_FOLDER:
        sh: echo "/usr/local/bin"
      LOG_SCRIPT:
        sh: echo "${PWD}/.config/log"
      PATTERNS:
        sh: |-
          if [[ {{ARCH}} == "arm" ]]; then
            echo "-p \"*Darwin_arm64\" -p \"*Darwin_arm64.tar.gz\" -p \"*darwin_arm64\" -p \"*darwin_arm64.tar.gz\" -p \"*darwin-arm64.tar.gz\" \
              -p \"*darwin-arm64\" -p \"*Darwin-64bit.tar.gz\" -p \"*darwin_x64.tar.gz\" -p \"*darwin.tar.gz\" -p \"*macOS-ARM64.tar.gz\" \
              -p \"*aarch64-apple-darwin.zip\" -p \"*-macos\" -p \"*darwin_arm64.zip\" -p \"*arm64-darwin.zip\" -p \"*darwin-amd64.zip\""
          else
            echo "-p \"*Darwin_x86_64\" -p \"*Darwin_x86_64.tar.gz\" -p \"*Darwin_amd64.tar.gz\" -p \"*darwin_amd64\" -p \"*darwin_amd64.tar.gz\" \
            -p \"*darwin-amd64.tar.gz\" -p \"*darwin-amd64\" -p \"*Darwin-64bit.tar.gz\" -p \"*darwin-x64\" -p \"*darwin_x64.tar.gz\" -p \"*MacOS_64-bit.tar.gz\" \
            -p \"*darwin-x86_64\" -p \"*darwin_x86_64.tar.gz\" -p \"*darwin.tar.gz\" -p \"*macOS-64bit.tar.gz\" -p \"*x86_64-apple-darwin.zip\" -p \"*-darwin.zip\" \
            -p \"*-macos\" -p \"*macOS_amd64.tar.gz\" -p \"*macOS_x86_64.tar.gz\" -p \"*darwin_amd64.zip\" -p \"*macos-x64.zip\" -p \"*x86_64-darwin.zip\" -p \"*darwin-arm64.zip\""
          fi
    env:
      TMP:
        sh: mktemp -d
    cmds:
      - gh release download -R "{{.PACKAGE}}" {{.PATTERNS}} -D "$TMP"
      - |
        cd "$TMP"
        COUNT=0
        while read FILE; do
          COUNT=$((COUNT + 1))
          if [ "$COUNT" == "1" ]; then
            if [[ "$FILE" == *.tar.gz ]]; then
              tar -xzf "$FILE"{{.TAR_OPTIONS}}
              rm "$FILE"
            elif [[ "$FILE" == *.zip ]]; then
              echo 'zip'
              unzip{{.ZIP_OPTIONS}} "$FILE"
              rm "$FILE"
            fi
          else
            {{.LOG_SCRIPT}} warn 'Multiple releases have possibly been downloaded'
          fi
        done < <(find . -type f)
        pwd
        ls -la
        rm -rf CHANGELOG.md LICENSE LICENSE* README.md autocomplete completion* *.1 *.sh *_autocomplete contrib Changes manpages
        mkdir -p {{.BIN_FOLDER}}
        if [ "$(ls 2>/dev/null * | wc -l | tr -d ' ')" == "1" ]; then
          {{.LOG_SCRIPT}} info 'Moving `{{.BIN_NAME}}` from its default location'
          mv * {{.BIN_FOLDER}}/{{.BIN_NAME}}
        else
          if [ -d bin ]; then
            cd bin
            if [ "$(ls 2>/dev/null * | wc -l | tr -d ' ')" == "1" ]; then
              {{.LOG_SCRIPT}} info 'Moving `{{.BIN_NAME}}` from the `bin` folder'
              mv * {{.BIN_FOLDER}}/{{.BIN_NAME}}
            else
              {{.LOG_SCRIPT}} warn 'Unmatched binary named `{{.BIN_NAME}}` from `*.tar.gz` with a `bin` folder'
              {{.COMMANDS}}
            fi
            cd ..
          elif [ "$(ls 2>/dev/null {{.BIN_NAME}} | wc -l | tr -d ' ')" == "1" ]; then
            {{.LOG_SCRIPT}} warn 'Moving `{{.BIN_NAME}}` from its default location, but, there are other files that are not moved'
            mv {{.BIN_NAME}} {{.BIN_FOLDER}}/{{.BIN_NAME}}
          else
            echo "$(ls 2>/dev/null {{.BIN_NAME}} | wc -l | tr -d ' ')"
            {{.LOG_SCRIPT}} warn 'Unmatched binary named `{{.BIN_NAME}}`'
            {{.COMMANDS}}
          fi
        fi
      - chmod +x {{.BIN_FOLDER}}/{{.BIN_NAME}}
    status:
      - '[ -f /usr/local/bin/{{.BIN_NAME}} ]'

  install:gh:linux:
    deps:
      - :install:software:gh
    vars:
      BIN_FOLDER:
        sh: echo "/usr/local/bin"
      LOG_SCRIPT:
        sh: echo "${PWD}/.config/log"
      PATTERNS: -p "*Linux_x86_64" -p "*Linux_x86_64.tar.gz" -p "*Linux_amd64.tar.gz" -p "*linux_amd64" -p "*linux_amd64.tar.gz" -p "*linux-amd64.tar.gz"
        -p "*linux-amd64" -p "*Linux-64bit.tar.gz" -p "*linux-x64" -p "*linux_x64.tar.gz" -p "*linux-x86_64" -p "*linux_x86_64.tar.gz" -p "*linux_amd64.zip"
        -p "*linux-x64.zip" -p "*linux-x64.tar.gz" -p "*x86_64-linux.zip" -p "*Linux_64-bit.tar.gz" -p "*linux-amd64.zip"
      PATTERNS_GNU: -p "*x86_64-unknown-linux-gnu.tar.gz" -p "*x86_64-unknown-linux-gnu.zip"
      PATTERNS_MUSL: -p "*x86_64-unknown-linux-musl.tar.gz"
    env:
      TMP:
        sh: mktemp -d
    cmds:
      - gh release download -R "{{.PACKAGE}}" {{.PATTERNS}} {{.PATTERNS_GNU}} -D "$TMP"
      - |
        cd "$TMP"
        COUNT=0
        while read FILE; do
          COUNT=$((COUNT + 1))
          if [ "$COUNT" == "1" ]; then
            if [[ "$FILE" == *.tar.gz ]]; then
              tar -xzf "$FILE"{{.TAR_OPTIONS}}
              rm "$FILE"
            elif [[ "$FILE" == *.zip ]]; then
              unzip{{.ZIP_OPTIONS}} "$FILE"
              rm "$FILE"
            fi
          else
            {{.LOG_SCRIPT}} warn 'Multiple releases have possibly been downloaded'
          fi
        done < <(find . -type f)
        ls -la
        rm -rf CHANGELOG.md LICENSE LICENSE* README.md autocomplete completion* *.1 *.sh *_autocomplete contrib Changes manpages
        mkdir -p {{.BIN_FOLDER}}
        if [ "$(ls 2>/dev/null * | wc -l)" == "1" ]; then
          {{.LOG_SCRIPT}} info 'Moving `{{.BIN_NAME}}` from its default location'
          if ! mv * {{.BIN_FOLDER}}/{{.BIN_NAME}}; then
            sudo mv * {{.BIN_FOLDER}}/{{.BIN_NAME}}
          fi
        else
          if [ -d bin ]; then
            cd bin
            if [ "$(ls 2>/dev/null * | wc -l)" == "1" ]; then
              {{.LOG_SCRIPT}} info 'Moving `{{.BIN_NAME}}` from the `bin` folder'
              if ! mv * {{.BIN_FOLDER}}/{{.BIN_NAME}}; then
                sudo mv * {{.BIN_FOLDER}}/{{.BIN_NAME}}
              fi
            else
              {{.LOG_SCRIPT}} warn 'Unmatched binary named `{{.BIN_NAME}}` from `*.tar.gz` with a `bin` folder'
              {{.COMMANDS}}
            fi
            cd ..
          elif [ "$(ls 2>/dev/null {{.BIN_NAME}} | wc -l)" == "1" ]; then
            {{.LOG_SCRIPT}} warn 'Moving `{{.BIN_NAME}}` from its default location, but, there are other files that are not moved'
            if ! mv {{.BIN_NAME}} {{.BIN_FOLDER}}/{{.BIN_NAME}}; then
              sudo mv {{.BIN_NAME}} {{.BIN_FOLDER}}/{{.BIN_NAME}}
            fi
          else
            {{.LOG_SCRIPT}} warn 'Unmatched binary named `{{.BIN_NAME}}`'
            {{.COMMANDS}}
          fi
        fi
      - chmod +x {{.BIN_FOLDER}}/{{.BIN_NAME}}
    status:
      - '[ -f {{.BIN_FOLDER}}/{{.BIN_NAME}} ]'

  install:gh:windows:
    deps:
      - :install:software:gh
    vars:
      BIN_FOLDER: C:/ProgramData/bin
      PATTERNS:
        -p "*x86_64-windows.zip" -p "*Windows_64-bit.tar.gz" -p "*windows-amd64*.zip" -p "*windows_amd64.zip" -p "*windows_amd64.tar.gz" -p "*windows-amd64*"
        -p "*win-x64.zip" -p "*Windows-64bit.zip" -p "*64*windows-msvc.zip" -p "*windows_x64.zip" -p "*Windows_x86_64.tar.gz" -p "*win-*.exe" -p "*setup*.exe" -p "*Setup*.exe"
        -p "*installer*64*.exe"
    env:
      TMPD:
        sh: powershell '$rnd=Get-Random; while(! (Test-Path $env:tmp/tmp.$rnd) -and ! ($created)){$ret=(md $env:tmp/tmp.$rnd -Force).FullName; $created=$true}$ret'
    cmds:
      - gh release download -R "{{.PACKAGE}}" {{.PATTERNS}} {{.EXE_PATTERN}} -D "$TMPD"
      - cd "$TMPD" && powershell '
        $COUNT = 0;
        $installer=$false;
        Get-ChildItem -File | %{
        $FILE = $_.Name;
        $COUNT = $COUNT + 1;
        if($COUNT -eq 1 ){
        if (("$FILE" -like "*.tar.gz") -or ("$FILE" -like "*.zip")){
        tar -xzf "$FILE"{{.TAR_OPTIONS}};
        Remove-Item "$FILE" -Force;
        }elseif((! $installer) -and (("$FILE" -like "*setup*64*.exe") -or ("$FILE" -like "*setup*.exe") -or ("$FILE" -like "*installer*64*.exe") -or ("$FILE" -like "*64*installer*.exe"))){
        $installer=$true;
        Start-Process "$FILE" /S -NoNewWindow -Wait -PassThru;
        Remove-Item "$FILE" -Force;
        }
        }else{
        Write-Host 'Multiple releases have possibly been downloaded' -ForegroundColor Yellow;
        }
        };
        Get-ChildItem;
        Remove-Item "CHANGELOG.md", "LICENSE", "LICENSE*", "README.md", "autocomplete", "completion*", "*_autocomplete", "contrib", "Changes" -EA SilentlyContinue -Force;
        mkdir {{.BIN_FOLDER}} -EA SilentlyContinue;
        pwd;
        if($installer){
        Write-Host '{{.BIN_NAME}} installed using the installer';
        }elseif((Get-ChildItem -File | Measure).Count -eq 1){
        Write-Host 'Moving {{.BIN_NAME}} from its default location';
        Move-Item -Force * {{.BIN_FOLDER}}/{{.BIN_NAME}}.exe;
        }else{
        if(Test-Path .\bin){
        cd bin;
        if((Get-ChildItem -File | Measure).Count -eq 1){
        Write-Host 'Moving {{.BIN_NAME}} from the bin folder';
        Move-Item -Force * {{.BIN_FOLDER}}/{{.BIN_NAME}}.exe;
        }else{
        Write-Host 'Unmatched binary named {{.BIN_NAME}} from *.tar.gz with a bin folder' -ForegroundColor Yellow;
        }
        cd ..;
        }elseif((Get-ChildItem -File {{.BIN_NAME}}* -ea SilentlyContinue | Measure).Count -gt 0){
        Write-Host 'Moving {{.BIN_NAME}} from its default location, but, there are other files that are not moved' -ForeGroundColor Yellow;
        Move-Item -Force {{.BIN_NAME}}*.exe {{.BIN_FOLDER}}/;
        }else{
        Write-Host 'Unmatched binary named {{.BIN_NAME}}' -ForegroundColor Yellow;
        }
        }'
    status:
      - '[ -f {{.BIN_FOLDER}}/{{.BIN_NAME}}.exe ]'

  jitsi-meet-electron:
    cmds:
      - task: jitsi-meet-electron:{{OS}}
    status:
      - '[ -f /usr/local/bin/jitsi-meet-electron ]'

  jitsi-meet-electron:darwin:
    cmds:
      - task: install:dmg
        vars:
          DMG_NAME: jitsi-meet
          PACKAGE: jitsi/jitsi-meet-electron

  jitsi-meet-electron:linux:
    cmds:
      - gh release download -R jitsi/jitsi-meet-electron -p "*.AppImage"
      - mv *.AppImage /opt/jitsi-meet.AppImage
      - chmod +x /opt/jitsi-meet.AppImage
      - ln -s /opt/jitsi-meet.AppImage /usr/local/bin/jitsi-meet-electron

  jitsi-meet-electron:windows:
    cmds:
      - task: install:gh
        vars:
          BIN_NAME: jitsi-meet
          PACKAGE: jitsi/jitsi-meet-electron

  jq:
    cmds:
      - task: jq:{{OS}}
    status:
      - '[ -f /usr/local/bin/jq ]'

  jq:darwin:
    cmds:
      - gh release download -R stedolan/jq -p "jq-osx-amd64"
      - mkdir -p /usr/local/bin && mv jq-osx-amd64 /usr/local/bin/jq
      - chmod +x /usr/local/bin/jq

  jq:linux:
    cmds:
      - gh release download -R stedolan/jq -p "jq-linux64"
      - mkdir -p /usr/local/bin && mv jq-linux64 /usr/local/bin/jq
      - chmod +x /usr/local/bin/jq

  jq:windows:
    cmds:
      - gh release download -R stedolan/jq -p "jq-win64.exe"
      - powershell 'md C:/ProgramData/bin -Force; Move-Item jq-win64.exe C:/ProgramData/bin/jq.exe -Force'

  kubectx:
    cmds:
      - task: install:gh
        vars:
          BIN_NAME: kubectx
          PACKAGE: ahmetb/kubectx

  kubenav:
    cmds:
      - task: kubenav:{{OS}}

  kubenav:darwin:
    vars:
      PATTERNS:
        sh: |-
          if [[ {{ARCH}} == "arm" ]]; then
            echo "-p \"*darwin-arm64.zip\""
          else
            echo "-p \"*darwin-amd64.zip\""
          fi
    env:
      TMP:
        sh: mktemp -d
    cmds:
      - gh release download -R kubenav/kubenav {{.PATTERNS}} -D "$TMP"
      - |
        cd "$TMP"
        FILE=$(find . -type f -iname *.zip)
        unzip -q "$FILE"
        rm "$FILE"
        mv kubenav.app /Applications/
    status:
      - '[ -d /Applications/kubenav.app ]'

  kubenav:linux:
    vars:
      PATTERNS:
        sh: |-
          if [[ {{ARCH}} == "arm" ]]; then
            echo "-p \"*linux-arm64.zip\""
          else
            echo "-p \"*linux-amd64.zip\""
          fi
    env:
      TMP:
        sh: mktemp -d
    cmds:
      - gh release download -R kubenav/kubenav {{.PATTERNS}} -D "$TMP"
      - |
        cd "$TMP"
        FILE=$(find . -type f -iname *.zip)
        unzip -q "$FILE"
        rm "$FILE"
        mv kubenav /usr/local/bin
        chmod +x /usr/local/bin/kubenav
    status:
      - '[ -f /usr/local/bin/kubenav ]'

  kubenav:windows:
    cmds:
      - gh release download -R kubenav/kubenav -p "*windows-amd64.zip"
      - tar -xf *windows-amd64.zip
      - cmd /C move kubenav.exe C:/ProgramData/bin/kubenav.exe
      - cmd /C del /Q *windows-amd64.zip
    status:
      - '[ -f C:/ProgramData/bin/kubenav.exe ]'

  linuxkit:
    cmds:
      - task: install:gh
        vars:
          BIN_NAME: linuxkit
          PACKAGE: linuxkit/linuxkit

  manta:
    cmds:
      - task: manta:{{OS}}

  manta:darwin:
    cmds:
      - task: install:dmg
        vars:
          DMG_NAME: Manta
          PACKAGE: hql287/manta

  manta:linux:
    cmds:
      - task: install:appimage
        vars:
          DEST_APPIMAGE_NAME: Manta
          PACKAGE: hql287/manta

  manta:windows:
    cmds:
      - task: install:gh
        vars:
          BIN_NAME: Manta
          EXE_PATTERN: '*Setup*.exe'

          PACKAGE: hql287/manta
  mark-text:
    cmds:
      - task: marktext:{{OS}}

  marktext:darwin:
    cmds:
      - echo 'Install Marktext using Homebrew.'

  marktext:linux:
    cmds:
      - task: install:appimage
        vars:
          DEST_APPIMAGE_NAME: Marktext
          PACKAGE: marktext/marktext

  marktext:windows:
    cmds:
      - task: install:gh
        vars:
          BIN_NAME: marktext
          PACKAGE: marktext/marktext

  masscode:
    cmds:
      - task: masscode:{{OS}}
    status:
      - '[ -f /usr/local/bin/masscode ]'

  masscode:darwin:
    cmds:
      - echo 'Install MassCode using Homebrew on MacOS.'

  masscode:linux:
    cmds:
      - task: install:appimage
        vars:
          DEST_APPIMAGE_NAME: MassCode
          PACKAGE: antonreshetov/masscode

  masscode:windows:
    cmds:
      - task: install:gh
        vars:
          BIN_NAME: masscode
          EXE_PATTERN: '*Setup*.exe'

          PACKAGE: antonreshetov/masscode
  mergestat:
    cmds:
      - task: install:gh
        vars:
          BIN_NAME: mergestat
          PACKAGE: mergestat/mergestat

  mkcert:
    cmds:
      - task: install:gh
        vars:
          BIN_NAME: mkcert
          PACKAGE: FiloSottile/mkcert

  motrix:
    cmds:
      - task: motrix:{{OS}}

  motrix:darwin:
    cmds:
      - task: install:dmg
        vars:
          DMG_NAME: Motrix
          PACKAGE: agalwood/motrix

  motrix:linux:
    cmds:
      - task: install:appimage
        vars:
          DEST_APPIMAGE_NAME: Motrix
          PACKAGE: agalwood/motrix

  motrix:windows:
    cmds:
      - task: install:gh
        vars:
          BIN_NAME: Motrix
          PACKAGE: agalwood/motrix

  mqttx:
    cmds:
      - task: install:gh
        vars:
          BIN_NAME: mqttx
          EXE_PATTERN: '*Setup*x64.exe'

          PACKAGE: agalwood/mqttx
  nebula:
    cmds:
      - task: install:gh
        vars:
          BIN_NAME: nebula
          PACKAGE: slackhq/nebula

  nfpm:
    cmds:
      - task: install:gh
        vars:
          BIN_NAME: nfpm
          PACKAGE: goreleaser/nfpm

  oq:
    cmds:
      - task: install:gh
        vars:
          BIN_NAME: oq
          PACKAGE: Blacksmoke16/oq

  osquery:
    cmds:
      - task: osquery:{{OS}}

  osquery:darwin:
    cmds:
      - task: install:gh
        vars:
          BIN_NAME: osquery
          PACKAGE: osquery/osquery

  osquery:linux:
    env:
      TMP:
        sh: mktemp -d
    cmds:
      - gh release download -R osquery/osquery -p "*linux_x86_64.tar.gz" -D "$TMP"
      - |
        cd "$TMP"
        FILE=$(find . -type f)
        tar -xzf "$FILE"
        rm "$FILE"
        cp -r * /
    status:
      - '[ -f /usr/bin/osqueryd]'

  osquery:windows:
    cmds:
      - echo 'osquery is not available for Windows.'

  peco:
    cmds:
      - task: install:gh
        vars:
          BIN_NAME: peco
          PACKAGE: peco/peco
          TAR_OPTIONS: ' --strip-components 1'
          ZIP_OPTIONS: ' -j'

  pretzel:
    cmds:
      - task: pretzel:{{OS}}

  pretzel:darwin:
    cmds:
      - task: install:dmg
        vars:
          DMG_NAME: Pretzel
          PACKAGE: amiechen/pretzel

  pretzel:linux:
    cmds:
      - echo 'Pretzel is not available for Linux'

  pretzel:windows:
    cmds:
      - echo 'Pretzel is not available for Windows'

  q:
    cmds:
      - task: install:gh
        vars:
          BIN_NAME: q
          PACKAGE: harelba/q

  raindrop:
    cmds:
      - task: raindrop:{{OS}}

  raindrop:darwin:
    cmds:
      - task: install:dmg
        vars:
          DMG_NAME: raindrop
          PACKAGE: raindropio/raindrop

  raindrop:linux:
    cmds:
      - echo 'Raindrop is not available for Linux'

  raindrop:windows:
    cmds:
      - task: install:gh
        vars:
          BIN_NAME: raindrop
          EXE_PATTERN: '*Installer.exe'

          PACKAGE: raindropio/raindrop
  responsively:
    cmds:
      - task: responsively:{{OS}}

  responsively:darwin:
    cmds:
      - task: install:dmg
        vars:
          DMG_NAME: ResponsivelyApp
          PACKAGE: responsively-org/responsively-app

  responsively:linux:
    cmds:
      - task: install:appimage
        vars:
          DEST_APPIMAGE_NAME: Responsively
          PACKAGE: responsively-org/responsively-app

  responsively:windows:
    cmds:
      - task: install:gh
        vars:
          BIN_NAME: ResponsivelyApp
          PACKAGE: responsively-org/responsively-app

  runjs:
    cmds:
      - task: runjs:{{OS}}

  runjs:darwin:
    cmds:
      - task: install:dmg
        vars:
          DMG_NAME: runjs
          PACKAGE: lukehaas/RunJS

  runjs:linux:
    cmds:
      - task: install:appimage
        vars:
          DEST_APPIMAGE_NAME: RunJS
          PACKAGE: lukehaas/RunJS

  runjs:windows:
    cmds:
      - task: install:gh
        vars:
          BIN_NAME: runjs
          PACKAGE: lukehaas/RunJS

  shfmt:
    cmds:
      - task: install:gh
        vars:
          BIN_NAME: shfmt
          PACKAGE: mvdan/sh

  ssl-proxy:
    cmds:
      - task: install:gh
        vars:
          BIN_NAME: ssl-proxy
          PACKAGE: suyashkumar/ssl-proxy

  switchhosts:
    cmds:
      - task: switchhosts:{{OS}}

  switchhosts:darwin:
    cmds:
      - task: install:dmg
        vars:
          DMG_NAME: SwitchHosts
          PACKAGE: oldj/SwitchHosts

  switchhosts:linux:
    cmds:
      - task: install:appimage
        vars:
          DEST_APPIMAGE_NAME: SwitchHosts
          PACKAGE: oldj/SwitchHosts

  switchhosts:windows:
    cmds:
      - task: install:gh
        vars:
          BIN_NAME: SwitchHosts
          PACKAGE: oldj/SwitchHosts

  tabby:
    cmds:
      - task: tabby:{{OS}}

  tabby:darwin:
    cmds:
      - task: install:dmg
        vars:
          DMG_NAME: tabby
          PACKAGE: Eugeny/tabby

  tabby:linux:
    env:
      TMP:
        sh: mktemp -d
    cmds:
      - gh release download -R Eugeny/tabby -p "*linux-x64.tar.gz" -D "$TMP"
      - |
        cd "$TMP"
        FILE=$(find . -type f)
        mkdir Tabby
        tar -xzf "$FILE" -C Tabby --strip-components 1
        rm "$FILE"
        mv Tabby /opt/
        ln -s /opt/Tabby/tabby /usr/local/bin/tabby
    status:
      - '[ -f /usr/local/bin/tabby]'

  tabby:windows:
    cmds:
      - task: install:gh
        vars:
          BIN_NAME: tabby
          PACKAGE: jackd248/tabby

  task:
    cmds:
      - task: install:gh
        vars:
          BIN_NAME: task
          PACKAGE: go-task/task

  temps:
    cmds:
      - task: temps:{{OS}}

  temps:darwin:
    env:
      TMP:
        sh: mktemp -d
    cmds:
      - gh release download -R jackd248/temps -p "*darwin-x64.zip" -D "$TMP"
      - |
        cd "$TMP"
        unzip -q "Temps-darwin-x64.zip"
        rm "Temps-darwin-x64.zip"
        mv Temps-darwin-x64/*.app /Applications/
    status:
      - '[ -d /Applications/Temps.app ]'

  temps:linux:
    env:
      TMP:
        sh: mktemp -d
    cmds:
      - gh release download -R jackd248/temps -p "*linux-x64.zip" -D "$TMP"
      - |
        cd "$TMP"
        unzip -q "Temps-linux-x64.zip"
        rm "Temps-linux-x64.zip"
        mv Temps-linux-x64 /opt/
        ln -s /opt/Temps-linux-x64/Temps /usr/local/bin/Temps
    status:
      - '[ -f /usr/local/bin/Temps]'

  temps:windows:
    cmds:
      - echo 'Temps does not work on Windows'

  tokei:
    cmds:
      - task: install:gh
        vars:
          BIN_NAME: tokei
          PACKAGE: XAMPPRocky/tokei

  transfer:
    cmds:
      - task: install:gh
        vars:
          BIN_NAME: transfer
          PACKAGE: rinetd/transfer

  trivy:
    cmds:
      - task: install:gh
        vars:
          BIN_NAME: trivy
          PACKAGE: aquasecurity/trivy

  udemy-downloader-gui:
    cmds:
      - task: udemy-downloader-gui:{{OS}}

  udemy-downloader-gui:darwin:
    cmds:
      - task: install:dmg
        vars:
          DMG_NAME: Udeler
          PACKAGE: FaisalUmair/udemy-downloader-gui

  udemy-downloader-gui:linux:
    cmds:
      - task: install:appimage
        vars:
          DEST_APPIMAGE_NAME: UdemyDownloadedGUI
          PACKAGE: FaisalUmair/udemy-downloader-gui

  udemy-downloader-gui:windows:
    cmds:
      - task: install:gh
        vars:
          BIN_NAME: udemy-downloader-gui
          PACKAGE: FaisalUmair/udemy-downloader-gui

  up:
    cmds:
      - task: up:{{OS}}
    status:
      - '[ -f /usr/local/bin/up ]'

  up:darwin:
    cmds:
      - gh release download -R akavel/up -p "up-darwin"
      - mkdir -p /usr/local/bin && mv up-darwin /usr/local/bin/up
      - chmod +x /usr/local/bin/up

  up:linux:
    cmds:
      - gh release download -R akavel/up -p "up"
      - mkdir -p /usr/local/bin && mv up /usr/local/bin/up
      - chmod +x /usr/local/bin/up

  up:windows:
    cmds:
      - echo 'up is not available for Windows from GitHub Releases'

  ventoy:
    cmds:
      - task: ventoy:{{OS}}
    status:
      - '[ -f /opt/ventoy/VentoyGUI.x86_64 ]'

  ventoy:darwin:
    cmds:
      - echo "Ventoy is not available for MacOS"

  ventoy:linux:
    cmds:
      - gh release download -R ventoy/Ventoy -p "*linux.tar.gz"
      - ls *linux.tar.gz | xargs tar -xvf
      - rm -f *.tar.gz
      - mkdir -p /opt/ventoy && mv ./ventoy-* /opt/ventoy/

  ventoy:windows:
    cmds:
      - gh release download -R ventoy/ventoy -p "ventoy_windows_amd64.exe"
      - powershell 'md C:/ProgramData/bin -Force; Move-Item ventoy_windows_amd64.exe C:/ProgramData/bin/ventoy.exe -Force'

  webtorrent:
    cmds:
      - task: webtorrent:{{OS}}

  webtorrent:darwin:
    cmds:
      - task: install:dmg
        vars:
          DMG_NAME: WebTorrent
          PACKAGE: webtorrent/webtorrent-desktop

  webtorrent:linux:
    env:
      TMP:
        sh: mktemp -d
    cmds:
      - gh release download -R webtorrent/webtorrent-desktop -p "*linux-x64.zip" -D "$TMP"
      - |
        cd "$TMP"
        FILE=$(find . -type f)
        unzip -q $FILE
        rm "$FILE"
        mv WebTorrent-linux-x64 /opt/
        ln -s /opt/WebTorrent-linux-x64/WebTorrent /usr/local/bin/webtorrent
    status:
      - '[ -f /usr/local/bin/webtorrent]'

  webtorrent:windows:
    cmds:
      - task: install:gh
        vars:
          BIN_NAME: WebTorrent
          PACKAGE: webtorrent/webtorrent-desktop

  whaler:
    cmds:
      - task: whaler:{{OS}}
    status:
      - '[ -f /usr/local/bin/whaler ]'

  whaler:darwin:
    cmds:
      - gh release download -R P3GLEG/whaler -p "Whaler_MacOS"
      - mkdir -p /usr/local/bin && mv Whaler_MacOS /usr/local/bin/whaler
      - chmod +x /usr/local/bin/whaler

  whaler:linux:
    cmds:
      - gh release download -R P3GLEG/whaler -p "Whaler_linux_amd64"
      - mkdir -p /usr/local/bin && mv Whaler_linux_amd64 /usr/local/bin/whaler
      - chmod +x /usr/local/bin/whaler

  whaler:windows:
    cmds:
      - gh release download -R P3GLEG/whaler -p "Whaler_windows.exe"
      - powershell 'md C:/ProgramData/bin -Force; Move-Item Whaler_windows.exe C:/ProgramData/bin/whaler.exe -Force'

  yq:
    cmds:
      - task: yq:{{OS}}
    status:
      - '[ -f /usr/local/bin/yq ]'

  yq:darwin:
    cmds:
      - gh release download -R mikefarah/yq -p "yq_darwin_amd64"
      - mkdir -p /usr/local/bin && mv yq_darwin_amd64 /usr/local/bin/yq
      - chmod +x /usr/local/bin/yq

  yq:linux:
    cmds:
      - gh release download -R mikefarah/yq -p "yq_linux_amd64"
      - mkdir -p /usr/local/bin && mv yq_linux_amd64 /usr/local/bin/yq
      - chmod +x /usr/local/bin/yq

  yq:windows:
    cmds:
      - gh release download -R mikefarah/yq -p "yq_windows_amd64.exe"
      - powershell 'md C:/ProgramData/bin -Force; Move-Item yq_windows_amd64.exe C:/ProgramData/bin/yq.exe -Force'
