#
# Get CPU type:
# sh -c "clang -v -fsyntax-only -march=native -x c /dev/null 2>&1 | grep -e '-target-cpu' | sed -e 's|.*-target-cpu \([[:alnum:]]*\) .*|\1|'"
#
# Check the CPU type:
# make -C /usr/src CPUTYPE=broadwell -V MACHINE_CPU
#
# Generate options file for ports
# poudriere options -j 12amd64 -p default -z stream -f /usr/local/etc/poudriere.d/12amd64-default-stream-pkglist
#
# Generate options file for specific ports
# poudriere options -j 12amd64 -p default -z stream -c lang/gcc48
#
# Build the packages
# poudriere bulk -j 12amd64 -p default -z stream -f /usr/local/etc/poudriere.d/12amd64-default-stream-pkglist
#
# List installed packages
# pkg query -e '%a = 0' '%o' | sort
#
# Consider setting the following in the poudriere vm-bhyve config:
# priority="20"

- name: Install packages
  package:
    name:
      - poudriere
      - bash
      - rsync
      - flock
    state: present

- name: Create directories
  file:
    name: "{{ item }}"
    state: directory
    mode: 0755
    owner: root
    group: wheel
  loop:
    # - /usr/ports/distfiles
    - /opt/poudriere/build_configs
    - /usr/local/poudriere/data/logs/bulk

- name: Install Configuration
  copy:
    src: "files/{{ item.src }}"
    dest: "{{ item.dest }}"
    mode: 0600
    owner: root
    group: wheel
  loop:
    - src: poudriere.conf
      dest: /usr/local/etc/poudriere.conf
    - src: poudriere.key
      dest: /usr/local/etc/poudriere.d/poudriere.key
#     - src: poudriere_deploy_ed25519
#       dest: /usr/local/etc/poudriere.d/poudriere_deploy_ed25519

# - name: Install Configuration directory
#   copy:
#     src: "files/{{ item.src }}"
#     dest: "{{ item.dest }}"
#     owner: root
#     group: wheel
#   loop:
#     - src: poudriere.d
#       dest: /usr/local/etc/

- name: Install scripts
  copy:
    src: "files/{{ item.src }}"
    dest: "{{ item.dest }}"
    mode: 0755
    owner: root
    group: wheel
  loop:
    - src: poudboot.bash
      dest: /usr/local/bin/poudboot

- name: Install Configuration
  template:
    src: "build_config.j2"
    dest: "/opt/poudriere/build_configs/{{ item.jail }}-{{ item.ports }}-{{ item.set }}"
    owner: root
    group: wheel
    mode: 0600
  loop: "{{ poudriere_builds }}"

- name: Install rc script
  copy:
    src: "files/{{ item.src }}"
    dest: "/usr/local/etc/rc.d/{{ item.dest|default(item.src) }}"
    owner: root
    group: wheel
    mode: 0755
  loop:
    - src: poudboot

- name: Get ports tree list
  command: poudriere ports -ln
  register: poudriere_ports_tree_list
  changed_when: false
  check_mode: no

- name: Configure the ports tree
  command: poudriere ports -c -m null -M /usr/local/portshaker/trees/main -p default
  when: '"default" not in poudriere_ports_tree_list.stdout_lines'

- name: Get jail list
  command: poudriere jail -l -n -q
  register: poudriere_jail_list
  changed_when: false
  check_mode: no

- name: Create the jails
  when: item.version != "CURRENT"
  command: |-
    poudriere jail {{poudriere_perf_flags}} -c -j {{ item.jail }} -v {{ item.version }}
  args:
    creates: "/usr/local/poudriere/jails/{{ item.jail }}"
  loop: "{{ poudriere_builds }}"

- name: Create the jails
  when: item.version == "CURRENT"
  # -D clones the entire history instead of just the most recent commit
  command: |-
    poudriere jail {{poudriere_perf_flags}} -c -j {{ item.jail }} -v {{ item.branch|default("main") }} -a amd64 -m git -D -U https://git.FreeBSD.org/src.git -K {{ item.kernel|default("GENERIC") }}
  args:
    creates: "/usr/local/poudriere/jails/{{ item.jail }}"
  loop: "{{ poudriere_builds }}"

# - name: Get current jail version
#   command: poudriere jail -i -j current
#   register: current_jail_version
#   changed_when: false
#   check_mode: no

# - name: Set current jail version
#   command: "poudriere jail -u {{poudriere_perf_flags}} -j current -t {{ freebsd_version }}"
#   when: freebsd_version[:9] not in current_jail_version.stdout