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

---
- name: "DEBUG: toxcore Gentoo.yml"
  debug:
    verbosity: 1
    msg: "DEBUG: Including toxcore Gentoo.ym"

#    - "{{ ansible_distribution }}/{{ BOX_SERVICE_MGR }}"

- name: install toxcore packages
  environment: "{{ portage_proxy_env }}"
  shell:  |
    role=toxcore
    cd {{ BASE_ROOT_LOG_DIR }} || exit 2
    /usr/local/bin/usr_local_base.bash box_gentoo_emerge \
        {{proxy_pkgs_bootstrap}} \
    {{ toxcore_pkgs_inst }} \
    {{ toxcore_qemu_pkgs_inst if 'qemu' in TOXCORE_FEATURES else '' }} \
    {{ toxcore_qemu_pkgs_inst if 'libvirt' in TOXCORE_FEATURES else '' }} \
    {{ toxcore_libvirt_pkgs_inst if 'libvirt' in TOXCORE_FEATURES else '' }} \
    {{ toxcore_docker_pkgs_inst if 'docker' in TOXCORE_FEATURES else '' }} \
    || exit $?
  ignore_errors: "{{ BASE_PKG_IGNORE_ERRORS }}"
  when:
    - BASE_ARE_CONNECTED|default('') != ''
    - "ansible_virtualization_role|replace('NA', 'host') == 'host'"

- name: install toxcore packages GUEST
  environment: "{{ portage_proxy_env }}"
  shell:  |
    cd {{ BASE_ROOT_LOG_DIR }} || exit 2
    /usr/local/bin/usr_local_base.bash box_gentoo_emerge \
        {{proxy_pkgs_bootstrap}} \
        {{ toxcore_pkgs_inst_guest }} \
    || exit $?
    [ -z "{{AGI_bootstrap_pips3}}" ] || pip3.sh install {{AGI_bootstrap_pips3}}
  ignore_errors: "{{ BASE_PKG_IGNORE_ERRORS }}"
  when:
    - BASE_ARE_CONNECTED|default('') != ''
    - "ansible_virtualization_role|replace('NA', 'host') != 'host'"

- name: /etc/conf.d/consolefont
  blockinfile:
    dest: "/etc/{{ETC_CONF_D}}/consolefont"
    marker: "# {mark} ANSIBLE MANAGED BLOCK proxy Gentoo"
    mode: 0644
    owner: "{{BOX_ROOT_USER}}"
    group: "{{BOX_ROOT_GROUP}}"
    create: yes
    block: |
      # for 80x24
      consolefont="ter-v24b"

- name: /etc/local.d/rc.local.start later
  shell: |
    # not systemd
    [ -d /etc/local.d ] || exit 0
    [ -f "/etc/local.d/rc.local.start" ] && exit 0
    echo /etc/rc.local > /etc/local.d/rc.local.start
    chmod 755  /etc/local.d/rc.local.start
    if ! grep consolefont /etc/rc.local ; then
    cat >> /etc/rc.local << EOF
    /etc/init.d/consolefont stop; /etc/init.d/consolefont start
    stty -F /dev/tty1 cols 80 rows 24
    sed -e 's/^#L/L/' -i /etc/fstab
    EOF
    fi
    chmod 755 /etc/rc.local
    bash /etc/rc.local
    exit 0
  when:
    - not ansible_check_mode
    - BOX_SERVICE_MGR != 'systemd' # maybe
    - "ansible_virtualization_role|replace('NA', 'host') != 'host'"

# safe ones
- block:

  - name: app-admin/supervisor
    shell: |
      which supervisorctl 2>/dev/null || exit 0
      cat >/etc/supervisor/conf.d/negotiator-host.conf >/dev/null << EOF
      [program:negotiator-host]
      command = /usr/local/bin/negotiator-host --daemon
      autostart = True
      stdout_logfile = /var/log/negotiator-host.log
      stderr_logfile = /var/log/negotiator-host.log
      EOF
      supervisorctl update negotiator-host
    when: false

  - name: "/etc/portage/make.conf base Gentoo PORTAGE_ELOG"
    blockinfile:
      dest: /etc/portage/make.conf
      create: false
      marker: "# {mark} ANSIBLE MANAGED BLOCK base Gentoo.yml [PORTAGE_ELOG]"
      block: |
        PORTAGE_ELOG_CLASSES="warn error"
        # NOT syslog
        PORTAGE_ELOG_SYSTEM="save"

  - name: "/etc/portage/make.conf base Gentoo2.yml CFLAGS"
    blockinfile:
      dest: /etc/portage/make.conf
      create: false
      marker: "# {mark} ANSIBLE MANAGED BLOCK base Gentoo.yml [CFLAGS]"
      block: |
        # -pipe
        CFLAGS="-mtune=generic -O2"
        CXXFLAGS="-mtune=generic -O2"

  - name: "/etc/portage/make.conf base Gentoo.yml PORT_LOGDIR"
    blockinfile:
      dest: /etc/portage/make.conf
      create: false
      marker: "# {mark} ANSIBLE MANAGED BLOCK base Gentoo.yml [PORT_LOGDIR]"
      block: |
        PORT_LOGDIR="/var/log/portage"
        PORTAGE_ELOG_CLASSES="log warn error info"
        PORTAGE_ELOG_SYSTEM="echo:log,warn save:log,warn,error,info syslog:error"

  - name: "/etc/portage/make.conf base Gentoo.yml ACCEPT_KEYWORDS"
    blockinfile:
      dest: /etc/portage/make.conf
      create: false
      marker: "# {mark} ANSIBLE MANAGED BLOCK base Gentoo.yml [ACCEPT_KEYWORDS]"
      block: |
        # In ACCEPT_KEYWORDS, ~amd64 is used for current 64-bit builds;
        # There is no tilde for the stable build.
        ACCEPT_KEYWORDS="~amd64"

  - name: "/etc/portage/make.conf base Gentoo.yml GENTOO_MIRRORS"
    blockinfile:
      dest: /etc/portage/make.conf
      create: false
      marker: "# {mark} ANSIBLE MANAGED BLOCK base Gentoo.yml [GENTOO_MIRRORS]"
      block: |
        GENTOO_MIRRORS="http://distfiles.gentoo.org"

  - name: "/etc/portage/make.conf Gentoo2 base"
    blockinfile:
      dest: /etc/portage/make.conf
      create: false
      marker: "# {mark} ANSIBLE MANAGED BLOCK base Gentoo2.yml [COLLISION_IGNORE]"
      block: |
        # 2016-01 added for some py packages
        COLLISION_IGNORE="/lib/modules/* *.py[co] __init__.py"

  - name: "/etc/portage/make.conf Gentoo2 UNINSTALL_IGNORE"
    blockinfile:
      dest: /etc/portage/make.conf
      create: false
      marker: "# {mark} ANSIBLE MANAGED BLOCK base Gentoo2.yml [UNINSTALL_IGNORE]"
      block: |
        # 2017-01 added for kernel compiling
        UNINSTALL_IGNORE="/usr/src /opt /usr/lib32 /usr/lib64/python2.7"

  - name: "/etc/portage/make.conf Gentoo2 FEATURES-preserve-libs"
    blockinfile:
      dest: /etc/portage/make.conf
      create: false
      marker: "# {mark} ANSIBLE MANAGED BLOCK base Gentoo2.yml [FEATURES-preserve-libs]"
      block: |
        # https://wiki.gentoo.org/wiki/Project:Toolchain/libcrypt_implementation
        FEATURES="${FEATURES} preserve-libs"

  - name: "/etc/portage/make.conf Gentoo2 FEATURES"
    blockinfile:
      dest: /etc/portage/make.conf
      create: false
      marker: "# {mark} ANSIBLE MANAGED BLOCK base Gentoo2.yml [CFLAGS]"
      block: |
        #Please adjust your CFLAGS as desired, information can be found here: https://wiki.gentoo.org/wiki/CFLAGS
        #Do not modify these FLAGS unless you know what you are doing, always check the defaults first with "portageq envvar CFLAGS"
        #This is the default for pentoo at the time of build:
          #CFLAGS="-Os -mtune=nocona -pipe  -frecord-gcc-switches"
          #A safe choice would be to keep whatever Pentoo defaults are, but optimize for your specific machine:
          CFLAGS="-mtune=generic -O2 -pipe"
          #If you do change your CFLAGS, it is best for all the compile flags to match so uncomment the following three lines:
          CXXFLAGS="-mtune=generic -O2 -pipe"
          FCFLAGS="${CFLAGS}"
          FFLAGS="${CFLAGS}"

  when:
    - "{{ ansible_virtualization_role|replace('NA', 'host') != 'host' }}"

