#!/bin/bash

# Upload files to a webdav storage,
# check them by an MD5 Hash
# and remove the local copy afterwards.

if [ "$1" == "-h" ];
then
echo "Syntax:"
echo "upload.sh [Local File Location] [Remote File Location] [username:password]"

elif [ $1 = $2 ];
then
echo "Source and Target are the same"
echo "No Arguments provided!"

else
#upload the image
curl -T $1 -u $3 $2 -k

# get the online images' MD5up
online_md5=$(curl $2 -k -u $3 | md5sum | awk '{print $1}')

# get the local images' MD5
local_md5=$(md5sum "$1" | awk '{print $1}')

# compare both MD5s
echo "$online_md5"
echo "$local_md5"

if [ "$online_md5" == "$local_md5" ];
# Remove the original Image if successful
then
echo "Uploaded successfully!"

# Remove Original
rm $1

# Fallback routine if not successful
else
echo "Upload failed!!"
fi
fi
