#!/bin/sh

# FlashAir utility by Marq (11.8.2014)

# FlashAir IP address - can be set externally
if [ -z "$FA" ] ; then
	FA=192.168.11.20
fi

case $1 in

# If 2nd parameter omitted, shows the root
ls)	curl -s "http://$FA/command.cgi?op=100&DIR=/$2" | cat \
	| grep "," | sed 's/[^,]*,//' | sed 's/,.*//'
	;;

rm)	shift
	curl -s "http://$FA/upload.cgi?WRITEPROTECT=ON" >/dev/null
	for n in  "$@"
	do
		curl -s "http://$FA/upload.cgi?DEL=$n" >/dev/null
	done
	;;

put)	shift
	curl -s "http://$FA/upload.cgi?WRITEPROTECT=ON" >/dev/null
	for n in "$@"
	do
		m=`basename "$n"`
		curl -s --form name="$m" --form "upload=@$n" \
		"http://$FA/upload.cgi" | cat >/dev/null
	done
	;;

get)	shift
	for n in "$@"
	do
		curl -s -O "http://$FA/$n"
	done
	;;

# Set upload dir / create one
cd)	curl -s "http://$FA/upload.cgi?UPDIR=$2" >/dev/null
	;;

*)
	echo "No such command."
esac
