#
# 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
      - rust-nightly # to build poudboot
    state: present

- name: Create directories
  file:
    name: "{{ item }}"
    state: directory
    mode: 0755
    owner: root
    group: wheel
  loop:
    - /usr/ports/distfiles
    - /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 poudboot
  command: "cargo install --profile release-lto --git 'https://code.fizz.buzz/talexander/poudboot.git'"
  args:
    creates: /root/.cargo/bin/poudboot

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

- name: Install src.conf
  when: item.srcconf is defined
  copy:
    src: "files/{{ item.srcconf }}"
    dest: "/usr/local/etc/poudriere.d/{{ item.jail }}-src.conf"
    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"
  check_mode: false
  command: |-
    echo poudriere jail {{poudriere_perf_flags}} -c -j {{ item.jail }} -v {{ item.version }} -a amd64 -K {{ item.kernel|default("GENERIC") }} -B -b
  args:
    creates: "/usr/local/poudriere/jails/{{ item.jail }}"
  loop: "{{ poudriere_builds }}"

- name: Create the jails
  when: item.version == "CURRENT"
  check_mode: false
  # -D clones the entire history instead of just the most recent commit
  # -B to build the pkgbase packages
  # -b to build the jail OS from source
  command: |-
    echo 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-NODEBUG") }} -B -b
  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
