#!/bin/bash
## quit container: ctrl + p + q
##

DOCKER_REMOTE_IMAGE_NAME=fogvdn
DOCKER_IMAGE_NAME=pear_cdn
DOCKER_CONTAINER_NAME=pear_cdn_container
VERSION=414

install_docker()
{
	case $1 in
		ubuntu)
			apt-get update
			apt-get -y install docker.io
			;;
		centos)
			yum -y install docker
			systemctl start docker
			systemctl enable docker
			;;
		*)
			echo "unknown platform, cannot install docker"
			exit -1
			;;
	esac
}


check_docker()
{
	docker rm -f hello-world > /dev/null 2>/dev/null

	result=`docker run --name hello-world hello-world 2>/dev/null`
	if [[ ${result} =~ "Hello from Docker" ]];then
		echo "install docker success"
		exit 0
	else
		echo "install docker fail"
		exit -1
	fi
}


remove_docker()
{
	docker image rm -f ${DOCKER_REMOTE_IMAGE_NAME}

	result=`docker image ls ${DOCKER_REMOTE_IMAGE_NAME}`
	if [[ ${result} =~ "${DOCKER_REMOTE_IMAGE_NAME}" ]];then
		echo "remove docker remote image fail"
		exit -1
	else
		echo "remove docker remote image success"
		exit 0
	fi
}


install_image()
{
cat<<\EOF > dockerfile
# The dockerfile has Change add sshd services on openfog/fogvdn
#
FROM openfog/fogvdn:latest
MAINTAINER pear fuqiyang "f@pear.hk"

# Install dependent libraries(curl wget vim nginx ping killall ifconfig netstat ssh scp)
#
#RUN apt-get update
#RUN apt-get -y install libcurl4-openssl-dev
#RUN apt-get -y install curl
#RUN apt-get -y install wget
#RUN apt-get -y install vim
#RUN apt-get -y install nginx
#RUN apt-get -y install inetutils-ping
#RUN apt-get -y install psmisc
#RUN apt-get -y install net-tools

# Auto running pear fogvdn
#
#ENTRYPOINT ["/sbin/install_service.sh"] 
#CMD ["PEAR_1900_X64_LINUX", "414"]

# expost ports
#
#EXPOSE 49193
#EXPOSE 49194
#EXPOSE 49195
#EXPOSE 49196
#EXPOSE 49197
#EXPOSE 49198
#EXPOSE 49199
#EXPOSE 49200
#EXPOSE 49201
#EXPOSE 49202
EOF
	
	# mkdir images from dockerfile
	#
	docker build -t ${DOCKER_IMAGE_NAME} .
}


check_image()
{
	result=`docker image ls ${DOCKER_IMAGE_NAME}`

	if [[ ${result} =~ "${DOCKER_IMAGE_NAME}" ]];then
		echo "install image success"
		exit 0
	else
		echo "install image fail"
		exit -1
	fi
}


remove_image()
{
	docker image rm -f ${DOCKER_IMAGE_NAME}

	result=`docker image ls ${DOCKER_IMAGE_NAME}`
	if [[ ${result} =~ "${DOCKER_IMAGE_NAME}" ]];then
		echo "remove image fail"
		exit -1
	else
		echo "remove image success"
		exit 0
	fi
}


check_container()
{
	result=`docker container ls -al`

	if [[ ${result} =~ "${DOCKER_CONTAINER_NAME}" ]];then
		echo "install container success"
		exit 0
	else
		echo "install container fail"
		exit -1
	fi
}


remove_container()
{
	docker container rm -f ${DOCKER_CONTAINER_NAME}

	result=`docker container ls -al`
	if [[ ${result} =~ "${DOCKER_CONTAINER_NAME}" ]];then
		echo "remove container fail"
		exit -1
	else
		echo "remove container success"
		exit 0
	fi
}


start_container()
{
	result=`docker start ${DOCKER_CONTAINER_NAME} 2>/dev/null`

	if [[ ${result} =~ "${DOCKER_CONTAINER_NAME}" ]];then
		echo "start container success"
		exit 0
	else
		echo "start container fail"
		exit -1
	fi
}


stop_container()
{
	result=`docker stop ${DOCKER_CONTAINER_NAME} 2>/dev/null`

	if [[ ${result} =~ "${DOCKER_CONTAINER_NAME}" ]];then
		echo "stop container success"
		exit 0
	else
		echo "stop container fail"
		exit -1
	fi
}


check_pear()
{
	result=`docker exec ${DOCKER_CONTAINER_NAME} ps aux | grep pear_`

	if [[ ${result} =~ "pear_update" ]];then
		echo "install pear success"
		exit 0
	else
		echo "install pear fail"
		exit -1
	fi
}


while getopts 'a:bcrstuvwm:g:p:xyzhi?h' OPT
do
	case ${OPT} in
		a)
		install_docker ${OPTARG}
		exit 0
		;;
		b)
		check_docker
		exit 0
		;;
		c)
		remove_docker
		exit 0
		;;
		r)
		install_image
		exit 0
		;;
		s)
		check_image
		exit 0 ;;
		t)
		remove_image
		exit 0
		;;
		u)
		;;
		v)
		check_container
		exit 0
		;;
		w)
		remove_container
		exit 0
		;;
		x)
		start_container
		exit 0
		;;
		y)
		stop_container
		exit 0
		;;
		z)
		check_pear
		exit 0
		;;
		m)
		MAC_ADDRESS=${OPTARG}
		;;
		g)
		MOUNT_DIR=${OPTARG}
		;;
		p)
		PLATFORM=${OPTARG}
		;;
		?|h|*)
		echo "`basename $0`: `basename $0`/1.0.0"
		echo "Usage:"
		echo ""
		echo "install docker      -a [ubuntu | centos]"
		echo "                    such as: `basename $0` -a ubuntu"
		echo ""
		echo "check   docker      -b"
		echo "                    such as: `basename $0` -b"
		echo ""
		echo "remove  docker      -c"
		echo "                    not implemention"
		echo ""
		echo "install image       -r"
		echo "                    such as: `basename $0` -r"
		echo ""
		echo "check   image       -s"
		echo "                    such as: `basename $0` -s"
		echo ""
		echo "remove  image       -t"
		echo "                    such as: `basename $0` -t"
		echo ""
		echo "install container   -u"
		echo "                    -m [mac]"
		echo "                    -g [disk_dir]"
		echo "                    -p [PEAR_1600_X64_UBUNTU | PEAR_1801_X64_LINUX | PEAR_1900_X64_LINUX]"
		echo "                    such as: `basename $0` -u -m 00:0c:29:04:d0:e1 -g /tmp/test -p PEAR_1801_X64_LINUX"
		echo ""
		echo "check container     -v"
		echo "                    such as: `basename $0` -v"
		echo ""
		echo "remove container    -w"
		echo "                    such as: `basename $0` -w"
		echo ""
		echo "start container     -x"
		echo "                    such as: `basename $0` -x"
		echo ""
		echo "stop container      -y"
		echo "                    such as: `basename $0` -y"
		echo ""
		echo "check pear          -z"
		echo "                    such as: `basename $0` -z"
		echo ""
		echo "help                [-? | -h]"
		exit 0
		;;
	esac
done

## check
##
if [ "q${MAC_ADDRESS}" = "q" ];then
	echo "you must set '-m mac_address'"
	exit -1
fi

if [ "q${MOUNT_DIR}" = "q" ];then
	echo "you must set '-d disk_dir'"
	exit -2
elif [ ! -d ${MOUNT_DIR} ];then
	echo "'-d disk_dir' not exist"
	exit -3
fi

if [ "q${PLATFORM}" = "q" ];then
	echo "you must set '-p [PEAR_1600_X64_UBUNTU | PEAR_1801_X64_LINUX | PEAR_1900_X64_LINUX]'"
	exit -4
fi

# mkdir images from dockerfile
#
#docker build -t pear_cdn /home/linux/test/test-docker/container/install_service

# make container
#
#--mount type=bind,source=${MOUNT_DIR},target=/sata	\
#-v ${MOUNT_DIR}:/sata	\
#
echo "DOCKER_IMAGE_NAME = ${DOCKER_IMAGE_NAME}"
echo "DOCKER_CONTAINER_NAME = ${DOCKER_CONTAINER_NAME}"
echo "MOUNT_DIR = ${MOUNT_DIR}"
echo "MAC_ADDRESS = ${MAC_ADDRESS}"
echo "PLATFORM = ${PLATFORM}"
docker run -d -it --name ${DOCKER_CONTAINER_NAME} \
-v ${MOUNT_DIR}:/sata \
--mac-address=${MAC_ADDRESS} ${DOCKER_IMAGE_NAME} /bin/bash -c "/sbin/install_service.sh ${PLATFORM} ${VERSION};/bin/bash"

exit 0


