---
- hosts: localhost
  vars:
    xcode_version: "15"
    ruby_version: "3.2.3"
    nvm_version: "0.38.0"
    node_version: "21.4.0"
    java_version: "17"
    bundler_version: "2.3.25"
    jenkins_user: "jenkinsagent"
  tasks:
    #- name: Download Xcode. # This is not working because of the authentication + MFA is required for the developer portal
    #- Download it manually and place it in /tmp directory
    #  get_url:
    #    url: https://developer.apple.com/services-account/download?path=/Developer_Tools/Xcode_15/Xcode_{{ xcode_version }}.xip
    #    dest: /tmp/Xcode_{{ xcode_version }}.xip

    - name: Unzip Xcode
      command: open /tmp/Xcode_{{ xcode_version }}.xip

    - name: Wait for Xcode to unzip
      wait_for:
        path: /tmp/Xcode.app
        state: present
        timeout: 900

    - name: Install Xcode
      command: cp -R /tmp/Xcode.app /Applications/

    - name: Select Xcode
      become: yes
      command: xcode-select --switch /Applications/Xcode.app/Contents/Developer

    - name: Accept Xcode license
      become: yes
      command: |
        xcodebuild -license accept

    - name: Verify Xcode selection
      command: xcode-select -p
      register: xcode_path
      changed_when: false

    - debug:
        var: xcode_path.stdout

    - name: Install RVM
      shell: |
        \curl -sSL https://get.rvm.io | bash -s stable
        echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"' >> /Users/{{ jenkins_user }}/.bash_profile
        source /Users/{{ jenkins_user }}/.rvm/scripts/rvm
      args:
        executable: /bin/bash

    - name: RVM USE
      shell: |
        source /Users/{{ jenkins_user }}/.rvm/scripts/rvm
        rvm reinstall {{ ruby_version }} --with-openssl-dir=$(brew --prefix openssl) --with-readline-dir=$(brew --prefix readline) --with-libyaml-dir=$(brew --prefix libyaml) --disable-dtrace --disable-docs
        rvm use {{ ruby_version }} --default
      args:
        executable: /bin/bash

    - name: Install NVM and export path
      shell: |
        curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v{{ nvm_version }}/install.sh | bash
        echo 'export NVM_DIR="$HOME/.nvm"' >> /Users/{{ jenkins_user }}/.bash_profile
        echo '[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"' >> /Users/{{ jenkins_user }}/.bash_profile
        source /Users/{{ jenkins_user }}/.bash_profile

    - name: NVM USE Node version
      shell: |
        source /Users/{{ jenkins_user }}/.bash_profile
        nvm install {{ node_version }}
        nvm use {{ node_version }} --default

    - name: Install Appium via NPM
      npm:
        name: appium
        global: yes

    - name: Install Fastlane
      gem:
        name: fastlane
        user_install: yes

    - name: Download Amazon Corretto 17. Run the installer manually in the tmp directory
      get_url:
        url: https://corretto.aws/downloads/latest/amazon-corretto-{{ java_version }}-x64-macos-jdk.pkg
        dest: /tmp/amazon-corretto-{{ java_version }}-x64-macos-jdk.pkg

    - name: Install Bundler
      gem:
        name: bundler
        version: "{{ bundler_version }}"
        user_install: yes

    - name: Install Sentry CLI
      homebrew:
        name: sentry-cli
        state: latest
