Simplified a bit.
[xremote.git] / xremote.sh
index 33396bc..bcb33ca 100755 (executable)
@@ -35,7 +35,7 @@ function check_remote_is_defined () {
 
 function help () {
     cat <<EOF
-xremote.sh [--help] [-h <remote_host>] [-i <remote_dir>] <script> [script arguments]
+xremote.sh [--help] [-h <remote_host>] [-d <remote_dir>] [-r <local_result_dir>] [-i] <script> [script arguments]
 
   This script takes a script as argument and executes it remotely in a
   temporary directory on a ssh-accessible server.
@@ -62,10 +62,17 @@ xremote.sh [--help] [-h <remote_host>] [-i <remote_dir>] <script> [script argume
 
   If the -h option is provided @XREMOTE_HOST is ignored.
 
+  If the -d option is provided, the provided directory is used and
+  kept, instead of a temporary one
+
   If the -i option is provided, all the files are installed and
   scripts run in the specified directory on the remote host, but the
   main executable and post-run commands are ignored
 
+  If the -r option is provided, the result files specified with
+  @XREMOTE_GET will be downloaded there instead of the current
+  directory.
+
   If no argument is provided to @XREMOTE_HOST, and the -h option is
   not specified, the environment variable \$XREMOTE_HOST is used
   instead
@@ -77,9 +84,15 @@ EOF
 }
 
 function cleanup_remote_tmp () {
-    if [[ "${REMOTE_HOST}" ]] && [[ "${REMOTE_DIR}" ]] && [[ ! "${ARG_DIR}" ]]
+    if [[ "${REMOTE_HOST}" ]] && [[ "${REMOTE_DIR}" ]]
     then
-        echo "xremote: Clean up remote workdir."
+        if [[ "${ARG_DIR}" ]]
+        then
+            echo "xremote: Keeping remote workdir."
+        else
+            echo "xremote: Cleaning up temporary remote workdir."
+            ssh "${REMOTE_HOST}" rm -rf "${REMOTE_DIR}"
+        fi
     fi
 }
 
@@ -96,13 +109,25 @@ do
             echo "xremote: remote forced to ${ARG_HOST}"
             ;;
 
-        -i)
+        -d)
             shift
             ARG_DIR="$1"
             [[ ${ARG_DIR} ]] || (echo "xremote: Directory missing." && exit 1)
             echo "xremote: remote dir set to ${ARG_DIR}"
             ;;
 
+        -i)
+            NO_RUN=1
+            echo "xremote: no run"
+            ;;
+
+        -r)
+            shift
+            ARG_RESULT_DIR="$1"
+            [[ ${ARG_RESULT_DIR} ]] || (echo "xremote: Directory missing." && exit 1)
+            echo "xremote: result dir set to ${ARG_RESULT_DIR}"
+            ;;
+
         --help)
             help
             exit 0
@@ -146,8 +171,8 @@ do
     if [[ "${line}" =~ '@XREMOTE' ]]
     then
 
-        label=$(echo "${line}" | sed -e 's/^.*@XREMOTE_\([^:]*\):.*$/\1/')
-        value=$(echo "${line}" | sed -e 's/^.*@XREMOTE_[^:]*: *\(.*\)$/\1/')
+        label=$(sed -e 's/^.*@XREMOTE_\([^:]*\):.*$/\1/' <<<"${line}")
+        value=$(sed -e 's/^.*@XREMOTE_[^:]*: *\(.*\)$/\1/' <<<"${line}")
 
         case "${label}" in
 
@@ -195,7 +220,7 @@ check_remote_is_defined
 
 tar c "${main}" | ssh "${REMOTE_HOST}" "cd \"${REMOTE_DIR}\" && tar mx"
 
-if [[ "${ARG_DIR}" ]]
+if [[ "${NO_RUN}" ]]
 then
     echo "xremote: everything has been set up in ${REMOTE_HOST}:${ARG_DIR}"
     exit 0
@@ -229,12 +254,21 @@ echo "xremote: -- retrieving files -----------------------------------------"
 
 set -f
 
+if [[ "${ARG_RESULT_DIR}" ]]
+then
+    RESULT_DIR="${ARG_RESULT_DIR}"
+else
+    RESULT_DIR="."
+fi
+
+[[ "${ARG_RESULT_DIR}" ]] && mkdir -p "${ARG_RESULT_DIR}"
+
 while read line
 do
     if [[ "${line}" =~ '@XREMOTE' ]]
     then
-        label=$(echo "${line}" | sed -e 's/^.*@XREMOTE_\([^:]*\):.*$/\1/')
-        value=$(echo "${line}" | sed -e 's/^.*@XREMOTE_[^:]*: *\(.*\)$/\1/')
+        label=$(sed -e 's/^.*@XREMOTE_\([^:]*\):.*$/\1/' <<<"${line}")
+        value=$(sed -e 's/^.*@XREMOTE_[^:]*: *\(.*\)$/\1/' <<<"${line}")
         case "${label}" in
             POST)
                 check_remote_is_defined
@@ -244,7 +278,7 @@ do
 
             GET)
                 check_remote_is_defined
-                ssh </dev/null "${REMOTE_HOST}" "cd \"${REMOTE_DIR}\" && tar 2>/dev/null c ${value}" | tar mxv
+                ssh </dev/null "${REMOTE_HOST}" "cd \"${REMOTE_DIR}\" && tar 2>/dev/null c ${value}" | tar mxv -C "${RESULT_DIR}"
                 ;;
         esac
     fi