---
# Set up directory and files for specifying software needs rather than using hosts file.

- name: General | Account Management | Provisioning Configuration | Variables 1
  set_fact:
    prov_dir: "/usr/local/etc/hyperling-scm"
    provision_variables: []

- name: General | Account Management | Provisioning Configuration | Variables 2
  set_fact:
    gen_file: "{{ prov_dir }}/general.ini"
    wrk_file: "{{ prov_dir }}/workstation.ini"
    mnr_file: "{{ prov_dir }}/miner.ini"
    srv_file: "{{ prov_dir }}/server.ini"

- name: General | Account Management | Provisioning Configuration | Create Directory
  file:
    path: "{{ prov_dir }}"
    state: directory
    mode: '0755'


## General ##

- name: General | Account Management | Provisioning Configuration | General | Create
  blockinfile:
    path: "{{ gen_file }}"
    block: |
      ; Please note that all potions require lowercase keys and values.
      ;
      ;;;;;;;;;;;;;;;;;;;;          Basic Options           ;;;;;;;;;;;;;;;;;;;;
      ;
      ;     enable : Provide true for the provision to occur.
      ;                Default: false
      ;
      ;       user : Provide the name of the user account.
      ;                Example: tom, jerry, kim, sarah
      ;                Default: user
      ;
      ;  user_desc : Provide the description of the user account.
      ;                Example: Thomas, Jerry, Kimberly, Sarah
      ;                Default: User
      ;
      ;  sshd_port : Determine the port which SSHD should listen on.
      ;                Example: 12345
      ;                Default: 22
      ;
      ; pentesting : Set to true to install tools such as metasploit.
      ;              nmap is already provided for reporting
      ;                Default: false
      ;
      ;;;;;;;;;;;;;;;;;;;;           Git Polling            ;;;;;;;;;;;;;;;;;;;;
      ;
      ;  For syncing this machine to a Git repository of this Ansible project
      ;  in this fashion: ssh://git@GIT_HOST:GIT_SSH_PORT/GIT_USER/GIT_PROJECT
      ;
      ;       branch : Branch to use on this machine.
      ;                  Example: main, dev, prod
      ;                  Default: main
      ;
      ;     git_host : Provide the git host that the machine should poll.
      ;                  Example: github.com, gitlab.com. gitea.com
      ;                  Default: git.hyperling.com
      ;
      ; git_ssh_port : Port of the server host to use for SSH requests.
      ;                  Example: 22, 222, 2222
      ;                  Default: 22
      ;
      ;     git_user : Provide the git user which projects are found under.
      ;                  Example: Hyperling, JimBobMcGeeJr
      ;                  Default: me
      ;
      ;  git_project : Name of this project on the server host.
      ;                  Example: Ansible, ansible-setup, my-env
      ;                  Default: env-ansible
      ;
      ;;;;;;;;;;;;;;;;;;;;            Swap File             ;;;;;;;;;;;;;;;;;;;;
      ;
      ; swap_block : Set to the block size if dd should create a swapfile.
      ;              Preferably keep this under 2G for integrity sakes, the
      ;              value is multiplied by count if you need a larger file.
      ;                Example: 500M, 512MB, 1G
      ;                Default: false
      ;
      ; swap_count : Set to the number of blocks to multiply the file size by.
      ;                Example: 8
      ;                Default: 1
      ;
      ;  swap_file : Set to the name and location of where the file should be.
      ;                Example: /usr/local/swap
      ;                Default: /swapfile
      ;
      ;;;;;;;;;;;;;;;;;;;;          Sync Frequency          ;;;;;;;;;;;;;;;;;;;;
      ;
      ; no_telem : Set to true to avoid setting up telemetry services.
      ;            Disables GitHub updates to this project.
      ;            Disables all telegraf pings.
      ;              Default: false
      ;
      ;  battery : Set to true to attempt to save battery life.
      ;            Slows down the rate of services such as telegraf and cron.
      ;              Default: false
      ;
      ;;;;;;;;;;;;;;;;;;;;         Server Shortcut          ;;;;;;;;;;;;;;;;;;;;
      ;
      ; For typing `prod` and getting straight to a server, like in env-termux.
      ;
      ; prod_host : Branch to use on this machine.
      ;               Example: myserver.com, mywebhost.net
      ;               Default: hyperling.com
      ;
      ; prod_port : Provide the git host that the machine should poll.
      ;               Example: 22, 222, 2222
      ;               Default: 22
      ;
      ; prod_user : Port of the server host to use for SSH requests.
      ;               Example: jim, bob, jeff, anne
      ;               Default: user
      ;
      [global]
    marker: '; {mark} MANAGED BY ANSIBLE | Generic Config'
    state: present
    create: yes
    backup: yes

- name: General | Account Management | Provisioning Configuration | General | Load
  set_fact:
    provision:  "{{ lookup('ini', 'enable     file={{gen_file}} default=false') }}"
    user:       "{{ lookup('ini', 'user       file={{gen_file}} default=ling') }}"
    user_desc:  "{{ lookup('ini', 'user_desc  file={{gen_file}} default=Hyperling') }}"
    branch:     "{{ lookup('ini', 'branch     file={{gen_file}} default=main') }}"
    pentesting: "{{ lookup('ini', 'pentesting file={{gen_file}} default=false') }}"
    no_telem:   "{{ lookup('ini', 'no_telem   file={{gen_file}} default=false') }}"
    battery:    "{{ lookup('ini', 'battery    file={{gen_file}} default=false') }}"
    swap_block: "{{ lookup('ini', 'swap_block file={{gen_file}} default=false') }}"
    swap_count: "{{ lookup('ini', 'swap_count file={{gen_file}} default=1') }}"
    swap_file:  "{{ lookup('ini', 'swap_file  file={{gen_file}} default=/swapfile') }}"
    sshd_port:  "{{ lookup('ini', 'sshd_port   file={{gen_file}} default=22') }}"
    git_host:     "{{ lookup('ini', 'git_host     file={{gen_file}} default=git.hyperling.com') }}"
    git_user:     "{{ lookup('ini', 'git_user     file={{gen_file}} default=me') }}"
    git_project:  "{{ lookup('ini', 'git_project  file={{gen_file}} default=env-ansible') }}"
    git_ssh_port: "{{ lookup('ini', 'git_ssh_port file={{gen_file}} default=22') }}"
    prod_host: "{{ lookup('ini', 'prod_host   file={{gen_file}} default=hyperling.com') }}"
    prod_port: "{{ lookup('ini', 'prod_port   file={{gen_file}} default=22') }}"
    prod_user: "{{ lookup('ini', 'prod_user   file={{gen_file}} default=user') }}"

- name: General | Account Management | Provisioning Configuration | General | List
  set_fact:
    provision_variables: "{{ provision_variables | combine(item) }}"
  loop:
    - { 'provision': "{{ provision }}" }
    - { 'user': "{{ user }}" }
    - { 'user_desc': "{{ user_desc }}" }
    - { 'branch': "{{ branch }}" }
    - { 'pentesting': "{{ pentesting }}" }
    - { 'no_telem': "{{ no_telem }}" }
    - { 'battery': "{{ battery }}" }
    - { 'swap_block': "{{ swap_block }}" }
    - { 'swap_count': "{{ swap_count }}" }
    - { 'swap_file': "{{ swap_file }}" }
    - { 'sshd_port': "{{ sshd_port }}" }
    - { 'git_host': "{{ git_host }}" }
    - { 'git_user': "{{ git_user }}" }
    - { 'git_project': "{{ git_project }}" }
    - { 'git_ssh_port': "{{ git_ssh_port }}" }
    - { 'prod_host': "{{ prod_host }}" }
    - { 'prod_port': "{{ prod_port }}" }
    - { 'prod_user': "{{ prod_user }}" }


## Workstation ##

- name: General | Account Management | Provisioning Configuration | Workstation | Create
  blockinfile:
    path: "{{ wrk_file }}"
    block: |
      ;;;;;;; Available options - all require lowercase values ;;;;;;
      ;
      ;  enable : Set to true for system to be considered a workstation.
      ;
      ;  coding : Set to true for installation of code editors (VSCode, PyCharm, Android Studio)
      ;
      ; editing : Set to true for installation of Audio/Video editors (Shotcut, Audacity, OBS Stdio, GIMP)
      ;           Set to video for only the video related portions.
      ;             - Shotcut, OpenShot, OBS Studio, etc.
      ;           Set to audio for only the music related portions.
      ;             - Audacity (or similar), LMMS, VMPK, etc.
      ;
      ;  gaming : Set to true for installation of gaming software (Steam, Lutris)
      ;
      ;     rdp : Set to true for installation of RDP protocol
      ;             CURRENTLY FREEBSD-ONLY
      ;
      ;     vnc : Set to true for installation of VNC protocol
      ;             UNFINISHED/UNTESTED
      ;
      ; bsd_gpu : Set to [] to install GPU driver
      ;             Example: amdgpu
      ;
      ;  mobile : Set to true if not using an amd64 processor.
      ;             Also used to disable tasks not helpful for Pinephone.
      ;
      [global]
    marker: '; {mark} MANAGED BY ANSIBLE | Workstation Config'
    state: present
    create: yes
    backup: yes

- name: General | Account Management | Provisioning Configuration | Workstation | Load
  set_fact:
    workstation: "{{ lookup('ini', 'enable  file={{wrk_file}} default=false') }}"
    coding:      "{{ lookup('ini', 'coding  file={{wrk_file}} default=false') }}"
    editing:     "{{ lookup('ini', 'editing file={{wrk_file}} default=false') }}"
    gaming:      "{{ lookup('ini', 'gaming  file={{wrk_file}} default=false') }}"
    rdp:         "{{ lookup('ini', 'rdp     file={{wrk_file}} default=false') }}"
    vnc:         "{{ lookup('ini', 'vnc     file={{wrk_file}} default=false') }}"
    bsd_gpu:     "{{ lookup('ini', 'bsd_gpu file={{wrk_file}} default=false') }}"
    mobile:      "{{ lookup('ini', 'mobile  file={{wrk_file}} default=false') }}"

- name: General | Account Management | Provisioning Configuration | Workstation | List
  set_fact:
    provision_variables: "{{ provision_variables | combine(item) }}"
  loop:
    - { 'workstation': "{{ workstation }}" }
    - { 'coding': "{{ coding }}" }
    - { 'editing': "{{ editing }}" }
    - { 'gaming': "{{ gaming }}" }
    - { 'rdp': "{{ rdp }}" }
    - { 'vnc': "{{ vnc }}" }
    - { 'bsd_gpu': "{{ bsd_gpu }}" }
    - { 'mobile': "{{ mobile }}" }

# No longer mining, this is now considered deprecated.
### Miner ##
#
#- name: General | Account Management | Provisioning Configuration | Miner | Create
#  blockinfile:
#    path: "{{ mnr_file }}"
#    block: |
#      ;;;;;;; Available options - all require lowercase values ;;;;;;
#      ;
#      ;             enable : Set to true for system to be considered a miner.
#      ;
#      ;             amdgpu : Set to true for installation of AMDGPU-Pro drivers on Ubuntu (20.04 preferred)
#      ;
#      ;          nanominer : Set to true for installation of nanominer
#      ;
#      ;      nanominer_cpu : Set to coin code that you'd like the CPU to work on
#      ;                        Valid values: xmr
#      ;
#      ; nanominer_cpu_pool : Set to pool organization to use for CPU mining
#      ;                        Valid values: nanopool
#      ;
#      ;      nanominer_gpu : Set to coin code that you'd like the GPU to work on
#      ;                        Valid values: eth
#      ;
#      ;     nanominer_gpus : Set to comma list of GPU devices that should be used for nanominer
#      ;                        Example: 0,2,3
#      ;
#      ; nanominer_gpu_pool : Set to pool organization to use for GPU mining
#      ;                        Valid values: nanopool etherpool f2pool
#      ;
#      ;    eth_minhashrate : Set to minimum hashrate over 10 minutes before restarting. Can accept M for Million and K for thousand.
#      ;                        Example: 100K
#      ;
#      ;           ethminer : Set to true for installation of ethminer
#      ;                        UNFINISHED/UNTESTED
#      ;
#      ;             nvidia : Set to true for NVidia support in ethminer.
#      ;                        UNFINISHED/UNTESTED
#      ;
#      ;       xmr_stak_cpu : Set to true for istallation
#      ;                        UNFINISHED/UNTESTED
#      ;
#      [global]
#    marker: '; {mark} MANAGED BY ANSIBLE | Miner Config'
#    state: present
#    create: yes
#    backup: yes
#
#- name: General | Account Management | Provisioning Configuration | Miner | Load
#  set_fact:
#    miner:              "{{ lookup('ini', 'enable             file={{mnr_file}} default=false') }}"
#    amdgpu:             "{{ lookup('ini', 'amdgpu             file={{mnr_file}} default=false') }}"
#    nanominer:          "{{ lookup('ini', 'nanominer          file={{mnr_file}} default=false') }}"
#    nanominer_cpu:      "{{ lookup('ini', 'nanominer_cpu      file={{mnr_file}} default=false') }}"
#    nanominer_cpu_pool: "{{ lookup('ini', 'nanominer_cpu_pool file={{mnr_file}} default=false') }}"
#    nanominer_gpu:      "{{ lookup('ini', 'nanominer_gpu      file={{mnr_file}} default=false') }}"
#    nanominer_gpus:     "{{ lookup('ini', 'nanominer_gpus     file={{mnr_file}} default=false') }}"
#    nanominer_gpu_pool: "{{ lookup('ini', 'nanominer_gpu_pool file={{mnr_file}} default=false') }}"
#    eth_minhashrate:    "{{ lookup('ini', 'eth_minhashrate    file={{mnr_file}} default=false') }}"
#    ethminer:           "{{ lookup('ini', 'ethminer           file={{mnr_file}} default=false') }}"
#    nvidia:             "{{ lookup('ini', 'nvidia             file={{mnr_file}} default=false') }}"
#    xmr_stak_cpu:       "{{ lookup('ini', 'xmr_stak_cpu       file={{mnr_file}} default=false') }}"
#
#- name: General | Account Management | Provisioning Configuration | Miner | List
#  set_fact:
#    provision_variables: "{{ provision_variables | combine(item) }}"
#  loop:
#    - { 'miner': "{{ miner }}" }
#    - { 'amdgpu': "{{ amdgpu }}" }
#    - { 'nanominer': "{{ nanominer }}" }
#    - { 'nanominer_cpu': "{{ nanominer_cpu }}" }
#    - { 'nanominer_cpu_pool': "{{ nanominer_cpu_pool }}" }
#    - { 'nanominer_gpu': "{{ nanominer_gpu }}" }
#    - { 'nanominer_gpus': "{{ nanominer_gpus }}" }
#    - { 'nanominer_gpu_pool': "{{ nanominer_gpu_pool }}" }
#    - { 'eth_minhashrate': "{{ eth_minhashrate }}" }
#    - { 'ethminer': "{{ ethminer }}" }
#    - { 'nvidia': "{{ nvidia }}" }
#    - { 'xmr_stak_cpu': "{{ xmr_stak_cpu }}" }


## Server ##

- name: General | Account Management | Provisioning Configuration | Server | Create
  blockinfile:
    path: "{{ srv_file }}"
    block: |
      ;;;;;;; Parameters ;;;;;;
      ;
      ;     enable : Set to true for system to be considered a server.
      ;
      ;     domain : Set to the domain which gives the server its FQDN.
      ;                Example: hyperling.com
      ;
      ; onlyoffice : Set to true for this server to be configured as an OnlyOffice server.
      ;
      ;    grafana : Set to true for this server to be configured as an Grafana web server.
      ;
      ;  influxdb1 : Set to true for this server to be configured as an Influx 1 DB server.
      ;
      ;  influxdb2 : Set to true for this server to be configured as an Influx 2 DB + web server.
      ;
      ;    certbot : Set to true to add cron job for `certbot renew`.
      ;
      ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
      ;
      ; Never got these fully working or did not understand how to use them.
      ;
      ;       hugo : Set to true to install HUGO static website generator.
      ;
      ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
      [global]
    marker: '; {mark} MANAGED BY ANSIBLE | Server Config'
    state: present
    create: yes
    backup: yes

- name: General | Account Management | Provisioning Configuration | Server | Load
  set_fact:
    server:     "{{ lookup('ini', 'enable     file={{srv_file}} default=false') }}"
    domain:     "{{ lookup('ini', 'domain     file={{srv_file}} default=hyperling.com') }}"
    onlyoffice: "{{ lookup('ini', 'onlyoffice file={{srv_file}} default=false') }}"
    grafana:    "{{ lookup('ini', 'grafana    file={{srv_file}} default=false') }}"
    influxdb1:  "{{ lookup('ini', 'influxdb1  file={{srv_file}} default=false') }}"
    influxdb2:  "{{ lookup('ini', 'influxdb2  file={{srv_file}} default=false') }}"
    certbot:    "{{ lookup('ini', 'certbot    file={{srv_file}} default=false') }}"
    hugo:       "{{ lookup('ini', 'hugo       file={{srv_file}} default=false') }}"

- name: General | Account Management | Provisioning Configuration | Server | List
  set_fact:
    provision_variables: "{{ provision_variables | combine(item) }}"
  loop:
    - { 'server': "{{ server }}" }
    - { 'domain': "{{ domain }}" }
    - { 'onlyoffice': "{{ onlyoffice }}" }
    - { 'grafana': "{{ grafana }}" }
    - { 'influxdb1': "{{ influxdb1 }}" }
    - { 'influxdb2': "{{ influxdb2 }}" }
    - { 'certbot': "{{ certbot }}" }
    - { 'hugo': "{{ hugo }}" }


## Exit if not turned on yet ##

- name: General | Account Management | Provisioning Configuration | Exit Check
  shell: echo "Please configure the config files in {{ prov_dir }} then try again." && exit 1
  when: provision != true


## Display values ##

- name: General | Account Management | Provisioning Configuration | Print
  debug:
    var: provision_variables