# role perfact_base_install
# installation of perfact-defaults for all systems

- name: include role set_hostname
  tags: set_hostname
  include_role:
    name: general/set_hostname

- name: Use role in loop init-packages
  tags: perfact-repo
  include_role:
    name: "{{ role_base_file }}"
  loop:
    - 'general/perfact-repo'
  loop_control:
    loop_var: role_base_file
  args:
    apply:
      become: true

- name: install etckeeper and git
  become: true
  ansible.builtin.package:
    name:
    - git
    - etckeeper
    state: present

- name: install unattended-upgrades
  become: true
  ansible.builtin.package:
    name:
    - unattended-upgrades
    state: present

- name: configure unattendend upgrades
  become: true
  ansible.builtin.template:
    src:   "etc/apt/apt.conf.d/90perfact-custom"
    dest: "/etc/apt/apt.conf.d/90perfact-custom"
  notify: restart unattendend_upgrades

- name: configure systemd/timesyncd
  # this configuration is also required by pfpyroutinecheck
  become: true
  ansible.builtin.lineinfile:
    path: /etc/systemd/timesyncd.conf
    line: 'NTP=ntp.perfact.de pool.ntp.org'
    insertafter: '[Time]'
  notify: restart systemd-timesyncd.service
  when: inventory_hostname not in ['tyke-devel','tyke']

- name: set pfsystemid
  become: true
  ansible.builtin.copy:
    # ugly workaround for trailing newline
    content: "{{ pfsystemid }}{{'\n'}}"
    dest: "/etc/pfsystemid"

- name: set pfsystemname
  become: true
  ansible.builtin.copy:
    # ugly workaround for trailing newline
    content: "{{ pfsystemname }}{{'\n'}}"
    dest: "/etc/pfsystemname"

- name: motd do not edit
  tags: motd
  become: true
  ansible.builtin.copy:
    content: "\n\n      This System is completely managed by Ansible.\n\n           Do not modify anything manually!\n\n\n"
    dest: /etc/motd

- name: hide motds
  tags: motd
  become: true
  ansible.builtin.file:
    path: "/etc/update-motd.d/{{ item }}"
    mode: 0644
  loop:
    - 88-esm-announce
    - 10-help-text
    - 91-contract-ua-esm-status
  when: ansible_distribution == 'Ubuntu'

- name: install tools
  become: true
  ansible.builtin.package:
    name:
      - vim
      - tig
      - emacs
      - rsync
      - atop
    state: present

- name: remove unnecessary packages
  become: true
  ansible.builtin.package:
    name:
      - popularity-contest
      - update-notifier
      - fwupd
      - fwupd-signed
      - snapd
      - bolt
      - libarchive13
      - libatasmart4
      - libblockdev-crypto2
      - libblockdev-fs2
      - libblockdev-loop2
      - libblockdev-part-err2
      - libblockdev-part2
      - libblockdev-swap2
      - libblockdev-utils2
      - libblockdev2
      - libfwupd2
      - libfwupdplugin1
      - libgcab-1.0-0
      - libgusb2
      - libsmbios-c2
      - libtss2-esys0
      - libudisks2-0
      - libvolume-key1
      - libxmlb1
      - sbsigntool
      - secureboot-db
      - squashfs-tools
      - tpm-udev
      - udisks2
      - libjcat1
      - libjson-glib-1.0-0
      - libjson-glib-1.0-common
      - libparted-fs-resize0
    state: absent

# TODO purge snapd

- name: create group perfact
  become: true
  ansible.builtin.group:
    name: perfact
    gid: 1000

- name: create user perfact
  become: true
  ansible.builtin.user:
    name: perfact
    comment: perfact Wartungsaccount
    uid: 1000
    shell: /bin/bash
    group: perfact
    groups: adm,sudo,dip,plugdev
    append: true
    create_home: true

- name: add bash_login for user perfact
  ansible.builtin.template:
    src: bash_login.j2
    dest: /home/perfact/.bash_login
    owner: perfact
    group: perfact
    mode: 0600

- name: base_install install perfact-phonehome
  tags: perfact-phonehome
  include_role:
    name: general/perfact-phonehome

- name: base_install create custom folder
  become: true
  ansible.builtin.file:
   path: /opt/perfact/custom
   recurse: yes
   state: directory
