---
# This playbook will try to ensure the localhost is configured as a Standalone
# Skolelinux host with the configuration details as listed on
# <https://wiki.nuug.no/skolelinux>
#
- name: "Configure Standalone Skolelinux host"
  hosts: localhost
  connection: local

  vars_files:
    - vars/common.yml

  tasks:
    - name: "Include machine specific variables | pc"
      ansible.builtin.include_vars:
        file: "vars/pc.yml"
      when:
        - ansible_system_vendor | lower != "google"

    - name: "Include machine specific variables | chromebook"
      ansible.builtin.include_vars:
        file: "vars/chromebook.yml"
      when:
        - ansible_system_vendor | lower == "google"

    # We'll use hasrun.stat.exists as a check to ensure tasks we only want to
    # run once, are not run again. Use something like this:
    # when: not hasrun.stat.exists
    # in your tasks you only want to run once.
    - name: "Check if playbook has run before"
      ansible.builtin.stat:
        path: "/root/.ansible-playbook-has-run"
      register: hasrun

    - name: "Create ansible inventory file for localhost | mkdir"
      ansible.builtin.file:
        path: "/etc/ansible"
        owner: "root"
        group: "root"
        mode: 0755
        state: directory

    - name: "Create ansible inventory file for localhost | file"
      ansible.builtin.copy:
        content: "localhost"
        dest: "/etc/ansible/hosts"
        force: "no"
        owner: "root"
        group: "root"
        mode: 0644

    - name: "Set timezone"
      community.general.timezone:
        name: "{{ timezone }}"

    - name: "Update locales"
      community.general.locale_gen:
        name: "{{ item }}"
        state: present
      loop: "{{ locales }}"

    - name: "Set default locale"
      ansible.builtin.copy:
        dest: "/etc/default/locale"
        content: |
          LANG="{{ lang }}"
        owner: "root"
        group: "root"
        mode: 0644
      when: not hasrun.stat.exists

    - name: "Configure the X keyboard layout"
      ansible.builtin.lineinfile:
        path: "/etc/default/keyboard"
        regexp: "^XKBLAYOUT="
        line: "XKBLAYOUT=\"no,ua\""
        owner: "root"
        group: "root"
        mode: 0644
      when: not hasrun.stat.exists

    - name: "Update apt repositories"
      ansible.builtin.copy:
        dest: "/etc/apt/sources.list"
        content: "{{ apt_repositories }}"
        owner: "root"
        group: "root"
        mode: 0644

    - name: "Install initial software packages"
      ansible.builtin.apt:
        name: "{{ packages }}"
        update_cache: yes
        autoremove: yes
        state: present
      when: not hasrun.stat.exists

    - name: "Install additional software packages"
      ansible.builtin.apt:
        name: "{{ additional_packages }}"
        update_cache: yes
        autoremove: yes
        state: present

    # We don't need to run this every time, it's taken care of by
    # unattended-upgrades
    - name: "Ensure all packages are upgraded"
      ansible.builtin.apt:
        upgrade: dist
      when: not hasrun.stat.exists

    - name: "Create .config directory for ukraina user"
      ansible.builtin.file:
        path: "/home/ukraina/.config"
        owner: "ukraina"
        group: "ukraina"
        mode: 0700
        state: directory

    - name: "Configure KDE | kscreenlockerrc"
      community.general.ini_file:
        path: "/home/ukraina/.config/kscreenlockerrc"
        section: "Daemon"
        option: "{{ item.option }}"
        value: "{{ item.value }}"
        no_extra_spaces: true
        owner: "ukraina"
        group: "ukraina"
        mode: 0600
      loop:
        - option: "Autolock"
          value: "false"
        - option: "LockOnResume"
          value: "false"
      when: not hasrun.stat.exists

    - name: "Configure KDE | kwalletrc"
      community.general.ini_file:
        path: "/home/ukraina/.config/kwalletrc"
        section: "Wallet"
        option: "{{ item.option }}"
        value: "{{ item.value }}"
        no_extra_spaces: true
        owner: "ukraina"
        group: "ukraina"
        mode: 0600
      loop:
        - option: "First Use"
          value: "false"
      when: not hasrun.stat.exists

    - name: "Configure KDE | powermanagementprofilesrc"
      ansible.builtin.copy:
        dest: "/home/ukraina/.config/powermanagementprofilesrc"
        content: |
          [AC]
          icon=battery-charging

          [AC][DPMSControl]
          idleTime=600
          lockBeforeTurnOff=0

          [AC][DimDisplay]
          idleTime=300000

          [AC][HandleButtonEvents]
          lidAction=1
          powerButtonAction=16
          powerDownAction=16
          triggerLidActionWhenExternalMonitorPresent=false

          [Battery]
          icon=battery-060

          [Battery][DPMSControl]
          idleTime=300
          lockBeforeTurnOff=0

          [Battery][DimDisplay]
          idleTime=120000

          [Battery][HandleButtonEvents]
          lidAction=1
          powerButtonAction=16
          powerDownAction=16
          triggerLidActionWhenExternalMonitorPresent=false

          [LowBattery]
          icon=battery-low

          [LowBattery][BrightnessControl]
          value=30

          [LowBattery][DPMSControl]
          idleTime=120
          lockBeforeTurnOff=0

          [LowBattery][DimDisplay]
          idleTime=60000

          [LowBattery][HandleButtonEvents]
          lidAction=1
          powerButtonAction=16
          powerDownAction=16
          triggerLidActionWhenExternalMonitorPresent=false
        owner: "ukraina"
        group: "ukraina"
        mode: 0600
      when: not hasrun.stat.exists

    - name: "Create .local/share/kwalletd directory for ukraina user"
      ansible.builtin.file:
        path: "/home/ukraina/.local/share/kwalletd"
        owner: "ukraina"
        group: "ukraina"
        mode: 0700
        state: directory

    - name: "Configure KDE | kdewallet.kwl"
      ansible.builtin.copy:
        src: "{{ item }}"
        dest: "/home/ukraina/.local/share/kwalletd/{{ item }}"
        force: false
        owner: "ukraina"
        group: "ukraina"
        mode: 0600
      loop:
        - "kdewallet.kwl"
        - "kdewallet.salt"
      when: not hasrun.stat.exists

    - name: "Create sddm config directory"
      ansible.builtin.file:
        path: "/etc/sddm.conf.d"
        owner: "root"
        group: "root"
        mode: 0755
        state: directory

    - name: "Update sddm.conf.d/autologin configuration"
      community.general.ini_file:
        path: "/etc/sddm.conf.d/autologin"
        section: "Autologin"
        option: "{{ item.option }}"
        value: "{{ item.value }}"
        no_extra_spaces: true
        owner: "root"
        group: "root"
        mode: 0644
      loop:
        - option: "User"
          value: "ukraina"
        - option: "Session"
          value: "plasma"
        - option: "Relogin"
          value: "true"
      when: not hasrun.stat.exists

    # This fails with "Could not find the requested service cfengine3" for some
    # reason.
    # - name: "Disable the cfengine3 service"
    #   ansible.builtin.service:
    #     name: "cfengine3"
    #     enabled: false

    # Remove apt mirror used during preseed
    - name: "Remove proxy configuration for apt"
      ansible.builtin.lineinfile:
        path: "/etc/apt/apt.conf"
        regexp: "^Acquire::http::Proxy "
        state: absent
      when: not hasrun.stat.exists

    # Copy over a script that can be used to update firmware for various parts
    # of the computer. This uses the tool as described in https://fwupd.org/
    - name: "Copy fwupdate.sh script"
      ansible.builtin.copy:
        src: "fwupdate.sh"
        dest: "/root/fwupdate.sh"
        force: false
        owner: "root"
        group: "root"
        mode: 0700

    - name: "Copy reglaptop script"
      ansible.builtin.copy:
        src: "reglaptop"
        dest: "/root/reglaptop"
        force: False
        owner: "root"
        group: "root"
        mode: 0700

    - name: "Create a script to run the ansible playbook"
      ansible.builtin.copy:
        src: "update-laptop"
        dest: "/root/update-laptop"
        owner: "root"
        group: "root"
        mode: 0700

    - name: "Create cronjob to update this ansible git repository"
      ansible.builtin.cron:
        name: "Update ansible-ukraina git repository"
        cron_file: "ansible-ukraina-update"
        user: "root"
        minute: "0"
        hour: "8,14,20"
        day: "*"
        month: "*"
        weekday: "*"
        job: "sleep $(shuf -i 0-30 -n 1)m && cd /root/ansible-ukraina && git pull"
        state: present

    - name: "Create cronjob to run ansible playbook"
      ansible.builtin.cron:
        name: "Run ansible-ukraina playbook"
        cron_file: "ansible-ukraina-run"
        user: "root"
        minute: "0"
        hour: "9,15,21"
        day: "*"
        month: "*"
        weekday: "*"
        job: "sleep $(shuf -i 0-30 -n 1)m && /root/update-laptop >/dev/null 2>&1"
        state: present

    - name: "Include unattended-upgrades role"
      ansible.builtin.include_role:
        name: unattended-upgrades
      tags:
        - unattended-upgrades

    # This file can be used to determine what tasks we want to run at a later
    # time, to avoid "resetting" something that might be modified locally (and
    # should remain so!)
    - name: "Create a file to indicate this playbook has run"
      ansible.builtin.file:
        path: "/root/.ansible-playbook-has-run"
        state: touch
        owner: "root"
        group: "root"
        mode: 0640
        modification_time: preserve
        access_time: preserve
