#!/bin/bash

## Copyright (C) 2019 - 2025 ENCRYPTED SUPPORT LLC <adrelanos@whonix.org>
## See the file COPYING for copying conditions.

set -x
set -e

## {{ Taken from qemu-system-common.postinst.
# Add the kvm group unless it's already there
if ! getent group kvm >/dev/null; then
   addgroup --quiet --system kvm || true
fi
## }} Taken from qemu-system-common.postinst.

## {{ Taken from libvirt-bin.postinst.
if ! getent group libvirt >/dev/null; then
   addgroup --system libvirt
fi
## }} Taken from libvirt-bin.postinst.

## Existence of account "user" is not guaranteed at this point.
## XXX: Or is it?
adduser user kvm >/dev/null || true
adduser user libvirt >/dev/null || true

## Create shared directory and adjust permissions
mkdir --parents /mnt/gateway-shared
mkdir --parents /mnt/workstation-shared
chmod 777 /mnt/gateway-shared
chmod 777 /mnt/workstation-shared

## TODO: proper error handling. '|| true' can probably be removed.

virsh -c qemu:///session net-autostart "default" || true
virsh -c qemu:///session net-start "default" || true
virsh -c qemu:///session net-define "/usr/share/libvirt-dist/xml/Whonix-External.xml" || true
virsh -c qemu:///session net-define "/usr/share/libvirt-dist/xml/Whonix-Internal.xml" || true
virsh -c qemu:///session net-autostart "Whonix-External" || true
virsh -c qemu:///session net-start "Whonix-External" || true
virsh -c qemu:///session net-autostart "Whonix-Internal" || true
virsh -c qemu:///session net-start "Whonix-Internal" || true

## Doing the following in a temporary directory to avoid modified files should
## this be interrupted in the middle.
temp_dir="$(mktemp --directory)"
cp -r /usr/share/libvirt-dist/xml "$temp_dir"

if virsh capabilities | grep "<domain type='kvm'>" ; then
   true "OK: found KVM"
else
   ## replace the 'kvm' domain type with 'qemu'
   search="<domain type='kvm'>"
   replace="<domain type='qemu'>"
   str_replace "$search" "$replace" "$temp_dir/xml/Whonix-Gateway.xml"
   str_replace "$search" "$replace" "$temp_dir/xml/Whonix-Workstation.xml"

   search="<cpu mode='host-passthrough'/>"
   replace=""
   str_replace "$search" "$replace" "$temp_dir/xml/Whonix-Gateway.xml"
   str_replace "$search" "$replace" "$temp_dir/xml/Whonix-Workstation.xml"

   ## https://forums.whonix.org/t/whonix-host-operating-system/3931/251
   search="<pvspinlock state='on'/>"
   replace=""
   str_replace "$search" "$replace" "$temp_dir/xml/Whonix-Gateway.xml"
   str_replace "$search" "$replace" "$temp_dir/xml/Whonix-Workstation.xml"

   ## https://forums.whonix.org/t/whonix-host-operating-system/3931/284
   search="<vcpu placement='static' cpuset='0'>1</vcpu>"
   replace=""
   str_replace "$search" "$replace" "$temp_dir/xml/Whonix-Gateway.xml"

   ## https://forums.whonix.org/t/whonix-host-operating-system/3931/284
   search="<vcpu placement='static' cpuset='1'>1</vcpu>"
   replace=""
   str_replace "$search" "$replace" "$temp_dir/xml/Whonix-Workstation.xml"
fi

test -f "$temp_dir/xml/Whonix-Gateway.xml"
test -f "$temp_dir/xml/Whonix-Workstation.xml"

virsh -c qemu:///session define "$temp_dir/xml/Whonix-Gateway.xml" || true
virsh -c qemu:///session define "$temp_dir/xml/Whonix-Workstation.xml" || true

virt-xml "Whonix-Gateway" --add-device --filesystem source=/mnt/gateway-shared,target=shared,type=mount,accessmode=mapped || true
virt-xml "Whonix-Workstation" --add-device --filesystem source=/mnt/workstation-shared,target=shared,type=mount,accessmode=mapped || true

mkdir --parents /var/lib/libvirt-dist
touch /var/lib/libvirt-dist/install.done
