Added some error messages.
[xremote.git] / xremote.sh
index 9752a9a..33396bc 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>] [-i <remote_dir>] <script> [script arguments]
 
   This script takes a script as argument and executes it remotely in a
   temporary directory on a ssh-accessible server.
@@ -56,6 +56,10 @@ xremote.sh [-h <remote_host>] [-i <remote_dir>] <script> [script arguments]
     @XREMOTE_GET: *.dat
     @XREMOTE_PRE: ln -s /home/fleuret/data/pytorch ./data
 
+  If a file with the same name as the script with the .xremote
+  extension appended to it exists, arguments will be read from it by
+  default.
+
   If the -h option is provided @XREMOTE_HOST is ignored.
 
   If the -i option is provided, all the files are installed and
@@ -88,17 +92,24 @@ do
         -h)
             shift
             ARG_HOST="$1"
+            [[ ${ARG_HOST} ]] || (echo "xremote: Hostname missing." && exit 1)
             echo "xremote: remote forced to ${ARG_HOST}"
             ;;
 
         -i)
             shift
             ARG_DIR="$1"
+            [[ ${ARG_DIR} ]] || (echo "xremote: Directory missing." && exit 1)
             echo "xremote: remote dir set to ${ARG_DIR}"
             ;;
 
+        --help)
+            help
+            exit 0
+            ;;
+
         *)
-            echo "Unknown option $1"
+            echo "xremote: Unknown option $1"
             exit 1
             ;;
     esac
@@ -107,12 +118,22 @@ done
 
 ######################################################################
 
-[[ -a "$1" ]] || (help && exit 1)
+[[ "$1" ]] || (echo "xremote: Script name missing" && exit 1)
 
-main="$(basename "$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_config}" ]]
+then
+    echo "xremote: found ${main_config}"
+else
+    main_config="${main}"
+fi
+
 shift
 
 trap cleanup_remote_tmp EXIT
@@ -166,7 +187,7 @@ do
         esac
     fi
 
-done < "${main}"
+done < "${main_config}"
 
 ######################################################################
 
@@ -192,6 +213,7 @@ fi
 ######################################################################
 
 # I find this slightly ugly ...
+
 for s in "$@"
 do
   quoted_args="${quoted_args} \"${s}\""
@@ -226,7 +248,8 @@ do
                 ;;
         esac
     fi
-done < "${main}"
+
+done < "${main_config}"
 
 set +f