#!/bin/bash

# --------------------------------------------------------------------------
# Copyright 2011, C12G Labs S.L.
#
# This file is part of OpenNebula VMware Drivers.
#
# OpenNebula VMware Drivers is free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation, either version 3 of
# the License, or the hope That it will be useful, but (at your
# option) any later version.
#
# OpenNebula VMware Drivers is distributed in 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 OpenNebula VMware Drivers . If not, see
# <http://www.gnu.org/licenses/>
# --------------------------------------------------------------------------

###############################################################################
# This script is used to copy a VM image (SRC) to the image repository as DST
# Several SRC types are supported
###############################################################################

# -------- Set up the environment to source common tools & conf ------------ 

if [ -z "${ONE_LOCATION}" ]; then
    LIB_LOCATION=/usr/lib/one
else
    LIB_LOCATION=$ONE_LOCATION/lib
fi

. $LIB_LOCATION/sh/scripts_common.sh
source $(dirname $0)/fsrc

SRC=$1
ID=$2
DST=`generate_image_path`

# ------------ Copy the image to the repository ------------- 

case $SRC in
http://*)
    log "Downloading $SRC to the image repository"
    exec_and_log "$WGET -O $DST $SRC" \
        "Error downloading $SRC"
    exec_and_log "chmod 0660 $DST"
    ;;

vmware://*)
    SRC=`echo $SRC|sed  's/vmware:\/\///g'`
    log "Copying local VMware disk folder $SRC to the image repository"
    exec_and_log "cp -rf $SRC $DST" \
        "Error copying $SRC to $DST"
    exec_and_log "chmod 0770 $DST"
    ;;

*)
    log "Copying local image $SRC to the image repository"
    exec_and_log "cp -f $SRC $DST" \
        "Error copying $SRC to $DST"
    exec_and_log "chmod 0660 $DST"
    ;;
esac


echo "$DST"
