commit a66f9ee269d9722e775840510d894d78de49ba18
parent 29f250d8f2a60b88ebd7a0ddd01f50772ce91180
Author: boyska <piuttosto@logorroici.org>
Date:   Tue, 15 Nov 2011 15:50:09 +0100
Undertaker:add --poll, file:/// See #70
Diffstat:
| M | src/undertaker |  |  | 140 | +++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------- | 
1 file changed, 90 insertions(+), 50 deletions(-)
diff --git a/src/undertaker b/src/undertaker
@@ -31,34 +31,30 @@ if [[ $? != 0 ]]; then
 fi
 source tomb source
 
-if ! [ $1 ] ; then
-    _warning "an argument is missing, the undertaker is confused"
-    _failure "usage: undertaker url://host:path/to/tomb.key"
-fi
-
-ARG1=${1}
-
-check_bin
+typeset -A opts
+typeset -A args
+function undertaker_scheme() {
+    zparseopts -D -print-path=print_path
 
-_message "Undertaker will look for ${ARG1}"
-
-baseurl=${ARG1%//*}
-
-case $baseurl in
-    bluetooth:)
-	_message "access to bluetooth protocol requested"
-	which obexftp &> /dev/null
-	if [[ $? != 0 ]]; then
-	    die "obexftp not found, needed for bluetooth: operation aborted."
-	fi
-	keytmp=`safe_dir undertaker`
-	cd $keytmp
-	# fetch key from bluetooth, url format: bluetooth://55:33:44:55:66/file/path
-	obexdevpath=${ARG1#*//}
-	obexdev=${obexdevpath%%/*}
-	obexpath=${obexdevpath#*/}
-	_message "obex device: $obexdev"
-	_message "obex path: $obexpath"
+    scheme=$1
+    keypath=$2
+    case $scheme in
+        bluetooth)
+            #TODO: support --print-path
+            act "access to bluetooth protocol requested"
+            which obexftp &> /dev/null
+            if [[ $? != 0 ]]; then
+                error "obexftp not found, needed for bluetooth: operation aborted."
+                return 64
+            fi
+            keytmp=`safe_dir undertaker`
+            cd $keytmp
+            # fetch key from bluetooth, url format: bluetooth://55:33:44:55:66/file/path
+            obexdevpath=${keypath#*//}
+            obexdev=${obexdevpath%%/*}
+            obexpath=${obexdevpath#*/}
+            act "obex device: $obexdev"
+            act "obex path: $obexpath"
 	obexftp -b $obexdev -g $obexpath 
 	if [[ $? != 0 ]]; then
 	    rmdir ${keytmp}
@@ -66,28 +62,72 @@ case $baseurl in
 	fi
 	# print out the key on stdout
 	cat $obexpath >&1
-	# wipe out the key
-	${=WIPE} $obexpath
-	cd -
-	rmdir ${keytmp}
+            # wipe out the key
+	${WIPE[@]} $obexpath
+            cd -
+            rmdir ${keytmp}
+
+        #	tombkey="basename $obexpath"
+            ;;
+        file)
+            if ! [[ -f $keypath ]]; then
+                error "Invalid path $keypath"
+                return 1
+            fi
+            if [[ -n $print_path ]]; then
+                echo $keypath;
+            else
+                < $keypath
+                r=$?
+                if [[ $r != 0 ]]; then return 1; fi
+                return 0
+            fi
+            ;;
+
+        *)
+            #TODO: support undertaker-$scheme
+            error "url protocol not recognized: $baseurl"
+            return 64
+            ;;
+    esac
+}
+function main() {
+        zparseopts -M -E -D -Aopts -poll -print-path
+        if ! [ $1 ] ; then
+            error "an argument is missing, the undertaker is confused"
+            act "usage: undertaker [options] url://host:path/to/tomb.key"
+            exit 1;
+        fi
+        check_bin
 
-#	tombkey="basename $obexpath"
-	
-	;;
+        notice "Undertaker will look for ${1}"
 
-    file:)
-        _message "local file access requested"
-	die "TODO"
-	;;
-    http:)
-	_message "access to web protocol requested"
-	die "TODO"
-	;;
-    ssh:)
-	_message "access to secure shell requested"
-	die "TODO"
-	;;
-    *)
-	die "url protocol not recognized: $baseurl"
-	;;
-esac
+        ARG1=${1}
+        scheme=${ARG1%://*}
+        keypath=${ARG1#*//}
+
+        if [[ -n ${(k)opts[--poll]} ]]; then
+            while true; do
+                undertaker_scheme $scheme $keypath
+                r=$?
+                if [[ $r == 0 ]]; then
+                    exit 0
+                fi
+                if [[ $r == 64 ]]; then
+                    exit 64
+                fi
+                sleep 3
+            done
+        else
+            undertaker_scheme $scheme $keypath
+        fi
+}
+main $*
+
+### Conventions and other comments:
+# 
+# EXIT CODES FOR SCHEME HANDLERS
+# 0 is for everything went fine
+# 64 is for "not supported/the problem won't be solved by polling". This is for things like: unmet dependencies, not supported at all, etc
+# everything else means just "error". Use 1, please. So other codes can be used if needed
+#