#!/usr/bin/bash
#
# Backup virtual system
# Copyright (C) 2013-2024 by Thomas Dreibholz
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Contact: dreibh@simula.no

# Bash options:
set -e


# ###### Main program #######################################################
if [ $# -lt 2 ] ; then
   echo >&2 "Usage: $0 VM_name VM_export_file [temporary_directory]"
   exit 1
fi

NORNET_DIRECTORY="/etc/nornet"
MACHINE_NAME="$1"
EXPORT_FILE="$2"

# Check whether NorNet directory is existing
if [ -e $NORNET_DIRECTORY/nornetapi-config ] ; then
   . $NORNET_DIRECTORY/nornetapi-config
elif [ -e ./nornetapi-config ] ; then
   . ./nornetapi-config
fi
if [ "$NorNet_LocalNode_NorNetUser" = "" ] ; then
   NorNet_LocalNode_NorNetUser="nornetpp"
fi

TEMP_DIRECTORY_BASE="$3"
if [ "$TEMP_DIRECTORY_BASE" = "" ] ; then
   TEMP_DIRECTORY_BASE=`dirname $EXPORT_FILE`
fi
TEMP_DIRECTORY=`sudo -u $NorNet_LocalNode_NorNetUser mktemp -d "--tmpdir=$TEMP_DIRECTORY_BASE" -t "vsystem-backup-tmp.XXXXXXXXXX"`
TEMP_FILE="$TEMP_DIRECTORY/`basename $EXPORT_FILE`"


# ====== Shut down machine, if necessary ====================================
initialMachineState=`Check-VSystem "$MACHINE_NAME"`
if [ "$initialMachineState" != "stopped" -a "$initialMachineState" != "aborted" -a "$initialMachineState" != "poweroff" ] ; then
   echo "`env LANG=C date +%FT%H:%M:%S`: $MACHINE_NAME is in state \"$initialMachineState\" -> stopping ..."
   service nornet-server stop "$MACHINE_NAME" || true
fi


# ====== Export to temporary file ===========================================
echo "`env LANG=C date +%FT%H:%M:%S`: Exporting $MACHINE_NAME ..."
echo "Temporary directory: $TEMP_DIRECTORY"
echo "Temporary file:      $TEMP_FILE"
rm -f "$EXPORT_FILE" "$TEMP_FILE"
result=1
sudo -u $NorNet_LocalNode_NorNetUser \
   VBoxManage export "$MACHINE_NAME" --output "$TEMP_FILE" --manifest --vsys 0 \
      --product "$MACHINE_NAME" --producturl "https://www.nntb.no" \
      --vendor "Simula Research Laboratory" --vendorurl "https://www.simula.no" \
      --version "1.0" && \
result=0 || true


# ====== Restart machine, if it was running =================================
if [ "$initialMachineState" != "stopped" -a "$initialMachineState" != "aborted" -a "$initialMachineState" != "poweroff" ] ; then
   echo "`env LANG=C date +%FT%H:%M:%S`: $MACHINE_NAME was in state \"$initialMachineState\" -> restarting ..."
   service nornet-server start "$MACHINE_NAME" || true
fi


# ====== On success, move export file to given name =========================
if [ $result -eq 0 ] ; then
   result=1
   sudo -u $NorNet_LocalNode_NorNetUser nice mv "$TEMP_FILE" "$EXPORT_FILE" && result=0 || true
fi

if [ $result -eq 0 ] ; then
   echo "`env LANG=C date +%FT%H:%M:%S`: $MACHINE_NAME successfully backed up!"
else
   echo "`env LANG=C date +%FT%H:%M:%S`: Backup of $MACHINE_NAME FAILED!"
fi


# ====== Clean up ===========================================================
sudo -u $NorNet_LocalNode_NorNetUser rm -f "$TEMP_FILE" || true
sudo -u $NorNet_LocalNode_NorNetUser rmdir "$TEMP_DIRECTORY" || true

exit $result
