#!/bin/bash
#Variable to indicate if a docker image should be saved (user choice)
	#If it is passed in as yes or no from a script use than. Otherwise use a input
if [ ! -n "$SAVE" ] 
then
echo "Do you want to save a new docker image as a backup?"
	read -p "	Yes/No: " SAVE
fi
#Convert to upper case.
SAVE=${SAVE^^}
#Exit if not valid
if [ ! $SAVE = YES ] && [ ! $SAVE = NO ] ; then
	echo "The option to save the docker image must be set to Yes or No. Exiting."
	exit
fi

DOCKER_CONTAINER_NAME="beamer_container"
DOCKER_IMAGE_NAME="acg/thesis_defense_latex:1.0"

	#If the docker image to run beamer LaTex doesn't exist make it 
	if [ -z "$(docker images -q $DOCKER_IMAGE_NAME 2> /dev/null)" ]; then
		echo "Building a LaTex docker image, for the thesis."
		#Move to the directory with the Dockerfile used to run the regression analysis, and create result figures.
		cd ./Docker_Make/
		#Move back tot he main working directory
		docker build -t $DOCKER_IMAGE_NAME .
		cd ../
		echo "Docker LaTex image built"
	fi
	#Check if a docker container of the image is running, and if an exited container blocks, removed if before running a new one.
	if [  "$(docker ps -a -q -f name=${DOCKER_CONTAINER_NAME})" ]; then
		echo "Removing existing container of thesis image."
		if [ "$(docker ps -aq -f status=exited -f name=${DOCKER_CONTAINER_NAME})" ]; then
			# cleanup
			docker rm ${DOCKER_CONTAINER_NAME}
		# run the container
		echo "Running docker image as a container"
		docker run -d -t --name "$DOCKER_CONTAINER_NAME" "$DOCKER_IMAGE_NAME" 

		  fi
	  else	
        # run the container
	echo "Running docker image as a container"
	docker run -d -t --name "$DOCKER_CONTAINER_NAME" "$DOCKER_IMAGE_NAME"
	fi



	#Save a image snapshot with all LaTex code. The SAVE variable is made to be lower case.
	if [ $SAVE = YES ]
	then
		timestamp=$(date +%Y-%m-%d)
		SAVE_DIR="ACG_DEFENSE_BEAMER_DOCKER_${timestamp}"
		SAVE_DIR_PATH="Saved_Images/${SAVE_DIR}"
		echo "Making Save Location ${SAVE_DIR_PATH}"
		mkdir -p ${SAVE_DIR_PATH} 
		echo "Copying LaTex Code to ${SAVE_DIR_PATH}"
		cp -r ./LATEX/* ${SAVE_DIR_PATH} 
		cd ${SAVE_DIR_PATH} 
		#Run the cleaning script to remove any files that do not need to be upload to the docker container.
		bash clean.sh
		mkdir -p DOCKER_IMAGE 
		echo "Writing script file to run the Docker LaTex code"
	echo '#If the image does not exist make it' > DOCKER_RUN.sh
	echo "if [ ! \"\$(docker images -q ${DOCKER_IMAGE_NAME} 2> /dev/null)\" ]; then" >> DOCKER_RUN.sh
	echo "	docker load -i DOCKER_IMAGE/DEFENSE_DOCKER.tar" >> DOCKER_RUN.sh
	echo "fi" >> DOCKER_RUN.sh
	echo "#If container is stoped remove." >> DOCKER_RUN.sh
	echo "if [ \"\$(docker ps -aq -f status=exited -f name=${DOCKER_CONTAINER_NAME})\" ]; then" >> DOCKER_RUN.sh
	echo "	docker rm ${DOCKER_CONTAINER_NAME}" >> DOCKER_RUN.sh
	echo "	docker run -d -t --name ${DOCKER_CONTAINER_NAME} ${DOCKER_IMAGE_NAME}" >> DOCKER_RUN.sh
	echo "fi" >> DOCKER_RUN.sh
	echo "#If container does not exists make it" >> DOCKER_RUN.sh
	echo "if [ ! \"\$(docker ps -a -q -f name=${DOCKER_CONTAINER_NAME})\" ]; then" >> DOCKER_RUN.sh
	echo "	docker run -d -t --name ${DOCKER_CONTAINER_NAME} ${DOCKER_IMAGE_NAME}" >> DOCKER_RUN.sh
	echo "fi" >> DOCKER_RUN.sh >> DOCKER_RUN.sh
	echo "#Run LaTex Docker on the build files, export the pdf, and exit" >> DOCKER_RUN.sh
echo "mv DOCKER_IMAGE/ ../" >> DOCKER_RUN.sh

	echo "docker cp ./ ${DOCKER_CONTAINER_NAME}:/LaTex" >> DOCKER_RUN.sh
	echo "docker exec ${DOCKER_CONTAINER_NAME} bash /LaTex/build.sh" >> DOCKER_RUN.sh
	echo "docker cp ${DOCKER_CONTAINER_NAME}:/LaTex/Defense.pdf ./" >> DOCKER_RUN.sh
echo "mv ../DOCKER_IMAGE/ ./" >> DOCKER_RUN.sh

	echo "docker stop ${DOCKER_CONTAINER_NAME}" >> DOCKER_RUN.sh
	echo "docker remove ${DOCKER_CONTAINER_NAME}" >> DOCKER_RUN.sh

		cd DOCKER_IMAGE
		echo "Creating Docker Image Saved to ${SAVE_DIR_PATH}/DOCKER_IMAGE/DEFENSE_DOCKER.tar"
		docker save "${DOCKER_IMAGE_NAME}"  > DEFENSE_DOCKER.tar
		cd ../../
		echo "Creating a compressed tar.gz file of all contents of ${SAVE_DIR_PATH}"
		tar -czf "${SAVE_DIR}.tar.gz" "${SAVE_DIR}" 
		echo "Removing original contents of  ${SAVE_DIR_PATH} to save space"
		rm -r ${SAVE_DIR}
		echo "Snapshot saved!"
	fi

	
