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

- name: test we are in the chroot
  shell: |
    df | grep /mnt/gentoo && exit 1

- name: "make /mnt mountpoints"
  shell: |
    [ -d /mnt ] || mkdir /mnt || exit 1
    for elt in {{ AGI_bootstrap_mountpoints|join(' ') }} ; do
        [ -d $elt ] || mkdir $elt
      done
    exit 0
  when: AGI_bootstrap_mountpoints|default([])|length > 0

- name: configure timezone
  lineinfile:
    dest: /etc/timezone
    line: "{{ AGI_install_timezone }}"
    regexp: '^'
    create: yes
    owner: root
    mode: '0644'

- name: timezone symlink
  file:
    dest: /etc/localtime
    src: /usr/share/zoneinfo/{{ AGI_install_timezone }}
    state: link
    force: yes

- name: configure locales
  lineinfile:
    dest: /etc/locale.gen
    line: "{{ item }}"
  with_items: "{{ AGI_install_locales }}"

- name: generate locales
  command: locale-gen

- name: set default locale
  command: eselect locale set {{ AGI_install_locale_default }}

- name: configure root mount
  mount:
    name: /
    src: "{{ AGI_install_disk }}p3"
    fstype: ext4
    state: present
    opts: noatime
    passno: 1

- name: configure boot mountpoint
  mount:
    name: /boot
    src: "{{ AGI_install_disk }}p1"
    fstype: ext2
    state: present
    opts: noatime,ro
    dump: 1
    passno: 2

- name: scramble root password
  shell: |
    echo "{{ AGI_install_root_password|default('root') }}" | \
        openssl password -1 -stdin
  register: root_password_out

- name: set root password
  user:
    name: root
    password: "{{ root_password_out.stdout }}"

- name: scramble gentoo password
  shell: |
    echo "{{ AGI_install_gentoo_password|default('gentoo') }}" | \
        openssl password -1 -stdin
  register: gentoo_password_out

- name: set gentoo password
  user:
    name: gentoo
    password: "{{ gentoo_password_out.stdout }}"
    group: "{{ BOX_USER_GROUP }}"
    append: true
    groups: ['{{ BOX_ALSO_GROUP }}', 'wheel']
    create_home: yes
    shell: /bin/bash
  #? usermod: user vagrant is currently used by process 2190
  ignore_errors: true

- name: configure sudoers
  lineinfile:
    dest: /etc/sudoers
    line: "%wheel ALL=(ALL:ALL) ALL"
    regexp: '^# %wheel ALL=(ALL:ALL) ALL'
    create: yes
    owner: root
    mode: '0640'

- name: make symlinks
  shell: |
    [ -e /var/db/repos/gentoo ] || \
      ln -s  /usr/portage /var/db/repos/gentoo
    grep -q ^tmpfs /etc/fstab || \
      echo >> /etc/fstab 'tmpfs                   /dev/shm                tmpfs   defaults,noexec,size=5%        0 0'
    
- block:

  - name: make symlinks
    shell: |
      {% for elt in AGI_bootstrap_pkgs %}
          [ -h {{ elt.to }} ] && continue
          [ -d {{ elt.to }} ] && echo "WARN: {{ elt.to }} exists as a directory" && continue
          parent=`dirname {{ elt.to }}`
          [ -d  $parent ] || mkdir -p $parent
          #? -h-e
          [ -h {{ elt.to }} ] || \
            ln -s {{ elt.from }} {{ elt.to }}
      {% endfor %}
  when: AGI_bootstrap_pkgs|default([])|length > 0
