#!/bin/sh
# This file is part of Checkbox.
#
# Copyright 2013 Canonical Ltd.
# Written by:
#   Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
#
# Checkbox is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3,
# as published by the Free Software Foundation.

#
# Checkbox is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Checkbox.  If not, see <http://www.gnu.org/licenses/>.

# Helper script to provision vagrant image 
# ========================================

# Set CHECKBOX_TOP for all shared scripts
export CHECKBOX_TOP=/vagrant 

# Make sure that dpkg never stops to have a chat with the user
export DEBIAN_FRONTEND=noninteractive

# Vagrant sanity check, see if /vagrant really works.
#
# On various installations of Ubuntu this is not the case as the cloud images
# we depend on seem to have broken virtualbox tools installation inside.  This
# causes the filesystem mounted on /vagrant to crash horribly and kill all
# processes attempting to use it.
if ! find /vagrant -mindepth 1 -maxdepth 1 >/dev/null; then
    cat <<__EOM__
E: It seems that /vagrant directory is broken
E: This is a known issue.
E: See this link for a workaround if you have recent-enough VirtualBox:
E: https://bugs.launchpad.net/ubuntu/+source/virtualbox/+bug/1252872
E: Alternatively you can use NFS on any version of VirtualBox:
E: http://docs.vagrantup.com/v2/synced-folders/nfs.html
__EOM__
    exit 1
else
    echo "I: it seems that /vagrant is working okay"
fi

# Add any necessary PPA repositories
$CHECKBOX_TOP/support/install-ppa-dependencies

# Update to have the latest packages, this is needed because the image comes
# with an old (and no longer working) apt cache and links to many packages no
# longer work. This is also needed if the step above actually added any PPAs.
apt-get update

# Update all packages to latest version
# NOTE: this is disabled as it takes a lot of time and we haven't found the
# need to run this. It might have to be re-enabled later if we find that some
# bug (that gets fixed) is affecting our behavior but so far that has not been
# the case.
# apt-get dist-upgrade --yes

set -e
$CHECKBOX_TOP/support/install-deb-dependencies
$CHECKBOX_TOP/support/get-external-tarballs
$CHECKBOX_TOP/support/install-pip-from-source
# Pip executable name is a variable that depends on python version
export PIP=/usr/local/bin/pip-3.*
$CHECKBOX_TOP/support/install-pip-dependencies
$CHECKBOX_TOP/support/develop-projects

# Create a cool symlink so that everyone knows where to go to
ln --no-dereference --force --symbolic /vagrant /home/vagrant/src
