#!/usr/bin/bash
# Push/Pull VM-Disk-Image and Infos from server
set -eu

show_help() {
    cat << EOF >&2
Usage: $(basename "$0") command [args]"
command:
  push_file
  get_file
  get_image
  delete_outdated_image
EOF
}

get_torrent() {
    if [[ ! -f "${VM_SYSDIR}/${VM_NAME}.qcow2.torrent" ]]; then
      echo "No torrent-File found"
      exit 1
    fi
    lockfile="/tmp/sync-vm-${VM_NAME}.lock"
    if ! flock -n "$lockfile" echo "try to acquire lock"; then
      echo torrent seems to be in process.
      echo waiting for completion ...
      flock -w 3600 "$lockfile" echo "...completed"
      sleep 5
    else
      (
        if ! flock -n 200; then
	  echo "failed to acquire lock"
	  echo "Bitte noch einmal starten."
          echo "Beliebige Taste zum Beenden."
	  read -n 1
	  exit 1
        fi
	# stop aria2-seeding if running
        sudo vm-aria2 stop "${VM_NAME}"
        cd "${VM_SYSDIR}"
	# get image
	aria2c --seed-time=0 --dht-file-path=$DHTDAT \
	       --dht-entry-point="${SEEDBOX_HOST}:${SEEDBOX_PORT}" \
	       "${VM_SYSDIR}/${VM_NAME}.qcow2.torrent"
	# and seed
        sudo vm-aria2 start "${VM_NAME}"
        if ! flock -u 200; then
	  echo failed to drop lock
	  exit 1
	fi
      ) 200>"$lockfile"
    fi
}


get_image_size() {
    torrentfile=$1
    length=$(aria2c -S "${torrentfile}"  | grep "Total Length" | \
		 sed -E -e 's/.*\(([0-9,]*)\)/\1/' -e 's/,//g')
    echo "$length"
}

delete_outdated_image() {
    cd "${VM_SYSDIR}"
    qcowsize=$(stat -c%s "${FILENAME}")
    if [[ -f "${FILENAME}.torrent" ]] && [[ "${qcowsize}" != $(get_image_size "${FILENAME}.torrent") ]]; then
      sudo vm-aria2 stop "${FILENAME%.qcow2}"
      rm -f "${FILENAME}"
    fi
}

get_file() {
   cd "${VM_SYSDIR}"
   curl --fail --noproxy ${SEEDBOX_HOST} -o "${FILENAME}" \
	"http://${SEEDBOX_HOST}/aria2/${FILENAME}" || echo "File not found on seedbox"
}

push_file() {
   cd "${VM_SYSDIR}"
   uploadseed --server "${SEEDBOX_HOST}:${SEEDBOX_RPC_PORT}" --dht-port "${SEEDBOX_PORT}" \
	      --pwdfile "${SEEDBOX_PWFILE}" --no-cert "${FILENAME}"
}

########################

if [[ "$(id -nu)" != "lmnsynci" ]]; then
    echo "$(basename "$0") must be run as lmnsynci user"
    show_help
    exit 1
fi

source /etc/lmn/vm.conf

while getopts ':' OPTION; do
    case "$OPTION" in
        ?)
            show_help
            exit 1
            ;;
    esac
done

shift "$((OPTIND -1))"

# if less than one arguments supplied, display usage
if [[  $# -lt 1 ]]; then
    show_help
    exit 1
fi

command=$1
shift

case "$command" in
   push_file)
      for FILENAME in "$@"; do
	 push_file
      done
      ;;
   get_file)
      for FILENAME in "$@"; do
	 get_file
      done
      ;;
   get_image)
      for VM_NAME in "$@"; do
	 get_torrent
      done
      ;;
   delete_outdated_image)
      for FILENAME in "$@"; do
         delete_outdated_image
      done
      ;;
   *)
      show_help
      exit 1
      ;;
esac