---
version: '3'

tasks:
  up:
    deps:
      - :install:software:jq
      - :install:software:vagrant
    desc: Select which virtualization platform to run the image on using an interactive prompt
    vars:
      PROMPT_OPTIONS:
        sh: |
          TMP="$(mktemp)"
          if type qemu-system-x86_64 &> /dev/null; then
            echo 'KVM' > "$TMP"
          fi
          if [[ '{{OS}}' == 'darwin' ]] && mdfind -name 'Parallels Desktop.app' &> /dev/null; then
            echo 'Parallels' > "$TMP"
          fi
          if type vboxmanage &> /dev/null; then
            echo 'VirtualBox' > "$TMP"
          fi
          if [[ '{{OS}}' == 'linux' ]] && type vmware &> /dev/null; then
            echo 'VMWare Workstation' > "$TMP"
          fi
          if [[ '{{OS}}' == 'darwin' ]] && type vmrun &> /dev/null; then
            echo 'VMWare Fusion' > "$TMP"
          fi
          LIST_LENGTH="$(jq -R -s -c -r 'split("\n") | length' < "$TMP")"
          if [ "$LIST_LENGTH" != '0' ]; then
            echo "\""$(jq -R -s -c -r 'split("\n") | join("\" \"")' < "$TMP")"\""
          else
            echo "None"
          fi
    cmds:
      - |
        if [[ '{{.PROMPT_OPTIONS' == 'None' ]]; then
          .config/log error 'No virtualization platforms installed. Install a platform (e.g. VirtualBox, VMWare, QEMU) to continue.' && exit 1
        else
          .config/log prompt 'Which desktop OS would you like to launch?'
          .config/log info 'Select "Other" to launch any OS from VagrantUp.com (TODO Implement feature)'
          OS_CHOICE="$(.config/log choose 'ArchLinux' 'CentOS' 'Debian' 'Fedora' 'macOS' 'Ubuntu' 'Windows' 'Other')"
          .config/log prompt 'Which virtualization platform would you like to use?'
          PLATFORM_CHOICE="$(.config/log choose '{{.PROMPT_OPTIONS}}')"
          if [[ "$PLATFORM_CHOICE" == 'KVM' ]]; then
            VAGRANT_UP_PROVIDER="libvirt"
          elif [[ "$PLATFORM_CHOICE" == 'Parallels' ]]; then
            VAGRANT_UP_PROVIDER="parallels"
          elif [[ "$PLATFORM_CHOICE" == 'VirtualBox' ]]; then
            VAGRANT_UP_PROVIDER="virtualbox"
          elif [[ "$PLATFORM_CHOICE" == 'VMWare Workstation' ]]; then
            VAGRANT_UP_PROVIDER="vmware_workstation"
          elif [[ "$PLATFORM_CHOICE" == 'VMWare Fusion' ]]; then
            VAGRANT_UP_PROVIDER="vmware_fusion"
          else
            .config/log error 'Unrecognized platform selection.'
          fi
          .config/log warn 'TODO Unfinished task - need to add in OS_CHOICE to vagrant up'
          vagrant up --provider="$VAGRANT_UP_PROVIDER"
        fi
