# 1. Base Image
FROM ubuntu:20.04

# 2. Prevent interactive prompts
ENV DEBIAN_FRONTEND=noninteractive

# 3. Install dependencies
RUN apt-get update && \
    apt-get install -y qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils virt-manager wget unzip gnupg kmod openssh-client net-tools

# 4. Install HashiCorp GPG key and repository
RUN wget -O- https://apt.releases.hashicorp.com/gpg | gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg && \
    echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(grep -oP '(?<=VERSION_CODENAME=).*' /etc/os-release) main" | tee /etc/apt/sources.list.d/hashicorp.list

# 5. Install build dependencies for vagrant plugins
RUN apt-get update && \
    apt-get install -y build-essential libvirt-dev ruby-dev

# 6. Install Vagrant from .deb package
RUN wget https://releases.hashicorp.com/vagrant/2.4.6/vagrant_2.4.6-1_amd64.deb && \
    apt-get install -y ./vagrant_2.4.6-1_amd64.deb

# 7. Install vagrant-libvirt plugin
RUN vagrant plugin install vagrant-libvirt

# Add root to libvirt group and configure qemu.conf
RUN usermod -aG libvirt $(whoami) && \
    sed -i 's/#user = "root"/user = "root"/' /etc/libvirt/qemu.conf && \
    sed -i 's/#group = "root"/group = "root"/' /etc/libvirt/qemu.conf

# 8. Download Vagrant Box
RUN vagrant box add peru/windows-10-enterprise-x64-eval --provider libvirt

# 9. Create and set working directory
RUN mkdir -p /opt/win10
WORKDIR /opt/win10

# 10. Copy Vagrantfile
COPY Vagrantfile /opt/win10/

# 11. Create and provision the VM during the build
# This step will download the Windows image and provision it.
# It can take a significant amount of time.
# RUN /usr/sbin/libvirtd -d && /usr/sbin/virtlogd -d && VAGRANT_DEFAULT_PROVIDER=libvirt vagrant up

# 12. Copy startup script
COPY startup.sh /usr/local/bin/startup.sh
RUN chmod +x /usr/local/bin/startup.sh

EXPOSE 3389 8090

# 13. Set the entrypoint
CMD ["/usr/local/bin/startup.sh"] 
