Replaced -i with -d and -i.
[xremote.git] / xremote.sh
index ecf6f3e..7ebc0d4 100755 (executable)
@@ -35,7 +35,7 @@ function check_remote_is_defined () {
 
 function help () {
     cat <<EOF
-xremote.sh [-h <remote_host>] [-i <remote_dir>] <script> [script arguments]
+xremote.sh [--help] [-h <remote_host>] [-d <remote_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,6 +62,9 @@ xremote.sh [-h <remote_host>] [-i <remote_dir>] <script> [script arguments]
 
   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
@@ -77,9 +80,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
 }
 
@@ -92,17 +101,29 @@ do
         -h)
             shift
             ARG_HOST="$1"
+            [[ ${ARG_HOST} ]] || (echo "xremote: Hostname missing." && exit 1)
             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"
+            ;;
+
+        --help)
+            help
+            exit 0
+            ;;
+
         *)
-            echo "Unknown option $1"
+            echo "xremote: Unknown option $1"
             exit 1
             ;;
     esac
@@ -111,18 +132,22 @@ done
 
 ######################################################################
 
-[[ -a "$1" ]] || (help && exit 1)
+[[ "$1" ]] || (echo "xremote: Script name missing" && exit 1)
+
+[[ -a "$1" ]] || (help && echo >&2 "xremote: Cannot find script \`$1'" && exit 1)
+
+cd "$(dirname "$1")"
 
 main="$(basename "$1")"
 main_config="${main}.xremote"
 
-if [[ ! -f "${main}.xremote" ]]
+if [[ -f "${main_config}" ]]
 then
+    echo "xremote: found ${main_config}"
+else
     main_config="${main}"
 fi
 
-cd "$(dirname "$1")"
-
 shift
 
 trap cleanup_remote_tmp EXIT
@@ -184,7 +209,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
@@ -202,6 +227,7 @@ fi
 ######################################################################
 
 # I find this slightly ugly ...
+
 for s in "$@"
 do
   quoted_args="${quoted_args} \"${s}\""
@@ -236,6 +262,7 @@ do
                 ;;
         esac
     fi
+
 done < "${main_config}"
 
 set +f