# -*- mode: yaml; indent-tabs-mode: nil; tab-width: 2; coding: utf-8-unix -*-

---

- name: "toxcore users.yml LOOP_USER_F[0]"
  debug:
    verbosity: 1
    msg: "toxcore users.yml LOOP_USER_F={{LOOP_USER_F[0]}}"

- block:

# https://stackoverflow.com/questions/13114268/passing-ciphers-to-libcurl-through-git
# https://git-scm.com/docs/git-config#git-config-httpsslCipherList
- name: ~/.gitconfig TEST_SSL_GIT_CIPHERLIST
  ini_file:
    path: "{{ item.dest }}"
    create: yes
    section: "http"
    option: "sslCipherList"
    value: "{{ TEST_SSL_GIT_CIPHERLIST|join(' ') }}"
  when:
    - TEST_SSL_GIT_CIPHERLIST|default([])|length > 0
  with_items:
    - {dest: "{{USER_HOME}}/.gitconfig", owner: "{{ LOOP_USER }}", group: "{{ BOX_ALSO_GROUP }}" }

# https://stackoverflow.com/questions/48938019/git-pull-push-unable-to-access-https-ssl-routines-seem-to-be-down
# git config http.sslVersion tlsv1.2
- name: ~/.gitconfig TEST_SSL_GIT_SSLVERSION
  ini_file:
    path: "{{ item.dest }}"
    create: yes
    section: "http"
    option: "sslVersion"
    value: "{{ TEST_SSL_GIT_SSLVERSION }}"
  when:
    - TEST_SSL_GIT_SSLVERSION|default('') != ''
  with_items:
    - { dest: "{{USER_HOME}}/.gitconfig", owner: "{{ LOOP_USER }}", group: "{{ BOX_ALSO_GROUP }}" }

  # https://stackoverflow.com/questions/21181231/server-certificate-verification-failed-cafile-etc-ssl-certs-ca-certificates-c
  - name: ~/.gitconfig with proxy http
    blockinfile:
      dest: "{{ item.dest|expanduser }}"
      owner: "{{ item.owner }}"
      create: yes
      state: "{{ 'present' if HTTP_PROXY != '' else 'absent' }}"
      marker: "# {mark} ANSIBLE MANAGED BLOCK proxy http"
      block: |
        [http]
          # https sic
          proxy = http://{{HTTPS_PROXYHOST}}:{{HTTPS_PROXYPORT}}
          sslCAinfo = /usr/local/etc/ssl/cacert-testforge.pem
          # http sic
          sslverify = true
        # sslversion = {{"tlsv1.3" if BOX_TLS_VERSION == "1.3" else "tlsv1.2"}}

    when:
      - HTTPS_PROXYHOST != '' and HTTPS_PROXYPORT != ''
    with_items:
      - dest: "~{{LOOP_USER}}/.gitconfig"
        owner: "{{ LOOP_USER }}"

  - name: git config --global http.sslverify "true"
    shell: |
      git config --global --list | grep -q http.sslverify.true || \
          git config --global http.sslverify "true"
      git config --global --list | grep -q https.sslverify.true || \
          git config --global https.sslverify "true"          
      exit 0

  - name: git config --global http.sslversion tlsv1.3
    shell: |
      # GIT_CURL_VERBOSE=1
      git config --global --list | \
          grep -q http.sslversion.tlsv1.{{BOX_TLS_VERSION}} || \
          git config --global http.sslversion tlsv1.{{BOX_TLS_VERSION}}
      exit 0
    when:
        - BOX_TLS_VERSION == "1.3"
        - false

  - name: ~/.gitconfig with proxy https
    blockinfile:
      dest: "{{ item.dest|expanduser }}"
      owner: "{{ item.owner }}"
      create: yes
      state: "{{ 'present' if HTTPS_PROXY != '' else 'absent' }}"
      marker: "# {mark} ANSIBLE MANAGED BLOCK proxy https"
      block: |
        [https]
          proxy = http://{{HTTPS_PROXYHOST}}:{{HTTPS_PROXYPORT}}
          sslCAinfo = {{PLAY_CA_CERT}}
          # http sic
          sslverify = true
          sslversion = {{"tlsv1.3" if BOX_TLS_VERSION == "1.3" else "tlsv1.2"}}
    when:
      - true
    with_items:
      - dest: "~{{LOOP_USER}}/.gitconfig"
        owner: "{{ LOOP_USER }}"

  - name: ~/.gitconfig with proxy core
    blockinfile:
      dest: "{{ item.dest|expanduser }}"
      owner: "{{ item.owner }}"
      create: yes
      state: "{{ 'present' if HTTP_PROXY != '' else 'absent' }}"
      marker: "# {mark} ANSIBLE MANAGED BLOCK proxy core"
      block: |
        [core]
          gitproxy = /usr/local/bin/gitproxy.bash
    when:
      - true
    with_items:
      - dest: "~{{LOOP_USER}}/.gitconfig"
        owner: "{{ LOOP_USER }}"

  - name: ~/.gitconfig with proxy socks
    blockinfile:
      dest: "{{ item.dest|expanduser }}"
      owner: "{{ item.owner }}"
      create: yes
      state: "{{ 'present' if SOCKS_PROXY != '' else 'absent' }}"
      marker: "# {mark} ANSIBLE MANAGED BLOCK proxy socks"
      block: |
        [socks]
          proxy = {{SOCKS_PROXYTYPE}}://{{SOCKS_PROXYHOST}}:{{SOCKS_PROXYPORT}}
    when:
      - true
    with_items:
      - dest: "~{{LOOP_USER}}/.gitconfig"
        owner: "{{ LOOP_USER }}"

  when:
    - "LOOP_USER != 'portage'"

- block:

  - name: /etc/sysconfig/docker
    blockinfile:
      dest: /etc/sysconfig/docker
      create: yes
      mode:  0644
      marker: "# {mark} ANSIBLE MANAGED BLOCK proxy proxy_post.yml"
      state: "{{ 'present' if HTTP_PROXYHOST != '' else 'absent' }}"
      block: |
        HTTP_PROXY={{HTTP_PROXYTYPE}}://{{HTTP_PROXYHOST}}:{{HTTP_PROXYPORT}}
        HTTPS_PROXY={{HTTPS_PROXYTYPE}}://{{HTTPS_PROXYHOST}}:{{HTTPS_PROXYPORT}}

  when:
    - "'docker' in HOSTVMS_FEATURES|default([])"

- name: Run c-toxcore ctest on the tester
  delegate_to: localhost
  shell: |
    [ -d {{TOXCORE_LOCAL_SRC}}/c-toxcore/_build ] || exit 0
    cd {{TOXCORE_LOCAL_SRC}}/c-toxcore/_build || exit 1
    ctest
    exit 0
# {{TOXCORE_LOCAL_SRC}}/c-toxcore/_build/auto_tests/auto_TCP_test
# {{TOXCORE_LOCAL_SRC}}/c-toxcore/_build/auto_tests/auto_announce_test
# {{TOXCORE_LOCAL_SRC}}/c-toxcore/_build/auto_tests/auto_bootstrap_test
# {{TOXCORE_LOCAL_SRC}}/c-toxcore/_build/auto_tests/auto_conference_av_test
# {{TOXCORE_LOCAL_SRC}}/c-toxcore/_build/auto_tests/auto_conference_double_invite_test
# {{TOXCORE_LOCAL_SRC}}/c-toxcore/_build/auto_tests/auto_conference_invite_merge_test
# {{TOXCORE_LOCAL_SRC}}/c-toxcore/_build/auto_tests/auto_conference_peer_nick_test
# {{TOXCORE_LOCAL_SRC}}/c-toxcore/_build/auto_tests/auto_conference_simple_test
# {{TOXCORE_LOCAL_SRC}}/c-toxcore/_build/auto_tests/auto_conference_test
# {{TOXCORE_LOCAL_SRC}}/c-toxcore/_build/auto_tests/auto_conference_two_test
# {{TOXCORE_LOCAL_SRC}}/c-toxcore/_build/auto_tests/auto_crypto_test
# {{TOXCORE_LOCAL_SRC}}/c-toxcore/_build/auto_tests/auto_dht_getnodes_api_test
# {{TOXCORE_LOCAL_SRC}}/c-toxcore/_build/auto_tests/auto_encryptsave_test
# {{TOXCORE_LOCAL_SRC}}/c-toxcore/_build/auto_tests/auto_file_saving_test
# {{TOXCORE_LOCAL_SRC}}/c-toxcore/_build/auto_tests/auto_file_transfer_test
# {{TOXCORE_LOCAL_SRC}}/c-toxcore/_build/auto_tests/auto_forwarding_test
# {{TOXCORE_LOCAL_SRC}}/c-toxcore/_build/auto_tests/auto_friend_connection_test
# {{TOXCORE_LOCAL_SRC}}/c-toxcore/_build/auto_tests/auto_friend_request_spam_test
# {{TOXCORE_LOCAL_SRC}}/c-toxcore/_build/auto_tests/auto_friend_request_test
# {{TOXCORE_LOCAL_SRC}}/c-toxcore/_build/auto_tests/auto_group_general_test
# {{TOXCORE_LOCAL_SRC}}/c-toxcore/_build/auto_tests/auto_group_invite_test
# {{TOXCORE_LOCAL_SRC}}/c-toxcore/_build/auto_tests/auto_group_message_test
# {{TOXCORE_LOCAL_SRC}}/c-toxcore/_build/auto_tests/auto_group_moderation_test
# {{TOXCORE_LOCAL_SRC}}/c-toxcore/_build/auto_tests/auto_group_save_test
# {{TOXCORE_LOCAL_SRC}}/c-toxcore/_build/auto_tests/auto_group_state_test
# {{TOXCORE_LOCAL_SRC}}/c-toxcore/_build/auto_tests/auto_group_sync_test
# {{TOXCORE_LOCAL_SRC}}/c-toxcore/_build/auto_tests/auto_group_tcp_test
# {{TOXCORE_LOCAL_SRC}}/c-toxcore/_build/auto_tests/auto_group_topic_test
# {{TOXCORE_LOCAL_SRC}}/c-toxcore/_build/auto_tests/auto_invalid_tcp_proxy_test
# {{TOXCORE_LOCAL_SRC}}/c-toxcore/_build/auto_tests/auto_invalid_udp_proxy_test
# {{TOXCORE_LOCAL_SRC}}/c-toxcore/_build/auto_tests/auto_lan_discovery_test
# {{TOXCORE_LOCAL_SRC}}/c-toxcore/_build/auto_tests/auto_lossless_packet_test
# {{TOXCORE_LOCAL_SRC}}/c-toxcore/_build/auto_tests/auto_lossy_packet_test
# {{TOXCORE_LOCAL_SRC}}/c-toxcore/_build/auto_tests/auto_network_test
# {{TOXCORE_LOCAL_SRC}}/c-toxcore/_build/auto_tests/auto_onion_test
# {{TOXCORE_LOCAL_SRC}}/c-toxcore/_build/auto_tests/auto_overflow_recvq_test
# {{TOXCORE_LOCAL_SRC}}/c-toxcore/_build/auto_tests/auto_overflow_sendq_test
# {{TOXCORE_LOCAL_SRC}}/c-toxcore/_build/auto_tests/auto_reconnect_test
# {{TOXCORE_LOCAL_SRC}}/c-toxcore/_build/auto_tests/auto_save_compatibility_test
# {{TOXCORE_LOCAL_SRC}}/c-toxcore/_build/auto_tests/auto_save_friend_test
# {{TOXCORE_LOCAL_SRC}}/c-toxcore/_build/auto_tests/auto_save_load_test
# {{TOXCORE_LOCAL_SRC}}/c-toxcore/_build/auto_tests/auto_send_message_test
# {{TOXCORE_LOCAL_SRC}}/c-toxcore/_build/auto_tests/auto_set_name_test
# {{TOXCORE_LOCAL_SRC}}/c-toxcore/_build/auto_tests/auto_set_status_message_test
# {{TOXCORE_LOCAL_SRC}}/c-toxcore/_build/auto_tests/auto_tcp_relay_test
# {{TOXCORE_LOCAL_SRC}}/c-toxcore/_build/auto_tests/auto_tox_dispatch_test
# {{TOXCORE_LOCAL_SRC}}/c-toxcore/_build/auto_tests/auto_tox_events_test
# {{TOXCORE_LOCAL_SRC}}/c-toxcore/_build/auto_tests/auto_tox_many_tcp_test
# {{TOXCORE_LOCAL_SRC}}/c-toxcore/_build/auto_tests/auto_tox_many_test
# {{TOXCORE_LOCAL_SRC}}/c-toxcore/_build/auto_tests/auto_tox_strncasecmp_test
# {{TOXCORE_LOCAL_SRC}}/c-toxcore/_build/auto_tests/auto_toxav_basic_test
# {{TOXCORE_LOCAL_SRC}}/c-toxcore/_build/auto_tests/auto_toxav_many_test
# {{TOXCORE_LOCAL_SRC}}/c-toxcore/_build/auto_tests/auto_typing_test
# {{TOXCORE_LOCAL_SRC}}/c-toxcore/_build/auto_tests/auto_version_test

- name: Run c-toxcore dockerfiles on the tester
  delegate_to: localhost
  shell: |
    [ -d {{TOXCORE_LOCAL_SRC}}/dockerfiles ] || exit 0
    cd {{TOXCORE_LOCAL_SRC}}/dockerfiles || exit 1
    exit 0

# - alpine-s390x
# - alpine-x86_64
# - buildfarm
# - compcert
# - flutter
# - frama-c
# - freebsd
# - ghc-android
# - ghc
# - haskell
# - infer
# - kythe
# - nacl
# - windows-qt5
# - windows


- name: Run toxygen_wrapper on the tester
  delegate_to: localhost
  shell: |
    [ -d {{TOXCORE_LOCAL_SRC}}/toxygen_wrapper ] || exit 0
    cd {{TOXCORE_LOCAL_SRC}}/toxygen_wrapper || exit 1
    exit 0

