#!/bin/bash
# Copyright 2013 Canonical Ltd.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; version 2.1.
#
# This program 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Author: Zoltán Balogh <zoltan.balogh@canonical.com>
set -e

EMULATOR_PATH=$1
EMULATOR_NAME=$2
EMULATOR_ARCH=$3
EMULATOR_CHANNEL=$4
EMULATOR_PASSWORD=$5
WHOAMI=$6
USERHOME=$7

GROUP=$(id -gn ${WHOAMI})

if [[ -z ${EMULATOR_CHANNEL} ]]; then
        EMULATOR_CHANNEL="devel"
fi


if [ ! -d ${EMULATOR_PATH} ]
then
	mkdir -p ${EMULATOR_PATH}
fi

HOME=${USERHOME} /usr/bin/ubuntu-emulator create ${EMULATOR_NAME} --arch=${EMULATOR_ARCH} --channel=${EMULATOR_CHANNEL} --password=${EMULATOR_PASSWORD}
chown ${WHOAMI}:${GROUP} ${EMULATOR_PATH} -R

