# -*- mode: yaml; indent-tabs-mode: nil; tab-width: 2; coding: utf-8-unix -*-
---
- name: "DEBUG: ansible-gentoo_install copy.yml"
  debug:
    verbosity: 1
    msg: "DEBUG: ansible-gentoo_install copy.yml"

- name: test we are NOT in the chroot
  shell: |
    df | grep "{{AGI_NBD_MP}}" || exit 1
    [ -n "{{AGI_NBD_MP}}" ] || exit 2
    [ -d "{{AGI_NBD_MP}}" ] || exit 3
    [ "{{ansible_distribution}}" == 'Gentoo' ] || \
      ( {{AGI_GENTOO_FROM_MP}} != '' && "{{AGI_GENTOO_FROM_MP}}" != '/' ) || \
      exit 4
    [ -d "{{AGI_GENTOO_FROM_MP}}" ] || exit 5
  check_mode: false

- name: resolve kernel symlink
  shell: |
    [ -h {{AGI_GENTOO_FROM_MP}}/usr/src/linux ] && \
      echo $(readlink /usr/src/linux | sed -e 's@/$@@' ) && \
      exit 0
    echo linux
  register: kernel_out
  check_mode: false

- name: install to mp from source
  delegate_to: localhost
  shell: |
    cd {{AGI_GENTOO_FROM_MP}}/usr/src/linux || exit 1
    [ -d "{{AGI_NBD_MP}}/lib/modules" ] || mkdir "{{AGI_NBD_MP}}/lib/modules"
    make INSTALL_PATH={{AGI_NBD_MP}}/boot install || exit 4
    make INSTALL_MOD_PATH={{AGI_NBD_MP}} modules_install || exit 5
  args:
    creates: "{{AGI_NBD_MP}}/lib/modules/{{kernel_out.stdout}}"
  when: AGI_use_local_kernel

- name: copy kernel sources
  copy:
    src:  "{{AGI_GENTOO_FROM_MP}}/usr/src/{{kernel_out.stdout}}"
    dest: "{{AGI_NBD_MP}}/usr/src/{{kernel_out.stdout}}"
    remote_src: no
  when:
    - kernel_out.rc|default(1) == 0
    - AGI_use_local_kernel
    - false

- name: resolve kver
  shell: |
    kernel="{{kernel_out.stdout}}"
    kver=$( echo $kernel | sed -e 's/.*-6\.\([0-9]\)/6.\1/' -e 's/-.*//' )
    echo $kver
  register: kver_out
  check_mode: false
  when:
    - kernel_out.rc|default(1) == 0

- name: resolve kmods
  shell: |
    ls -d {{AGI_GENTOO_FROM_MP}}/lib/modules/{{kver_out.stdout}}* | head -1
  register: mods_out
  check_mode: false
  when:
    - kver_out.rc|default(1) == 0

- name: resolve vmlinux
  shell: |
    kver="{{kver_out.stdout}}"
    ls {{AGI_GENTOO_FROM_MP}}/boot/vmlinuz-${kver}* | head -1
  register: vmlinux_out
  check_mode: false
  when:
    - kver_out.rc|default(1) == 0

- name: copy kernel vmlinux
  copy:
    src:  "{{AGI_GENTOO_FROM_MP}}{{vmlinux_out.stdout}}"
    dest: "{{AGI_NBD_MP}}/boot"
    remote_src: no
  when:
    - AGI_use_local_kernel
  check_mode: false

- name: resolve ramfs
  shell: |
    kver="{{kver_out.stdout}}"
    ls {{AGI_GENTOO_FROM_MP}}/boot/initramfs-pentoo-x86_64-${kver}* | head -1
  register: ramfs_out
  check_mode: false
  when:
    - kver_out.rc|default(1) == 0

- name: copy kernel vmlinux
  copy:
    src:  "{{AGI_GENTOO_FROM_MP}}{{ramfs_out.stdout}}"
    dest: "{{AGI_NBD_MP}}/boot"
    remote_src: no
  when:
    - AGI_use_local_kernel
    - ramfs_out.rc|default(1) == 0
    - false
  ignore_errors: true

- name: make directories
  shell: |
    cd {{AGI_GENTOO_FROM_MP}} || exit 1
    for dir in {{' '.join(AGI_bootstrap_dirs)}}; do
        [ -d "{{AGI_NBD_MP}}/$dir" ] && continue
        mkdir -p "{{AGI_NBD_MP}}/$dir"
    done
    for file in {{' '.join(AGI_bootstrap_files)}}; do
        [ -f "{{AGI_NBD_MP}}/$file" ] && continue
        cp -np "$file" "{{AGI_NBD_MP}}/$file"
    done
  ignore_errors: true

# dracut
