#!/bin/bash


if [[ -z "${SUDO_USER}" || "${SUDO_USER}" == "${USER}" || "$USER" != "root" ]]; then
    echo "Must be run via 'sudo ${0}'"
    exit 1
fi

	DEBIAN_FRONTEND=noninteractive				\
        && echo -e					       "\
deb	ftp://ftp.ca.debian.org/debian		jessie		main contrib non-free\n\
\n\
deb	ftp://ftp.ca.debian.org/debian		jessie-updates	main contrib non-free\n\
\n\
deb	http://security.debian.org/		jessie/updates	main contrib non-free\n"\
            > /etc/apt/sources.list				\
        && apt-get update					\
        && apt-get -u -y dist-upgrade				\
        && apt-get install -y					\
            apt-show-versions python-pip python-dev		\
            lxc wget bsdtar curl git aufs-tools bridge-utils	\
            emacs24-nox emacs24-el screen			\
            multitail aspell zip docker.io			\
        && apt-get install -y					\
            `apt-show-versions -a				\
                | sed -ne \'s/^linux-image-\(\w\+\):\w*[[:space:]].*installed$/linux-headers-\1/p\'` \
        && sudo addgroup $SUDO_USER staff			\
        && echo "Installing docker and utilities..."		\
        && addgroup $SUDO_USER docker				\
        && sudo -u $SUDO_USER wget -q --output-document=/usr/local/bin/pipework \
                https://raw.github.com/pjkundert/pipework/master/pipework \
        && chmod 0775 /usr/local/bin/pipework			\
        && echo "Enabling IPv4 forwarding..."			\
        && sed -i -e "s/#\(net.ipv4.ip_forward\)=.*/\1=1/" /etc/sysctl.conf \
        && sudo sysctl -w net.ipv4.ip_forward=1		        \
        && echo && echo "Login w/ vagrant ssh"
