#!/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 testing containers or environments
# =============================================================

# Vagrant and LXC are explicitly supported, other types of containers (KVM)
# can be made to work but aren't yet guaranteed to.
#
# This script is entirely run *inside* the VM/container.

# Set CHECKBOX_TOP for all shared scripts. This should indicate the location
# of the checkbox source tree in the VM/container.
# If not passed as the first argument to the script, it defaults to /vagrant
# since by default vagrant mounts the current directory in the host system
# as /vagrant in the VM.
export CHECKBOX_TOP=${1:-/vagrant}

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

if [ $CHECKBOX_TOP = "/vagrant" ] ; then
	# 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
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

# Ensure that certain Debian dependencies are *not* installed
$CHECKBOX_TOP/support/remove-deb-anty-dependencies

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.
#
# Vagrant runs the provision script as root, but we want the symlink to /vagrant,
# so we're reduced to assuming the stuff is there and blindly linking.
if [ -d /vagrant ]; then
    [ -d /home/vagrant ] && ln --no-dereference --force --symbolic /vagrant /home/vagrant/src
elif [ "$container" = "lxc" ]; then
    # In LXC, $HOME is inherited from the invoking environment. Link the source
    # location to /home/$user-invoking-lxc/src.  This should work consistently
    # since all the lxc-attach commands will use the same inherited environment, so
    # $user-invoking-lxc should always point to the same place.  Under LXC,
    # however, the home dir may not exist (as lxc creates only the ubuntu user by
    # default), so we need to create the home dir ourselves.
    [ ! -d $HOME ] && mkdir -p $HOME
    ln --no-dereference --force --symbolic $CHECKBOX_TOP $HOME/src
if patch -p0 /usr/lib/python3/dist-packages/pkg_resources.py << EOF
--- pkg_resources.py.orig	2014-05-15 19:39:38.365807720 +0000
+++ pkg_resources.py	2014-05-15 19:44:50.449807720 +0000
@@ -1749,11 +1749,16 @@
                     for dist in find_distributions(os.path.join(path_item, entry)):
                         yield dist
                 elif not only and lower.endswith('.egg-link'):
-                    for line in open(os.path.join(path_item, entry)):
-                        if not line.strip(): continue
-                        for item in find_distributions(os.path.join(path_item,line.rstrip())):
-                            yield item
-                        break
+                    try:
+                        entry_file = open(os.path.join(path_item, entry))
+                        for line in entry_file:
+                            if not line.strip(): continue
+                            for item in find_distributions(os.path.join(path_item,line.rstrip())):
+                                yield item
+                            break
+                    finally:
+                        entry_file.close()
+
 register_finder(ImpWrapper,find_on_path)

 _declare_state('dict', _namespace_handlers={})
EOF
then
	echo "Patching pkg_resources.py succeeded"
else
	echo "Patching pkg_resources.py failed"
	echo "This is not necessarily bad, this is only needed on precise"
fi

fi



