#!/usr/bin/env bash
# Profile: sc-build-machine (vulcan)
# Role: Arch Linux build machine — compiles AxiomFlow artifacts, runs scheduled
#       jobs, deploys to hermes. Intentionally different distro from Debian servers.
# Distro: Arch Linux cloud image

DOMAIN="sc-build-machine"
HOSTNAME="vulcan"
RAM_MB=768
VCPUS=2
DISK_SIZE="10G"
GRAPHICS="vnc"
BASE_URL="https://geo.mirror.pkgbuild.com/images/latest/Arch-Linux-x86_64-cloudimg.qcow2"
BASE_IMAGE="$SC_BASE_DIR/Arch-Linux-x86_64-cloudimg.qcow2"

generate_user_data() {
cat <<EOF
#cloud-config
hostname: ${HOSTNAME}
fqdn: ${HOSTNAME}.axiomworks.internal
manage_etc_hosts: false
ssh_pwauth: false
users:
  - default
  - name: player
    gecos: Axiom Works Builder
    groups: [wheel]
    shell: /bin/bash
    sudo: ["ALL=(ALL) NOPASSWD:ALL"]
    ssh_authorized_keys:
      - ${PUBKEY}
write_files:
  - path: /etc/hosts
    owner: root:root
    permissions: '0644'
    content: |
      127.0.0.1   localhost
      127.0.1.1   vulcan vulcan.axiomworks.internal
      ${GAME_HOST_IP}   axiomworks.internal portal.axiomworks.internal
      10.42.0.40  hermes hermes.axiomworks.internal
  - path: /etc/sudoers.d/99-player
    owner: root:root
    permissions: '0440'
    content: |
      player ALL=(ALL) NOPASSWD:ALL
  - path: /etc/sysctl.d/99-sc-vulcan.conf
    owner: root:root
    permissions: '0644'
    content: |
      vm.swappiness=10
      vm.vfs_cache_pressure=50
      vm.dirty_ratio=25
      vm.dirty_background_ratio=5
      net.ipv6.conf.all.disable_ipv6=1
      net.ipv6.conf.default.disable_ipv6=1
  - path: /home/player/.bashrc
    owner: root:root
    permissions: '0644'
    content: |
      [ -z "\$PS1" ] && return
      export PATH=/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/bin
      export TERM=xterm-256color
      export EDITOR=vim
      PS1='\[\e[0;35m\]\u@\h\[\e[0m\]:\[\e[0;34m\]\w\[\e[0m\]\$ '
      HISTSIZE=5000
      HISTFILESIZE=10000
      HISTCONTROL=ignoredups:erasedups
      shopt -s histappend
      alias ll='ls -lh --color=auto'
      alias la='ls -lha --color=auto'
      alias grep='grep --color=auto'
      alias ..='cd ..'
      alias pacs='pacman -Ss'
      alias paci='sudo pacman -S'
      alias pacq='pacman -Qi'
      if [ -f /usr/share/bash-completion/bash_completion ]; then
        . /usr/share/bash-completion/bash_completion
      fi
  - path: /home/player/.bash_profile
    owner: root:root
    permissions: '0644'
    content: |
      [[ -f ~/.bashrc ]] && . ~/.bashrc
runcmd:
  - pacman -Sy --noconfirm archlinux-keyring
  - pacman -Su --noconfirm
  - pacman -S --noconfirm --needed sudo openssh qemu-guest-agent base-devel git inetutils iproute2 curl wget rsync vim nano htop python python-pip jq less tree unzip tcpdump lsof strace bind-tools openbsd-netcat bash-completion
  - systemctl enable qemu-guest-agent sshd
  - systemctl start qemu-guest-agent sshd
  - mkdir -p /srv/repo /srv/builds /var/log/axiomworks
  - printf 'vulcan — AxiomFlow build machine\n' > /srv/repo/README.txt
  - dd if=/dev/zero of=/swapfile bs=1M count=1024 status=progress
  - chmod 600 /swapfile
  - mkswap /swapfile
  - swapon /swapfile
  - echo '/swapfile none swap sw 0 0' >> /etc/fstab
  - sysctl -p /etc/sysctl.d/99-sc-vulcan.conf
  - chown -R player:player /home/player /srv/repo /srv/builds
  - systemctl disable ModemManager || true
  - systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target
final_message: "Vulcan build machine is ready."
EOF
}
