#!/usr/bin/bash

set -eu

# if less than one arguments supplied, display usage
if [[  $# -ne 2 ]]; then
    echo "This script takes as input the name of the VM " >&2
    echo "Usage: $0 [start|stop] vm_name" >&2
    exit 1
fi

COMMAND="$1"
VM_NAME="$2"

source /etc/lmn/vm.conf

if [[ "${COMMAND}" = "start" ]]; then
   systemd-run --unit=aria2-"${VM_NAME}" \
                --slice=system-aria2 \
                --uid="$(id -u lmnsynci)" \
                --gid="$(id -g lmnsynci)" \
                --nice=19 \
                --working-directory="${VM_SYSDIR}" \
                --collect \
                --property=Type=exec \
                --property=SuccessExitStatus=1 \
                aria2c --bt-hash-check-seed=true --check-integrity=true --seed-ratio=0.0 \
		--dht-entry-point="${SEEDBOX_HOST}:${SEEDBOX_PORT}" \
		--dht-file-path=$DHTDAT \
		"${VM_SYSDIR}/${VM_NAME}.qcow2.torrent"
elif [[ "${COMMAND}" = "stop" ]] && systemctl is-active "aria2-${VM_NAME}.service"; then
    systemctl stop "aria2-${VM_NAME}.service"
fi
