From 68abd3c4efda5edcda858552be2d925c02796827 Mon Sep 17 00:00:00 2001
From: mviereck <bachbaum24@gmx.de>
Date: Thu, 12 Jul 2018 01:39:00 +0200
Subject: [PATCH] New syntax for custom `docker run` options (#58)

---
 CHANGELOG.md | 21 +++++++++++++++++----
 x11docker    | 18 ++++++++++++++----
 2 files changed, 31 insertions(+), 8 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index e730314c..6498d497 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,19 +7,32 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
 Project website: https://github.com/mviereck/x11docker
 
 ## [Unreleased]
-### Changed
- - Adjustments to support [MSYS2/CYGWIN/MINGW](https://www.msys2.org/). 
+### Added
+ - Support of [MSYS2/CYGWIN/MINGW](https://www.msys2.org/). 
    [(#55)](https://github.com/mviereck/x11docker/issues/55)
+### Changed
+ - Syntax changed for custom `DOCKER_RUN_OPTIONS`:
+     Previous, still valid:
+   x11docker [OPTIONS] -- "[DOCKER_RUN_OPTIONS]" IMAGE [COMMAND [ARG1 ARG2 ...]]
+     New:
+   x11docker [OPTIONS] -- DOCKER_RUN_OPTIONS -- IMAGE [COMMAND [ARG1 ARG2 ...]]
+     BREAKS due to wrongly parsed -- :
+   x11docker [OPTIONS] -- "DOCKER_RUN_OPTIONS" IMAGE COMMAND ARG1 -- ARG2
+     Valid:
+   x11docker [OPTIONS] -- "DOCKER_RUN_OPTIONS" -- IMAGE COMMAND ARG1 -- ARG2
+   x11docker [OPTIONS] -- IMAGE COMMAND ARG1 -- ARG2
+   [(#58)](https://github.com/mviereck/x11docker/issues/58)
 ### Fixed
  - `--xpra`: Support of outdated xpra version v0.17.6 that is still
    distributed in debian stretch and buster.
- - `--pw gksu`, `--pw gksudo`: Disable keyboard grabbing to avoid issues
-   with Gnome 3 Wayland session.
  - `--xpra`: Check if `xhost` is available if xpra version 2.3.1 has cookie 
    issue. [(#57)](https://github.com/mviereck/x11docker/issues/57)
  - `--xpra-xwayland`: Set Weston `--fullscreen` to get Xwayland resolution
    matching host display. Weston v4.0.0 seems to interpret screen size 
    settings in `weston.ini` different than before.
+ - `--pw gksu`, `--pw gksudo`: Disable keyboard grabbing to avoid issues
+   with Gnome 3 Wayland session.
+ - Don't fail on missing password prompt frontend if no password is needed.
 
 ## [4.3.6](https://github.com/mviereck/x11docker/releases/tag/v4.3.6) - 2018-07-03
 ### Changed
diff --git a/x11docker b/x11docker
index c9082164..3ed10e82 100755
--- a/x11docker
+++ b/x11docker
@@ -10,7 +10,7 @@
 # Type 'x11docker --help' or scroll down to read usage information.
 # More documentation at:  https://github.com/mviereck/x11docker
 
-Version="4.3.7-beta"
+Version="5.0.0-beta"
 
 usage() {               # --help: show usage information
   echo "
@@ -19,7 +19,8 @@ x11docker: Run GUI applications and desktop environments in docker.
 Usage:
 To run a docker image with new X server (auto-choosing X server):
   x11docker [OPTIONS] IMAGE [COMMAND]
-  x11docker [OPTIONS] -- "'"[DOCKER_RUN_OPTIONS]"'" IMAGE [COMMAND [ARG1 ARG2 ...]]
+  x11docker [OPTIONS] -- IMAGE [COMMAND [ARG1 ARG2 ...]]
+  x11docker [OPTIONS] -- DOCKER_RUN_OPTIONS -- IMAGE [COMMAND [ARG1 ARG2 ...]]
 To run a host application on a new X server:
   x11docker [OPTIONS] --exe COMMAND
   x11docker [OPTIONS] --exe -- COMMAND [ARG1 ARG2 ...]
@@ -746,7 +747,7 @@ storepid () {           # store pid $1 and name $2 of background process in file
 checkpid() {            # check if PID $1 is active
   ps -p ${1:-} >/dev/null 2>&1
 }
-pspid() {               # ps -p --no-headers $1
+pspid() {               # ps -p $1 --no-headers
   ps -p ${1:-} | tail -n+2
 }
 no_xhost() {            # remove any access to X server granted by xhost
@@ -1645,6 +1646,13 @@ $Customenvironment"    ;  shift ;;                         # set custom environm
 
       ##### custom docker options / image name + image command
       --) shift
+        [ "$(cut -c1 <<< "${1:-}")"  = "-" ] && grep -q " -- "  <<< "$*" && {
+          Customdockeroptions="$(sed 's/ -- .*//' <<< "$*" )"
+          Imagecommand="$(sed -n 's/^.* -- //p' <<< "$*" )"
+          Imagename="$(cut -d ' ' -f1 <<< "$Imagecommand")"
+          Imagecommand="$(cut -d ' ' -f2 -s <<< "$Imagecommand")"
+          break
+        }
         while [ $# -gt 0 ] ; do
           [ -n "${1:-}" ] && [ -z "$Imagename" ] && [ "$(echo "${1:-}" | cut -c1)"  = "-" ]  && Customdockeroptions="$Customdockeroptions ${1:-}"
           [ -n "${1:-}" ] && [ -z "$Imagename" ] && [ "$(echo "${1:-}" | cut -c1)" != "-" ]  && Imagename="${1:-}" && shift
@@ -3756,7 +3764,9 @@ done
     *) error "Unknown password prompt '$Passwordfrontend' (option --pw).
   Possible: su sudo gksu gksudo lxsu lxsudo kdesu kdesudo beesu pkexec none" ;;
   esac
-  command -v $(echo $Passwordcommand|cut -d' ' -f1) >/dev/null || error "Password prompt frontend $(echo $Passwordcommand|cut -d' ' -f1) not found"
+  [ "$Passwordneeded" = "yes" ] && {
+    command -v $(echo $Passwordcommand|cut -d' ' -f1) >/dev/null || error "Password prompt frontend $(echo $Passwordcommand|cut -d' ' -f1) not found"
+  }
 }
 
 { #### part: check terminal for password prompt of su or sudo and for 'docker pull'