#!/bin/bash
# Copyright 2014 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: Benjamin Zeller <zeller.benjamin@canonical.com>

ARCHITECTURE=$1
FRAMEWORK=$2
SERIES=$3
NAME=$4
SCRIPTPATH=$(dirname $0)

click chroot -a $ARCHITECTURE -f $FRAMEWORK -n $NAME exists

if [ $? -eq 0 ]
then
	echo "The click target ${NAME}-${FRAMEWORK}-${ARCHITECTURE} exists already"
        exit 1
fi

click chroot -a $ARCHITECTURE -f $FRAMEWORK -n $NAME create

if [ $? -eq 0 ]
then
    echo "The click target was created successfully, installing required packages"

    click chroot -a $ARCHITECTURE -f $FRAMEWORK  -n $NAME maint apt-get install -y cmake

    exit 0
fi

echo "There was an error creating the click target, cleaning up"

"${SCRIPTPATH}/click_destroy_target" ${ARCHITECTURE} ${FRAMEWORK} ${SERIES} ${NAME}

exit 1
