# -*- mode: yaml; indent-tabs-mode: nil; tab-width: 2; coding: utf-8-unix -*-
---
- name: "DEBUG: ansible-gentoo_install local"
  debug:
    verbosity: 0
    msg: "DEBUG: ansible-gentoo_install local BOX_NBD_DEV={{BOX_NBD_DEV}}"
  check_mode: no

- assert:
    that:
      -  "'{{BOX_NBD_DEV}}' != ''"
  when: ansible_connection in ['local', 'chroot']

- set_fact:
    AGI_use_local_kernel: true
  when:
    - ansible_distribution == 'Gentoo' or BOX_GENTOO_FROM_MP not in ['/', '']

- set_fact:
    AGI_PROXY_MODE: "{{PROXY_MODE|default('')}}"
  when:
     - PROXY_MODE|default('') != ''
  check_mode: no

- set_fact:
    AGI_PROXY_MODE: "{{BOX_PROXY_MODE|default('')}}"
  when:
     - AGI_PROXY_MODE == ''
  check_mode: no

- block:

  - name: check for mounted disk
    shell: |
      grep '/dev/{{AGI_NBD_DEV}}p3' /proc/mounts && exit 0
      if [ ! -f "{{BOX_NBD_BASE_QCOW}}" ] ; then
        echo ERROR: not created BOX_NBD_DEV="{{BOX_NBD_DEV}}" - use
        echo qemu-img create -f qcow2 "{{BOX_NBD_BASE_QCOW}}" 20G
        exit 1
      fi
      if ! ps ax | grep -v grep | \
          grep "qemu-nbd.*{{AGI_NBD_DEV}}.*{{BOX_NBD_BASE_QCOW}}" ; then
        echo ERROR: not mounted BOX_NBD_DEV="{{BOX_NBD_DEV}}" - use
        echo qemu-nbd -n -f qcow2 -c /dev/nbd1 {{BOX_NBD_BASE_QCOW}}
        exit 2
        fi
      exit 3
    failed_when: false
    changed_when: false
    register: check_mounted_disk
    check_mode: no

  - name: partition if disk not mounted
    fail:
      msg: "make and nbd mount {{BOX_NBD_BASE_QCOW}}"
    when:
      - check_mounted_disk.rc in [2, 1]
    check_mode: no

  - name: partition if disk not mounted or active
    include: disk.yml
    when:
      - check_mounted_disk.rc > 2
    check_mode: no

  - name: mount root partition
    mount:
      name: "{{AGI_NBD_MP}}"
      src: "{{ AGI_install_disk }}p3"
      fstype: ext4
      state: mounted
    check_mode: false

  - name: create /boot mountpoint
    file:
      path: "{{AGI_NBD_MP}}/boot"
      state: directory
    check_mode: false

  - name: mount boot partition
    mount:
      name: "{{AGI_NBD_MP}}/boot"
      src: "{{ AGI_install_disk }}p1"
      fstype: ext2
      state: mounted
    check_mode: false

  - include: libvirt.yml
  - include: tarball.yml
  - include: copy.yml
    when: AGI_use_local_kernel

  - name: mount distfiles
    delegate_to: localhost
    shell: |
      [ -d "{{MOUNT_GENTOO_DISTFILES_ARCHIVES}}" ] || exit 1
      grep {{MOUNT_GENTOO_DISTFILES_ARCHIVES}} /proc/mounts && exit 0
      [ -d {{AGI_NBD_MP}}/usr/portage/ ] || exit 0
      [ -d {{AGI_NBD_MP}}/usr/portage/distfiles ] || mkdir {{AGI_NBD_MP}}/usr/portage/distfiles
      mount --bind {{MOUNT_GENTOO_DISTFILES_ARCHIVES}} {{AGI_NBD_MP}}/usr/portage/distfiles
    when:
      - "MOUNT_GENTOO_DISTFILES_ARCHIVES != ''"
      - "AGI_NBD_MP != ''"

  - include: chroot.yml

  delegate_to: localhost
  when: ansible_connection in ['chroot', 'local'] # libvirt?

- block:

  - name: check chroot wrapper installed
    shell: |
      [ -x /var/tmp/chroot_wrapper.sh ] || exit 1
      df /mnt/gentoo || exit 2
      /var/tmp/chroot_wrapper.sh /bin/df | grep /mnt/gentoo && exit 4
      exit 0
    register: chroot_out
    check_mode: false

  - name: enable chroot wrapper
    set_fact:
      ansible_shell_executable: /var/tmp/chroot_wrapper.sh
      old_ansible_python_interpreter: "{{ansible_python_interpreter}}"
      ansible_python_interpreter: "/usr/bin/python3"
    check_mode: false

  when: ansible_connection in ['local']

- block:

  - include: portage.yml
  - include: misc.yml

  - include: network.yml

  - include: kernel.yml
    when: not AGI_use_local_kernel

  - include: bootloader.yml
  - include: daemons.yml

  #   - include: finish.yml

  check_mode: false
  when:
    - "ansible_connection in ['chroot'] or (ansible_connection in ['local'] and chroot_out.rc|default(1) == 0)"
  rescue:
    - debug:
        msg: "ERROR: error during chroot execution"

- name: disable chroot wrapper
  set_fact:
    ansible_shell_executable: /bin/sh
    ansible_python_interpreter: "{{old_ansible_python_interpreter}}"
  when:
    - "ansible_connection in ['local'] and chroot_out.rc|default(1) == 0"
  check_mode: false

- name: unmount filesystems
  mount:
    name: "{{AGI_NBD_MP}}/{{ item }}"
    state: unmounted
  with_items:
    - proc
    - sys
    - dev/pts
    - dev/shm
    - dev
    - boot
    - ''
  loop_control:
    label: "{{AGI_NBD_MP}}/{{ item }}"
  when:
    - "ansible_connection in ['local'] and chroot_out.rc|default(1) == 0"
    - false # leave it mounted for testing

- name: dismount any other mounts
  shell: |
    if [ -z "{{MOUNT_GENTOO_DISTFILES_ARCHIVES}}" ] && \
         [ -d "{{MOUNT_GENTOO_DISTFILES_ARCHIVES}}" ] && \
         grep {{MOUNT_GENTOO_DISTFILES_ARCHIVES}} /proc/mounts ; then
           umount  {{MOUNT_GENTOO_DISTFILES_ARCHIVES}}
      fi
    df -a | grep "{{AGI_NBD_MP}}" | sed -e 's/.* //' | tac | while read elt;do
        umount $elt
        done
    base_chroot_unbind.bash "{{AGI_NBD_MP}}"
  when:
    - "ansible_connection in ['chroot'] or chroot_out.rc|default(1) == 0"
    - false # leave it mounted for testing
