#!/bin/bash

#    This file is part of vlbuildbot.
#
#    vlbuildbot is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License v3 as published by
#    the Free Software Foundation.
#
#    vlbuildbot 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.
#
#    See http://www.gnu.org/licenses/ for complete licensing terms.

#  AUTHOR:  Moises Henriquez


CWD=$PWD
if [ ! -f $HOME/.vlsandbox/vlsandbox.conf ]; then
	if [ -f /etc/vlbuildslave/vlsandbox/vlsandbox.conf ]; then
		mkdir -p $HOME/.vlsandbox/
		cp /etc/vlbuildslave/vlsandbox/vlsandbox.conf $HOME/.vlsandbox/vlsandbox.conf || exit 1
	fi
fi
CONF=${CONF:-$HOME/.vlsandbox/vlsandbox.conf}
EP=${EP:-"/etc/vlbuildslave/vlsandbox/vlsandbox-entrypoint.sh"}
MOTD=${MOTD:-"/etc/vlbuildslave/vlsandbox/vlsandbox.motd"}
DEPPER=${DEPPER:-"/etc/vlbuildslave/vlsandbox/vldepper"}

. $CONF || { echo "Error reading $CONF"; exit 1; }

set -e
# Will enter a sandbox and place the user at /home/slackbuilds
VLARCH=${VLARCH:-$(uname -m)}
VLRELEASE=${VLRELEASE:-"veclinux-7.2"}
NUMJOBS=${NUMJOBS:-"-j2"}

# Determine which docker image to use
case $VLARCH in
	i?86)
		imgname="m0elnx/vector"
		;;
	x86_64)
		imgname="m0elnx/vlocity"
		;;
	*)
		echo "$VLARCH is not (yet) supported"
		exit 1
esac

# Make up the 'tag' to use with the imgname
case $VLRELEASE in
	*-7.0)
		vlnumrel=7.0
		imgname="${imgname}-7.0-std"
		repodirname="vl70"
		;;
	*)
		vlnumrel=$(echo $VLRELEASE | cut -f 2 -d -)
		imgname="${imgname}-${vlnumrel}-bb"
		repodirname="vl$(echo $vlnumrel | sed 's|\.||g')"
esac

# Test to make sure the docker image we want exists on the sytem.  Otherwise, no point
# in contiuing
docker images | grep $imgname  >/dev/null || { \
	echo "ERROR:  The ${imgname}:${imgtag} does not exist or cannot be accessed."; 
	echo "        Check to make sure docker is running and the image is available" ;
	echo "";
	echo "	      If the docker daemon is running, pull the image by doing" ;
	echo "	      'docker pull $imgname'";
	echo "";
	echo "        You can also set VLARCH and VLRELEASE to different values to use"; 
	echo "        a different image."; exit 1; }

# Check to make sure the git tree exitsts before trying to use it
if [ ! -d $GIT_DATADIR/${repodirname} ]; then
	echo "Local clone of the ${repodirname} does not yet exist.  Cloning it."
	(
		mkdir -p $GIT_DATADIR
		cd $GIT_DATADIR || exit 1
		git clone http://bitbucket.org/VLCore/${repodirname}.git || exit 1
	) || exit 1
fi

sandbox_name="$(echo "${imgname%%-*}${vlnumrel/./-}sandbox$$.vlsandbox.net"|cut -f 2 -d /)"
docker_args=""
docker_args+=" -v $GIT_DATADIR/${repodirname}:/home/slackbuilds"
if [ -f $HOME/.gitconfig ]; then
	docker_args+=" -v $HOME/.gitconfig:/root/.gitconfig"
fi
if [ -d $HOME/.ssh ]; then
	docker_args+=" -v $HOME/.ssh/:/root/.ssh/"
fi
docker_args+=" -v ${EP}:/usr/bin/vlsandbox-entrypoint:ro"
docker_args+=" -v ${MOTD}:/etc/motd:ro"
docker_args+=" -v ${DEPPER}:/usr/bin/vldepper:ro"
docker_args+=" --hostname $sandbox_name"
docker_args+=" -e NUMJOBS=${NUMJOBS}"
docker_args+=" ${imgname}:latest /usr/bin/vlsandbox-entrypoint"

# echo ${docker_args}
#exit 0
docker run --rm -ti ${docker_args} || exit 1
#docker run --rm -ti --hostname="vlsandbox${sandbox_id}" ${docker_args} || exit 1

