#cloud-config
hostname: hdmistat-test
manage_etc_hosts: true

users:
  - name: ubuntu
    groups: [adm, audio, cdrom, dialout, dip, floppy, lxd, netdev, plugdev, sudo, video]
    sudo: ALL=(ALL) NOPASSWD:ALL
    shell: /bin/bash
    lock_passwd: false
    plain_text_passwd: ubuntu

packages:
  - git
  - build-essential
  - wget
  - fbset
  - rsync

# Enable SSH (already enabled in cloud images)
ssh_pwauth: true

# Disable all package updates
package_update: false
package_upgrade: false
package_reboot_if_required: false

write_files:
  - path: /tmp/install-go.sh
    permissions: '0755'
    content: |
      #!/bin/bash
      set -e
      GO_VERSION="1.24.4"
      wget -q -O /tmp/go.tar.gz "https://go.dev/dl/go${GO_VERSION}.linux-arm64.tar.gz"
      sudo rm -rf /usr/local/go
      sudo tar -C /usr/local -xzf /tmp/go.tar.gz
      echo 'export PATH=$PATH:/usr/local/go/bin' | sudo tee -a /etc/profile
      echo 'export GOPATH=$HOME/go' | sudo tee -a /etc/profile
      echo 'export PATH=$PATH:$GOPATH/bin' | sudo tee -a /etc/profile

  - path: /tmp/setup-hdmistat.sh
    permissions: '0755'
    content: |
      #!/bin/bash
      set -e
      export HOME=/root
      export PATH=$PATH:/usr/local/go/bin
      export GOPATH=$HOME/go
      export PATH=$PATH:$GOPATH/bin
      export GOCACHE=$HOME/.cache/go-build
      mkdir -p $GOCACHE
      
      # Mount hdmistat source
      mkdir -p /mnt/hdmistat
      mount -t 9p -o trans=virtio,version=9p2000.L hdmistat /mnt/hdmistat || echo "Failed to mount hdmistat source"
      
      # Build hdmistat
      if [ -d "/mnt/hdmistat" ]; then
          echo "Copying hdmistat source to /tmp..."
          rsync -av --exclude='test' --exclude='.git' /mnt/hdmistat/ /tmp/hdmistat-build/
          cd /tmp/hdmistat-build
          echo "Building hdmistat debug build in /tmp..."
          # Build debug version with VCS disabled
          make debug GOFLAGS="-buildvcs=false"
          cp hdmistat-debug /usr/local/bin/hdmistat
          chmod +x /usr/local/bin/hdmistat
          echo "hdmistat binary installed to /usr/local/bin/"
      fi
      
      # Install hdmistat as systemd service (handles everything)
      if [ -f /usr/local/bin/hdmistat ]; then
          /usr/local/bin/hdmistat install
          
          # Check service status
          systemctl status hdmistat --no-pager || true
          
          # Show logs
          journalctl -u hdmistat -n 20 --no-pager || true
      else
          echo "ERROR: hdmistat binary not found!"
      fi
      
      # Enable framebuffer
      echo "fbcon=map:10" | tee -a /etc/default/grub
      update-grub || true

runcmd:
  - /tmp/install-go.sh
  - /tmp/setup-hdmistat.sh
  - echo "hdmistat setup complete! Service installed and started by 'hdmistat install'"
  - sleep 5
  - echo "Checking hdmistat service status..."
  - systemctl status hdmistat --no-pager || true
  - echo "Recent hdmistat logs:"
  - journalctl -u hdmistat -n 50 --no-pager || true
  - echo "Checking framebuffer device..."
  - ls -la /dev/fb* || true
  - fbset -i || true

final_message: "hdmistat test environment ready!"
