From 477ca7c2538c9003929cd01b7cd536e61a52efa4 Mon Sep 17 00:00:00 2001
From: jurgenhaas <juergen@paragon-es.de>
Date: Sat, 29 Dec 2018 12:05:16 +0100
Subject: [PATCH] #2 Name and re-use ne container per project

---
 README.md | 17 ++++++++++++++++-
 l3d       | 47 +++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 61 insertions(+), 3 deletions(-)

diff --git a/README.md b/README.md
index 5c3ec90..7e48308 100644
--- a/README.md
+++ b/README.md
@@ -8,6 +8,13 @@ Nothing else than docker and this [script](https://gitlab.lakedrops.com/docker/l
 
 Ideally you download that script and copy that to a directory in your search path (e.g. `/usr/local/bin`), then it will be available for all your Drupal projects.
 
+You can execute the following commands:
+
+```bash
+sudo wget -O /usr/local/bin/l3d https://gitlab.lakedrops.com/docker/l3d/raw/master/l3d
+sudo chmod +x /usr/local/bin/l3d
+```
+
 ## Quick start
 
 Go to the project root for which you want to use **L3D** and execute the downloaded script (simply type `l3d` at the command prompt). It will ask you for the PHP version to be used (input `7.0`, `7.1` or `7.2`) and it will download the matching L3D image and start a container for you.
@@ -27,7 +34,15 @@ Inside the container you'll find the directory `/drupal` which contains everythi
 
 ## Update the image
 
-When starting **L3D** with the script this will simply re-use the latest available image in your local docker environment. To force your docker to download the latest version of an image you can use `l3d -u` which will also start the working container for you but check for an updated version of the image first.
+When starting **L3D** with the script this will simply re-use the latest available image in your local docker environment. To force your docker to download the latest version of an image you can use `l3d update` which will also start the working container for you but check for an updated version of the image first.
+
+## Update the start script
+
+Just call `l3d selfupdate` and the start script will be updated from the original repository.
+
+## Getting help
+
+Calling `l3d help` displays information about **L3D** and provides a link to further details.
 
 ## Examples
 
diff --git a/l3d b/l3d
index 90d90cd..e2ec6fc 100755
--- a/l3d
+++ b/l3d
@@ -1,5 +1,31 @@
 #!/usr/bin/env bash
 
+if [[ "$1" == "help" ]]; then
+  echo "L3D - LakeDropsDrupalDev"
+  echo "========================="
+  echo ""
+  echo "Version: 1.1.0"
+  echo "Info and support: https://gitlab.lakedrops.com/docker/l3d"
+  exit 0
+fi
+
+if [[ "$1" == "selfupdate" ]]; then
+  DEST=$(which l3d)
+  WGET=$(which wget)
+  CURL=$(which curl)
+  if [[ -n "$WGET" ]]; then
+    sudo wget -O $DEST https://gitlab.lakedrops.com/docker/l3d/raw/master/l3d
+  elif [[ -n "$CURL" ]]; then
+    sudo curl -o $DEST https://gitlab.lakedrops.com/docker/l3d/raw/master/l3d
+  else
+    echo "Could't find wget or curl to perform the update!"
+    exit 1
+  fi
+  sudo chmod +x $DEST
+  echo "Self update succeeded!"
+  exit 0
+fi
+
 export $(cat .env | xargs)
 if [[ "${PHP_VERSION}" == "" ]]; then
   while true; do
@@ -19,8 +45,25 @@ if [[ "${COMPOSE_PROJECT_NAME}" == "" ]]; then
   echo "COMPOSE_PROJECT_NAME=${COMPOSE_PROJECT_NAME}" >>.env
 fi
 
-if [[ "$1" == "-u" ]]; then
+if [[ "$1" == "update" ]]; then
   docker pull registry.lakedrops.com/docker/l3d:php-${PHP_VERSION}
 fi
 
-docker run --rm -it -v /var/run/docker.sock:/var/run/docker.sock -v ${PWD}:/drupal -v ${SSH_AUTH_SOCK}:/ssh-agent -v ${HOME}/.traefik:/root/.traefik -e SSH_AUTH_SOCK=/ssh-agent -e COMPOSE_PROJECT_NAME=${COMPOSE_PROJECT_NAME} -w /drupal registry.lakedrops.com/docker/l3d:php-${PHP_VERSION} /usr/bin/fish
+ID=$(docker container ls -q -f name=${COMPOSE_PROJECT_NAME}_l3d)
+if [[ -n "$ID" ]]; then
+  docker start ${COMPOSE_PROJECT_NAME}_l3d
+else
+  docker run --name ${COMPOSE_PROJECT_NAME}_l3d -dt \
+    --hostname ${COMPOSE_PROJECT_NAME}-l3d \
+    --env COMPOSE_PROJECT_NAME=${COMPOSE_PROJECT_NAME} \
+    --env SSH_AUTH_SOCK=/ssh-agent \
+    --env-file ${PWD}/.env \
+    --volume /var/run/docker.sock:/var/run/docker.sock \
+    --volume ${PWD}:/drupal \
+    --volume ${SSH_AUTH_SOCK}:/ssh-agent \
+    --volume ${HOME}/.traefik:/root/.traefik \
+    --workdir /drupal \
+    --restart unless-stopped \
+    registry.lakedrops.com/docker/l3d:php-${PHP_VERSION}
+fi
+docker exec -it ${COMPOSE_PROJECT_NAME}_l3d /usr/bin/fish
-- 
GitLab