#!/bin/ash

####
# webdav file uploader/downloader script
# using cadaver and empty-expect
#
# Copyright 2014 Erinome.net
####

ififo=/tmp/empty.in.fifo
ofifo=/tmp/empty.out.fifo
cmd="cadaver https://webdav.yandex.ru"

username=YourName
password=YourPassword

case "$1" in
	put)
		if [ -z "$2" ]; then
			echo "Usage: $0 put <local file> [remote file]";
			exit;
		fi
		if [ ! -f "$2" ]; then
			echo "$2: no such file or directory";
			exit;
		fi
		;;
	get)
		if [ -z "$2" ]; then
			echo "Usage: $0 get <remote file> [local file]";
			exit;
		fi
		if [ -n "$3" -a -f "$3" ] || [ -z "$3" -a -f "$2" ]; then
			[ -n "$3" ] && ofile="$3" || ofile="$2"
			read -p "File '$ofile' already exists! Overwrite? " -n 1 -r confirm
			echo 
			case "$confirm" in
				y|Y )
					;;
				* )
					echo 'Aborted by user command.';
					exit;
					;;
			esac
		fi
		;;
	mput)
		if [ -z "$2" ]; then
			echo "Usage: $0 mput <local file(s)> [remote path]";
			exit;
		fi
		;;
	mget)
		if [ -z "$2" ]; then
			echo "Usage: $0 mget <remote file(s)> [local path]";
			exit;
		fi;
		;;
	*)
		echo "Usage:";
		echo "	$0 put <local file> [remote file]";
		echo "	$0 get <remote file> [local file]";
		echo "  $0 mput <local file(s)> [remote path]";
		echo "  $0 mget <remote file(s)> [local path]";
		exit;
		;;
esac

empty -f -i $ififo -o $ofifo -L /tmp/empty.log $cmd
empty -w -i $ofifo -o $ififo "Do you wish to accept the certificate?" "y\n"
empty -w -i $ofifo -o $ififo "Username:" "$username\n"
empty -w -i $ofifo -o $ififo "Password:" "$password\n"

case "$1" in
	put|get )
		dpath="$3"
		if [ -z "$3" ]; then
			dpath=`basename "$2"`
		fi
		if [[ "${dpath:$((${#dpath} - 1))}" == '/' ]]; then
			dname=`basename "$2"`
			dpath="${dpath}${dname}"
		fi
		empty -v -w -i $ofifo -o $ififo "dav:" "$1 $2 $dpath\n"
		empty -v -w -i $ofifo -o $ififo "failed:" "" "succeeded." ""
		retval=$?
		if [ "$retval" -eq 1 ]; then
			echo "Transfer failed."
		fi
		;;
	mput )
		lpath=`dirname "$2"`
		lmask=`basename "$2"`

		empty -v -w -i $ofifo -o $ififo "dav:" "lcd $lpath\n"
		empty -v -w -i $ofifo -o $ififo -t 2 "Could not change local" "" "dav:" "\n"
		if [ $? -eq 1 ]; then
			echo "Local path can't be set."
			empty -s -o $ififo "exit\n"
			exit;
		fi

		if [ -n "$3" ]; then
			empty -v -w -i $ofifo -o $ififo "dav:" "cd $3\n"
			empty -v -w -i $ofifo -o $ififo -t 2 "Could not access" "" "dav:" "\n"
			retval=$?
			if [ "$retval" -eq 1 ]; then
				echo "Remote path can't be set."
				empty -s -o $ififo "exit\n"
				exit;
			fi
		fi
		empty -v -w -i $ofifo -o $ififo "dav:" "mput $lmask\n"
		empty -v -w -i $ofifo -o $ififo "dav:" ""
		;;
	mget )
		rpath=`dirname "$2"`
		rmask=`basename "$2"`

		empty -v -w -i $ofifo -o $ififo "dav:" "cd $rpath\n"
		empty -v -w -i $ofifo -o $ififo -t 2 "Could not access" "" "dav:" "\n"
		if [ $? -eq 1 ]; then
			echo "Remote path can't be set."
			empty -s -o $ififo "exit\n"
			exit;
		fi

		if [ -n "$3" ]; then
			empty -v -w -i $ofifo -o $ififo "dav:" "lcd $3\n"
			empty -v -w -i $ofifo -o $ififo -t 2 "Could not change local" "" "dav:" "\n"
			retval=$?
			if [ "$retval" -eq 1 ]; then
				echo "Local path can't be set."
				empty -s -o $ififo "exit\n"
				exit;
			fi
		fi
		empty -v -w -i $ofifo -o $ififo "dav:" "mget $rmask\n"
		empty -v -w -i $ofifo -o $ififo "dav:" ""
		;;
esac

empty -s -o $ififo "exit\n"
echo

