#!/usr/bin/env bash

if [ $# -ne 1 ] ; then
	echo "Usage: " $(basename $0) "<file>"
	echo
	echo "The following shell variables can be set in ~/.seeddms-upload.env"
	echo "  baseurl: url of restapi service"
	echo "  username: name of user to be used"
	echo "  password: password of user to be used. If not set, it will be asked."
	echo "  targetfolder: id of folder where file is uploaded"
	exit 0
fi

if [ ! -f $1 ] ; then
	echo "File '$1' not found"
	exit 1
fi

uploadfile=$1

if [ -f ~/.seeddms-upload.env ] ; then
	. ~/.seeddms-upload.env
fi

if [ -z "$baseurl" ] ; then
	echo "Missing base url"
	exit 1
fi

if [ -z "$username" ] ; then
	echo "Missing username"
	exit 1
fi

if [ -z "$targetfolder" ] ; then
	echo "Missing target folder"
	exit 1
fi

if [ -z "$password" ] ; then
	read -s -p "Password: " password
	echo
fi

cookiefile=/tmp/cookies.txt
trap 'rm -f $cookiefile' EXIT

origfilename=$(php -r "echo urlencode(basename('$uploadfile'));")

# First login
data=$(curl -s -c $cookiefile --data "user=$username&pass=$password" $baseurl/login)
success=$(echo $data | jq .success)
if [ "$success" = "false" ] ; then
	echo "Could not login! Check your credentials";
	echo $data | jq
	exit 1;
fi

# Upload file
#data=$(curl -s -b $cookiefile -T $uploadfile "$baseurl/folder/$targetfolder/document?name=$origfilename&origfilename=$origfilename")
data=$(cat $uploadfile | curl -s -b $cookiefile -T - "$baseurl/folder/$targetfolder/document?name=$origfilename&origfilename=$origfilename")
success=$(echo $data | jq .success)
if [ "$success" = "false" ] ; then
	echo $data | jq
	exit 1;
fi

echo $data | jq
