#!/bin/sh
#
# linbo_upload
# thomas@linuxmuster.net
# 20260520
#

usage(){
  local RC="$1"
  echo
  echo "Uploads a file from the cache to the server. Linbo user password is necessary."
  echo
  echo "Usage:"
  echo "  linbo_upload <password> <file> | [help]"
  echo
  echo "For compatibility reasons legacy options are also accepted:"
  echo "  linbo_upload <server> <user> <password> <cache> <file>"
  echo
  exit "$RC"
}

# print help
[ -z "$1" -o "$1" = "help" ] && usage 0

# get environment
source /usr/share/linbo/shell_functions

# do not execute in localmode
if localmode; then
  echo "Local mode detected, no upload possible!"
  exit 0
fi

# check params with legacy compatibility (serverip user password cache file)
if [ -z "$1" ] || validip "$1"; then shift; fi
[ -z "$1" -o "$1" = "linbo" ] && shift
LINBOUSER="linbo"
export RSYNC_PASSWORD="$1" ; shift
[ -z "$1" -o -b "$1" ] && shift
FILE="$1"

echo "### $timestamp $(basename "$0") $FILE ###"

linbo_mountcache &> /dev/null || exit 1
cd /cache

RC="0"
if [ -e "$FILE" ]; then
  filelist="$FILE"
  ext="${FILE##*.}"
  # expand filelist in case of qcow2|qdiff file
  #echo "### filetype: $ext ###"
  if [ "$ext" = "qcow2" -o "$ext" = "qdiff" ]; then
    imagebase="${FILE%.$ext}"
    subdir="images/$imagebase/"
    for item in info desc hash torrent; do
      [ -s "${FILE}.${item}" ] && filelist="$filelist ${FILE}.${item}"
    done
  fi
  echo "Uploading $filelist to $LINBOSERVER ..."
  for item in $filelist; do
    interruptible rsync --skip-compress="$RSYNC_SKIP_COMPRESS" --log-file=/tmp/rsync.log --progress -Ha $RSYNC_PERMISSIONS --partial "$item" "$LINBOUSER@$LINBOSERVER::linbo-upload/${subdir}${item}"
    # because return code is always 0 this is necessary
    grep -q " rsync --skip-compress="$RSYNC_SKIP_COMPRESS" error" /tmp/rsync.log && RC=1
    cat /tmp/rsync.log >> /tmp/linbo.log
    rm /tmp/rsync.log
    [ "$RC" = "0" ] || break
  done
else
  RC=1
  echo "File $FILE does not exist."
fi

if [ "$RC" = "0" ]; then
  echo "Successfully uploaded $filelist to $LINBOSERVER."
else
  echo "Failed to upload $filelist to $LINBOSERVER."
fi

cd /
sendlog

exit "$RC"
