#!/bin/bash

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

set -x
set -e

true "INFO: Currently running script: $BASH_SOURCE $@"

MYDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

cd "$MYDIR"
cd ..
cd help-steps

source pre
source colors
source variables

copy-vm-files(){
   exception_handler_setup "exception_handler_unchroot_unmount" ERR INT TERM

   true "${green}INFO: Skipping $BASH_SOURCE, because not yet implemented.${reset}"
   return 0
   ## TODO: port to VirtualBox

   "$dist_source_help_steps_folder/mount-raw" "$@"

   ## here we copy the Gateway and Workstation .qcow2 files into the host VM
   ## previously I ran qemu-img convert -f qcow2 -O qcow2 on the host to reduce their size
   ## a final, clean build step should also take care of building/extracting/shrinking these files automatically
   ## of course it would also use variables instead of version numbers
   ## we also apply correct file permissions

   ## 'cp --sparse=always' did not work.
   ## https://forums.whonix.org/t/whonix-host-operating-system/3931/202
   #cp --sparse=always "$copy_vms_into_raw_file_one" "$CHROOT_FOLDER/var/lib/libvirt/images/Whonix-Gateway.qcow2"
   #cp --sparse=always "$copy_vms_into_raw_file_two" "$CHROOT_FOLDER/var/lib/libvirt/images/Whonix-Workstation.qcow2"

   ## Therefore using qemu-img.
   $SUDO_TO_ROOT qemu-img convert -f qcow2 -O qcow2 "$copy_vms_into_raw_file_one" "$CHROOT_FOLDER/var/lib/libvirt/images/Whonix-Gateway.qcow2"
   $SUDO_TO_ROOT qemu-img convert -f qcow2 -O qcow2 "$copy_vms_into_raw_file_two" "$CHROOT_FOLDER/var/lib/libvirt/images/Whonix-Workstation.qcow2"

   ## TODO: qemu-img parameters?
#    $SUDO_TO_ROOT qemu-img \
#       convert \
#          -p \
#          -O qcow2 \
#          -o extended_l2=on \
#          -o cluster_size=128k \
#          -o preallocation=metadata \
#          "$binary_image_raw_file" \
#          "$binary_image_qcow2_file"

   ## Debugging.
   $SUDO_TO_ROOT qemu-img info "$CHROOT_FOLDER/var/lib/libvirt/images/Whonix-Gateway.qcow2"
   $SUDO_TO_ROOT qemu-img info "$CHROOT_FOLDER/var/lib/libvirt/images/Whonix-Workstation.qcow2"

   ## https://forums.whonix.org/t/whonix-host-operating-system/3931/180
   chroot_run chmod --verbose --recursive o-r   "/var/lib/libvirt/images/Whonix-Gateway.qcow2"
   chroot_run chmod --verbose --recursive o-r   "/var/lib/libvirt/images/Whonix-Workstation.qcow2"
   chroot_run chmod --verbose --recursive ugo-w "/var/lib/libvirt/images/Whonix-Gateway.qcow2"
   chroot_run chmod --verbose --recursive ugo-w "/var/lib/libvirt/images/Whonix-Workstation.qcow2"

   chroot_run chown --verbose --recursive libvirt-qemu:libvirt-qemu "/var/lib/libvirt/images/Whonix-Gateway.qcow2"
   chroot_run chown --verbose --recursive libvirt-qemu:libvirt-qemu "/var/lib/libvirt/images/Whonix-Workstation.qcow2"

   "$dist_source_help_steps_folder/unmount-raw" "$@"

   true
}

main() {
   exception_handler_setup "exception_handler_unchroot_unmount" ERR INT TERM

   if [ "$build_dry_run" = "true" ]; then
      true "${bold}${cyan}INFO: dry-run, skipping $BASH_SOURCE. ${reset}"
      return 0
   fi

   if [ "$dist_build_type_long" = "kicksecure" ]; then
      true "INFO: Skipping $0, because dist_build_type_long kicksecure does not need to copy VM images into the build, ok."
      return 0
   fi

   if [ "$dist_build_iso" = "true" ]; then
      copy-vm-files "$@"
   else
      true "${green}INFO: Skipping $BASH_SOURCE, because dist_build_iso is not set to 'true'.${reset}"
      return 0
   fi
}

main "$@"
