#!/bin/bash
#
#	Stop the Docker Container
#
dir=`dirname $0`
cd $dir


# Commit the container to create an image
echo "$ cd " `pwd`
echo "$ docker commit {{&CONTAINER_NAME}}-container tooltwist/{{&CONTAINER_NAME}}"
        docker commit {{&CONTAINER_NAME}}-container tooltwist/{{&CONTAINER_NAME}}
rv=$?
if [ "${rv}" != "0" ] ; then
	echo 'Error creating an image from the container.'
	exit 1
fi

# Remove the container, so we can start another
echo "$ docker rm {{&CONTAINER_NAME}}-container"
        docker rm {{&CONTAINER_NAME}}-container
rv=$?

# Anything else?
if [ "${rv}" == "0" ] ; then
	
	# All ok. Display a nice message
	echo ""
	# echo ""
	# echo "        Success - a Docker image named tooltwist/{{&CONTAINER_NAME}} has been created."
	# echo ""

	# Create a README file
	cat > {{&PROJECT_ROOT}}/DOCKER_README << ENDD
	DOCKER_README
	-------------
	A Docker image named tooltwist/{{&CONTAINER_NAME}} has been committed.
	
	This image may be run in a container using:

	      $ docker run -p 9080:8080 --rm --name {{&CONTAINER_NAME}} tooltwist/{{&CONTAINER_NAME}}

	Or it can be run in the background (but will need to be stopped after use):

	      $ docker run -p 9080:8080 -d --name {{&CONTAINER_NAME}} tooltwist/{{&CONTAINER_NAME}}
	
	To stop it:

	      $ docker stop {{&CONTAINER_NAME}}
		
	You can log on using:
	
	      $ docker exec -it {{&CONTAINER_NAME}} /bin/bash
		
	If you have created a repository at Docker Hub with this name, you can push using:

	      $ docker tag tooltwist/{{&CONTAINER_NAME}} tooltwist/{{&CONTAINER_NAME}}:VERSION 
	      $ docker login -u <username> -p <password> -e <email address>
	      $ docker push tooltwist/{{&CONTAINER_NAME}}:VERSION
	
	You can see the demo application (if it's installed) at:
		
	      http://{{&DOCKER_HOST}}:3080/ttsvr/n/Home/ttdemo-1
		

	For a list of Docker commands see: https://github.com/wsargent/docker-cheat-sheet#lifecycle
	Have fun!
	
ENDD

	# Display the README file
	cat {{&PROJECT_ROOT}}/DOCKER_README
	echo ""
	echo "The above information has been saved in a file named DOCKER_README."
	echo ""

fi

# Finish up
exit ${rv}