#!/bin/bash
# Intialize libvirt config storage
# Invoked by emhttp after mounting libvirt loopback but before starting libvirt.

# run & log functions
. /etc/rc.d/rc.runlog


# Sync domain data if IMAGE_FILE and OLD_IMAGE_FILE differ
DOMAIN_CFG=/boot/config/domain.cfg

# Read values from domain.cfg safely (no eval)
IMAGE_FILE=$(grep -E '^IMAGE_FILE=' "$DOMAIN_CFG" | head -1 | cut -d= -f2-)
OLD_IMAGE_FILE=$(grep -E '^OLD_IMAGE_FILE=' "$DOMAIN_CFG" | head -1 | cut -d= -f2-)

# Remove quotes
IMAGE_FILE="${IMAGE_FILE%\"}"
IMAGE_FILE="${IMAGE_FILE#\"}"
OLD_IMAGE_FILE="${OLD_IMAGE_FILE%\"}"
OLD_IMAGE_FILE="${OLD_IMAGE_FILE#\"}"

# Proceed only if both variables are set and OLD_IMAGE_FILE exists
if [ -n "$IMAGE_FILE" ] && [ -n "$OLD_IMAGE_FILE" ] && [ "$IMAGE_FILE" != "$OLD_IMAGE_FILE" ]; then
  if [ ! -e "$OLD_IMAGE_FILE" ]; then
    log "OLD_IMAGE_FILE not found: $OLD_IMAGE_FILE — skipping sync"
  else
    log "IMAGE_FILE and OLD_IMAGE_FILE differ, syncing..."

    TMP_MNT=/etc/libvirt-sync
    IMG_FILE_NAME=$(basename "$IMAGE_FILE")
    OLD_IMG_FILE_NAME=$(basename "$OLD_IMAGE_FILE")
    TIMESTAMP=$(date +%Y%m%d-%H%M%S)

    if [[ "$OLD_IMAGE_FILE" == *.img ]]; then
      # Backup image before mounting
      BACKUP_PATH="${OLD_IMAGE_FILE%.img}.bak-${TIMESTAMP}.img"
      log "Creating backup of OLD_IMAGE_FILE: $BACKUP_PATH"
      cp -p "$OLD_IMAGE_FILE" "$BACKUP_PATH"

      log "Mounting $OLD_IMAGE_FILE to $TMP_MNT"
      mkdir -p "$TMP_MNT"
      if ! mount "$OLD_IMAGE_FILE" "$TMP_MNT"; then
        log "ERROR: Failed to mount $OLD_IMAGE_FILE"
        rm -rf "$TMP_MNT"
        exit 1
      fi
      log "Copying full contents from image to directory $IMAGE_FILE"
      if ! rsync -a --exclude="$OLD_IMG_FILE_NAME" "$TMP_MNT/" "$IMAGE_FILE/"; then
        log "WARNING: rsync encountered errors"
      fi
      umount "$TMP_MNT" || log "WARNING: Failed to unmount $TMP_MNT"
      rmdir "$TMP_MNT" 2>/dev/null
    elif [[ "$IMAGE_FILE" == *.img ]]; then
      log "Mounting $IMAGE_FILE to $TMP_MNT"
      mkdir -p "$TMP_MNT"
      mount "$IMAGE_FILE" "$TMP_MNT"
      log "Copying full contents from directory $OLD_IMAGE_FILE to image"
      rsync -a --exclude="$IMG_FILE_NAME" --exclude='*.bak-*.img' "$OLD_IMAGE_FILE/" "$TMP_MNT/"
      umount "$TMP_MNT"
    else
      log "Both IMAGE_FILE and OLD_IMAGE_FILE are directories, copying full contents"
      rsync -a --exclude="$IMG_FILE_NAME" "$OLD_IMAGE_FILE/" "$IMAGE_FILE/"
    fi

    # Update OLD_IMAGE_FILE in domain.cfg
    log "Updating OLD_IMAGE_FILE in $DOMAIN_CFG"
    sed -i "s|^OLD_IMAGE_FILE=.*|OLD_IMAGE_FILE=\"$IMAGE_FILE\"|" "$DOMAIN_CFG"
  fi
else
  log "IMAGE_FILE and OLD_IMAGE_FILE match, or one is unset — skipping sync"
fi


# missing qemu directory would indicate new libvirt image file created
if [ ! -d /etc/libvirt/qemu ]; then
  log "initializing /etc/libvirt"
  # initialize with default settings
  cp -rp /etc/libvirt-/* /etc/libvirt
  # check if libvirt image file exists on USB flash
  OLD_IMAGE=/boot/config/plugins/dynamix.kvm.manager/domain.img
  if [ ! -f $OLD_IMAGE ]; then
    OLD_IMAGE=/boot/config/plugins/virtMan/virtMan.img
    if [ ! -f $OLD_IMAGE ]; then
      OLD_IMAGE=
    fi
  fi
  if [ "$OLD_IMAGE" != "" ]; then
    # found existing image, use qemu config from there
    rm -rf /etc/libvirt/qemu/*
    mount $OLD_IMAGE /etc/libvirt-
    cp -rp /etc/libvirt-/qemu/* /etc/libvirt/qemu
    if [ -f /etc/libvirt-/hooks/qemu ]; then
      cp -p /etc/libvirt-/hooks/qemu /etc/libvirt/hooks/qemu
    fi
    umount /etc/libvirt-
  fi
fi

# if vfio-pci bind error, prevent autostart
if [ -s /var/log/vfio-pci-errors ]; then
  mkdir -p /run/libvirt/qemu
  echo "vfio-pci bind error" > /run/libvirt/qemu/autostarted
  /usr/local/emhttp/webGui/scripts/notify -e "VM Autostart disabled" -s "vfio-pci-errors " -d "VM Autostart disabled due to vfio-bind error" -m "Please review /var/log/vfio-pci-errors" -i "alert" -l "/VMs"
fi

# Copy XML from VM Directories to QEMU directory/
/usr/local/emhttp/plugins/dynamix.vm.manager/scripts/libvirtrestore
#
