- name: Install packages
  package:
    name:
      - git
    state: present

- name: Create directories
  file:
    name: "{{ item }}"
    state: directory
    mode: 0755
    owner: "{{ build_user.name }}"
    group: "{{ build_user.group }}"
  loop:
    - "/usr/src"
    - "/usr/obj"

- name: chown the FreeBSD source
  file:
    name: "{{ item }}"
    state: directory
    owner: "{{ build_user.name }}"
    group: "{{ build_user.group }}"
    recurse: true
  loop:
    - "/usr/src"

- name: Clone FreeBSD Source
  git:
    repo: "https://git.FreeBSD.org/src.git"
    dest: /usr/src
    version: "{{ freebsd_version }}"
    force: true
  become: true
  become_user: "{{ build_user.name }}"
  diff: false

- name: Install Configuration
  copy:
    src: "files/{{ item.src }}"
    dest: "{{ item.dest }}"
    mode: 0644
    owner: root
    group: wheel
  loop:
    - src: make.conf
      dest: /etc/make.conf

- name: Install Configuration
  copy:
    src: "files/{{ item.src }}"
    dest: "{{ item.dest }}"
    mode: 0644
    owner: "{{ build_user.name }}"
    group: "{{ build_user.group }}"
  loop:
    - src: CUSTOM
      dest: /usr/src/sys/amd64/conf/CUSTOM

- name: Install Configuration
  template:
    src: "templates/{{ item.src }}.j2"
    dest: "{{ item.dest }}"
    mode: 0644
    owner: root
    group: wheel
  loop:
    - src: src.conf
      dest: /etc/src.conf

- name: Install scripts
  copy:
    src: "files/{{ item.src }}"
    dest: "{{ item.dest }}"
    mode: 0700
    owner: "{{ build_user.name }}"
    group: "{{ build_user.group }}"
  loop:
    - src: freebsd_update_step1
      dest: /usr/local/bin/freebsd_update_step1
    - src: freebsd_update_step2
      dest: /usr/local/bin/freebsd_update_step2
