#!/sbin/openrc-run

depend()
{
	after transfused
}

start()
{
	ebegin "Starting Docker"

	# check if overriding Docker with a dev bundle
	if mobyconfig exists bundle
	then
		bundle=$(mobyconfig get bundle)
		[ -n "$bundle" -a -d "$bundle" -a -h "$bundle/binary-client/docker" ] && \
		  # note we copy all the symlinks, in case layout changes
		  cp -a $bundle/binary-client/* \
		        $bundle/binary-daemon/* \
		  /usr/bin
		printf "Overriding Docker with provided bundle: $bundle\n"
	fi

	command="${DOCKER_BINARY:-/usr/bin/dockerd}"

	pidfile="/run/docker.pid"

	# mount /run shared eg for volume drivers
	mount --make-shared /run

	# create cgroups mount for systemd containers
	if [ ! -d /sys/fs/cgroup/systemd ]
	then
		mkdir -p /sys/fs/cgroup/systemd
		mount -t cgroup -o none,name=systemd cgroup /sys/fs/cgroup/systemd
	fi

	# Only start with networking on cloud editions
	DOCKER_OPTS="${DOCKER_OPTS} -H unix:///var/run/docker.sock"
	if [ "$(mobyplatform)" = "aws" ] || [ "$(mobyplatform)" = "azure" ]
	then
		# TODO: Don't do this
		DOCKER_OPTS="${DOCKER_OPTS} -H 0.0.0.0:2375"
	fi

	# On desktop editions, force swarm advertise address to be on eth0
	# Currently we do not support multi node swarm on these editions
	# for cloud plartforms the init scripts set this up
	case "$(mobyplatform)" in
	windows|mac)
		DOCKER_OPTS="${DOCKER_OPTS} --swarm-default-advertise-addr=eth0"
		;;
	esac

	# some config is always in /etc/docker cannot specify alternative eg certs
	mkdir -p /etc/docker
	if mobyconfig exists etc/docker
	then
		for f in $(mobyconfig find etc/docker)
		do
			mkdir -p $(dirname $f)
			mobyconfig get $f > $f
		done
	fi

	[ -f /etc/docker/daemon.json ] || printf '{}' > /etc/docker/daemon.json

	if mobyconfig exists network
	then
		NETWORK_MODE="$(mobyconfig get network | tr -d '[[:space:]]')"
		NATIVE_PORT_FORWARDING="$(mobyconfig get native/port-forwarding | tr -d '[[:space:]]')"
		if [ "${NETWORK_MODE}" = "slirp" -o "${NATIVE_PORT_FORWARDING}" = "true" ]; then
			cp /usr/bin/slirp-proxy /usr/bin/docker-proxy
		fi
	fi

	if mobyconfig exists proxy
	then
		export http_proxy="$(mobyconfig get proxy/http)"
		export https_proxy="$(mobyconfig get proxy/https)"
		export no_proxy="$(mobyconfig get proxy/exclude)"
	fi

	# Set Docker to debug debug if not specified in daemon.json
	cat /etc/docker/daemon.json | jq -e 'has("debug")' > /dev/null || DOCKER_OPTS="${DOCKER_OPTS} --debug"

	# choose storage driver
	if ! $(cat /etc/docker/daemon.json | jq -e '."storage-driver"' > /dev/null)
	then
		STORAGE_DRIVER="overlay2"
		[ -d /var/lib/docker/aufs ] && STORAGE_DRIVER="aufs"
		DOCKER_OPTS="${DOCKER_OPTS} --storage-driver ${STORAGE_DRIVER}"
	fi

	# shift logs onto host before docker starts
	# busybox reopens its log files every second
	if cat /proc/cmdline | grep -q 'com.docker.driver'
	then
		DRIVERDIR="/host_docker_app/$(cat /proc/cmdline | sed -e 's/.*com.docker.driver="//' -e 's/".*//')"
		if ! grep -q "osxfs on /var/log" /proc/mounts ; then
			mkdir -p /run/log
			mount -o bind /var/log /run/log
			mount --bind "${DRIVERDIR}/log" /var/log
		fi
	fi

	# set ulimits as high as possible
	ulimit -n 1048576
	ulimit -p unlimited
	# set locked memory higher for varnish
	ulimit -l 82000

	DOCKER_LOGFILE="/var/log/docker.log"

	# Allow iptables to be overriden
	export PATH=/usr/local/sbin:$PATH

	start-stop-daemon --start --quiet \
		--background \
		--exec ${command} \
		--pidfile ${pidfile} \
		--stderr "${DOCKER_LOGFILE}" \
		--stdout "${DOCKER_LOGFILE}" \
		-- --pidfile=${pidfile} ${DOCKER_OPTS}

	ewaitfile 20 ${pidfile} /var/run/docker.sock /var/run/docker/libcontainerd/docker-containerd.sock

	eend $? "Failed to start docker"
}

stop()
{
	# stop docker
	einfo "Stopping docker"
	pidfile="/run/docker.pid"
	start-stop-daemon --stop --quiet --pidfile ${pidfile} --retry 15

        return 0
}
