From 85d36bfd4171b610616d0785d1f6e4a098c930f4 Mon Sep 17 00:00:00 2001
From: Fabio Rehm <fgrehm@gmail.com>
Date: Sun, 9 Mar 2014 19:36:48 -0300
Subject: [PATCH] boxes: Implement `make clean`

---
 boxes/Makefile     |  7 +++++--
 boxes/clean.sh     | 27 +++++++++++++++++++++++++++
 boxes/mk-ubuntu.sh |  2 +-
 3 files changed, 33 insertions(+), 3 deletions(-)
 create mode 100755 boxes/clean.sh

diff --git a/boxes/Makefile b/boxes/Makefile
index c7619af..e4273d4 100644
--- a/boxes/Makefile
+++ b/boxes/Makefile
@@ -12,5 +12,8 @@ $(UBUNTU_BOXES):
 	@sudo chmod +rw $(PACKAGE)
 
 clean:
-	@echo "Implement clean"
-	@exit 1
+	@for r in $(UBUNTU_BOXES); do \
+		sudo -E ./clean.sh $${r}\
+		                   vagrant-lxc-base-$${r}-amd64-${TODAY} \
+				               output/vagrant-lxc-$${r}-amd64-${TODAY}.box; \
+		done
diff --git a/boxes/clean.sh b/boxes/clean.sh
new file mode 100755
index 0000000..079498e
--- /dev/null
+++ b/boxes/clean.sh
@@ -0,0 +1,27 @@
+#!/bin/bash
+set -e
+
+source common/ui.sh
+
+export RELEASE=$1
+export CONTAINER=$2
+export PACKAGE=$3
+export LOG=$(readlink -f .)/log/${CONTAINER}.log
+
+info "Cleaning ${RELEASE} artifacts..."
+
+# If container exists, check if want to continue
+if $(lxc-ls | grep -q ${CONTAINER}); then
+  log "Removing '${CONTAINER}' container"
+  lxc-stop -n ${CONTAINER} &>/dev/null || true
+  lxc-destroy -n ${CONTAINER}
+else
+  log "The container '${CONTAINER}' does not exist"
+fi
+
+if [ -e ${PACKAGE} ]; then
+  log "Removing '${PACKAGE}'"
+  rm -f ${PACKAGE}
+else
+  log "The package '${PACKAGE}' does not exist"
+fi
diff --git a/boxes/mk-ubuntu.sh b/boxes/mk-ubuntu.sh
index b83ebe8..563be5f 100755
--- a/boxes/mk-ubuntu.sh
+++ b/boxes/mk-ubuntu.sh
@@ -36,5 +36,5 @@ info "Building box to '${PACKAGE}'..."
 ./common/package.sh ${CONTAINER} ${PACKAGE}
 
 info "Finished building '${PACKAGE}'!"
-log "Run \`sudo lxc-destroy -n ${CONTAINER}\` to remove the container that was created along the way"
+log "Run \`sudo lxc-destroy -n ${CONTAINER}\` or \`make clean\` to remove the container that was created along the way"
 echo