#!/bin/bash

DEST=$1

PKGVERSION="$VERSION"
if [ -n "$(git status --porcelain)" ]; then
	PKGVERSION="$PKGVERSION-$(date +%Y%m%d%H%M%S)-$GITCOMMIT"
fi

PACKAGE_ARCHITECTURE="$(dpkg-architecture -qDEB_HOST_ARCH)"
PACKAGE_URL="http://www.docker.io/"
PACKAGE_MAINTAINER="docker@dotcloud.com"
PACKAGE_DESCRIPTION="Linux container runtime
Docker complements LXC with a high-level API which operates at the process
level. It runs unix processes with strong guarantees of isolation and
repeatability across servers.
Docker is a great building block for automating distributed systems:
large-scale web deployments, database clusters, continuous deployment systems,
private PaaS, service-oriented architectures, etc."
PACKAGE_LICENSE="Apache-2.0"

# Build docker as an ubuntu package using FPM and REPREPRO (sue me).
# bundle_binary must be called first.
bundle_ubuntu() {
	DIR=$DEST/build

	# Include our udev rules
	mkdir -p $DIR/etc/udev/rules.d
	cp contrib/udev/80-docker.rules $DIR/etc/udev/rules.d/

	# Include our init scripts
	mkdir -p $DIR/etc
	cp -R contrib/init/upstart $DIR/etc/init
	cp -R contrib/init/sysvinit $DIR/etc/init.d
	mkdir -p $DIR/lib/systemd
	cp -R contrib/init/systemd $DIR/lib/systemd/system

	mkdir -p $DIR/etc/default
	cat > $DIR/etc/default/docker <<'EOF'
# Docker Upstart and SysVinit configuration file

# Customize location of Docker binary (especially for development testing).
#DOCKER="/usr/local/bin/docker"

# Use DOCKER_OPTS to modify the daemon startup options.
#DOCKER_OPTS="-dns 8.8.8.8"

# If you need Docker to use an HTTP proxy, it can also be specified here.
#export http_proxy=http://127.0.0.1:3128/
EOF

	# Copy the binary
	# This will fail if the binary bundle hasn't been built
	mkdir -p $DIR/usr/bin
	# Copy the binary
	# This will fail if the binary bundle hasn't been built
	cp $DEST/../binary/docker-$VERSION $DIR/usr/bin/docker

	# Generate postinst/prerm/postrm scripts
	cat > /tmp/postinst <<'EOF'
#!/bin/sh
set -e
set -u

getent group docker > /dev/null || groupadd --system docker || true

update-rc.d docker defaults > /dev/null || true
if [ -n "$2" ]; then
	_dh_action=restart
else
	_dh_action=start
fi
service docker $_dh_action 2>/dev/null || true

#DEBHELPER#
EOF
	cat > /tmp/prerm <<'EOF'
#!/bin/sh
set -e
set -u

service docker stop 2>/dev/null || true

#DEBHELPER#
EOF
	cat > /tmp/postrm <<'EOF'
#!/bin/sh
set -e
set -u

if [ "$1" = "purge" ] ; then
	update-rc.d docker remove > /dev/null || true
fi

# In case this system is running systemd, we make systemd reload the unit files
# to pick up changes.
if [ -d /run/systemd/system ] ; then
	systemctl --system daemon-reload > /dev/null || true
fi

#DEBHELPER#
EOF
	# TODO swaths of these were borrowed from debhelper's auto-inserted stuff, because we're still using fpm - we need to use debhelper instead, and somehow reconcile Ubuntu that way
	chmod +x /tmp/postinst /tmp/prerm

	(
		cd $DEST
		fpm -s dir -C $DIR \
		    --name lxc-docker-$VERSION --version $PKGVERSION \
		    --after-install /tmp/postinst \
		    --before-remove /tmp/prerm \
		    --after-remove /tmp/postrm \
		    --architecture "$PACKAGE_ARCHITECTURE" \
		    --prefix / \
		    --depends lxc \
		    --depends aufs-tools \
		    --depends iptables \
		    --deb-recommends ca-certificates \
		    --deb-recommends xz-utils \
		    --description "$PACKAGE_DESCRIPTION" \
		    --maintainer "$PACKAGE_MAINTAINER" \
		    --conflicts docker \
		    --conflicts docker.io \
		    --conflicts lxc-docker-virtual-package \
		    --provides lxc-docker \
		    --provides lxc-docker-virtual-package \
		    --replaces lxc-docker \
		    --replaces lxc-docker-virtual-package \
		    --url "$PACKAGE_URL" \
		    --license "$PACKAGE_LICENSE" \
		    --config-files /etc/udev/rules.d/80-docker.rules \
		    --config-files /etc/init/docker.conf \
		    --config-files /etc/init.d/docker \
		    --config-files /etc/default/docker \
		    --deb-compression gz \
		    -t deb .
		fpm -s empty \
		    --name lxc-docker --version $PKGVERSION \
		    --architecture "$PACKAGE_ARCHITECTURE" \
		    --depends lxc-docker-$VERSION \
		    --description "$PACKAGE_DESCRIPTION" \
		    --maintainer "$PACKAGE_MAINTAINER" \
		    --url "$PACKAGE_URL" \
		    --license "$PACKAGE_LICENSE" \
		    --deb-compression gz \
		    -t deb
	)
}

bundle_ubuntu
