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

- set_fact:
    AGI_target: Gentoo2

- name: look for nbd partitions
  shell: |
    grep nbd /proc/partitions | head -1|sed -e 's/.* //'
  register: nbd_out
  failed_when: false
  check_mode: false

- name: "include base by-platform vars"
  include_vars: "{{item}}.yml"
  with_items:
    - "Linux"
    - "{{ ansible_distribution }}{{ ansible_distribution_major_version }}"
    - "target_{{AGI_target}}"
  tags: always

- name: find module gentooimgr
  shell: |
    echo nbd_disk={{ nbd_disk }} ansible_connection={{ ansible_connection }}
    echo ansible_distribution={{ansible_distribution}} BOX_GENTOO_FROM_MP={{BOX_GENTOO_FROM_MP}}
    [ -d '{{PLAY_ANSIBLE_SRC}}/src/ansible_gentooimgr' ] || exit 1
    [ -f '{{PLAY_ANSIBLE_SRC}}/src/ansible_gentooimgr/__init__.py' ] || exit 2
    [ -d '{{PLAY_ANSIBLE_SRC}}/src/ansible_gentooimgr/gentooimgr' ] || exit 3
    [ -f '{{PLAY_ANSIBLE_SRC}}/src/ansible_gentooimgr/gentooimgr/__init__.py' ] || exit 4
    {{ansible_python_interpreter}} \
        -c "import os, sys; sys.path.append('{{PLAY_ANSIBLE_SRC}}/src/ansible_gentooimgr'); import gentooimgr; print(os.path.dirname(gentooimgr.__file__))" || exit 5
  register: gentooimgr_out
  check_mode: false
  ignore_errors: true

- block:

  - name: set AGI_gentooimgr_configs
    set_fact:
      AGI_gentooimgr_configs: "{{gentooimgr_out.stdout_lines[-1]}}/configs"

  - name: check for a gentooimgr base config
    shell: |
      if [ -z "{{BOX_NBD_DEV}}" ] ; then
          echo ERROR: empty "BOX_NBD_DEV" - define it in hosts.yml
          exit 1
      fi
      if ! grep -q "{{BOX_NBD_DEV}}" /proc/partitions ; then
          echo ERROR: not mounted BOX_NBD_DEV="{{BOX_NBD_DEV}}" - use qemu-nbd
          echo qemu-img create -f qcow2 /a/tmp/GentooImgr/gentoo.qcow2 20G
          echo qemu-nbd -n -f qcow2 -c /dev/nbd1 /a/tmp/GentooImgr/gentoo.qcow
          exit 2
      fi
      exit 0

  - name: make a gentooimgr base config
    shell: |
      base=base.json
      tofile="{{AGI_GENTOOIMGR_CONFIGFILE}}"
      todir=`dirname "$tofile"`
      [ -d $todir ] || mkdir $todir
      if [ ! -f "$tofile" ] ; then
          if [ -f "{{AGI_gentooimgr_configs}}/$base" ] ; then
              cp -p  "{{AGI_gentooimgr_configs}}/$base" "$tofile" || exit 2
          else
              echo WARN: FNF "{{AGI_gentooimgr_configs}}/$base"
              # use base.json
          fi
        fi
      # should operate on json with jq
      if [ -f "$tofile" ] ; then
          #   "imgsize": "20G",
          sed -i -e 's@"imgsize": ".*"@"imgsize": "{{BOX_NBD_OVERLAY_GB}}"@' $tofile
          #   "memory": 4096,
          sed -i -e 's@"memory": ".*"@"imgsize": "{{BOX_NBD_OVERLAY_RAM}}"@' $tofile
          #   "mountpoint": "/mnt/gentoo",
          sed -i -e 's@"mountpoint": ".*"@"mountpoint": "{{BOX_NBD_MP}}"@' $tofile
          #   "imagename": null,
          sed -i -e 's@"imagename": ".*"@"imagename": "{{BOX_NBD_OVERLAY_NAME}}"@' $tofile
          #   "initsys": "openrc",
          sed -i -e 's@"initsys": ".*"@"initsys: "{{BOX_NBD_BASE_PROFILE}}"@' $tofile
          #   "iso": null,
          #   "portage": null,
          sed -i -e 's@""portage": ".*"@""portage: "{{BOX_NBD_PORTAGE_FILE}}"@' $tofile
          #   "stage3": null,
          sed -i -e 's@""stage3": ".*"@""stage3: "{{BOX_NBD_STAGE3_FILE}}"@' $tofile
          # "partition": 1      
          sed -i -e 's@"partition": ".*"@"partition": 3@' $tofile
          # but this is crucial
          # "disk": "/dev/sda",
          sed -i -e 's@"disk": ".*"@"disk": "{{BOX_NBD_DEV}}"@' $tofile
          grep {{BOX_NBD_DEV}} $tofile || exit 4
      fi
      
  - name: check for library/ansible_gentooimgr.py
    shell: |
      # find it
      [ -f '{{PLAY_ANSIBLE_SRC}}/library/ansible_gentooimgr.py' ] || exit 1
      # run it
      echo '{}' | \
      {{ansible_python_interpreter}} \
          '{{PLAY_ANSIBLE_SRC}}/library/ansible_gentooimgr.py' 2>&1| \
          grep "Unable to figure out what parameters were passed" && exit 0
      echo WARN: ansible_gentooimgr.py BROKEN

  - name: ansible_gentooimgr NBD STATUS
    ansible_gentooimgr:
      action: status
      loglevel: "{{BOX_NBD_LOGLEVEL}}"
      threads: 1
      # base.json - bare filename in configs
      config: "{{AGI_GENTOOIMGR_CONFIGFILE}}" 
      profile: "{{BOX_NBD_BASE_PROFILE}}"
      kernel_dir: "{{BOX_NBD_KERNEL_DIR}}"
      portage: "{{BOX_NBD_PORTAGE_FILE}}"
      stage3: "{{BOX_NBD_STAGE3_FILE }}"
      temporary_dir: "{{BOX_NBD_BASE_DIR}}"
      download_dir:  "{{AGI_NBD_FILES}}"
    register: ansible_gentooimgr_out
    ignore_errors: true
    check_mode: false

  - name: "DEBUG: ansible-gentoo_install nbd_disk ansible_connection"
    debug:
      verbosity: 0
      var: ansible_gentooimgr_out

  check_mode: false
  when:
    - ansible_connection in ['chroot', 'local', 'libvirt_qemu']
    - ansible_distribution == 'Gentoo' or BOX_GENTOO_FROM_MP not in ['/', '']
#    - nbd_disk|default('') == AGI_NBD_DISK

- name: include_tasks local.yml
  include_tasks: local.yml
  when:
    - ansible_connection in ['chroot', 'local']
    - ansible_distribution == 'Gentoo' or BOX_GENTOO_FROM_MP not in ['/', '']
#    - nbd_disk|default('') == AGI_NBD_DISK
