#!/bin/bash

function add_market()
{
    NAME=$1
    ENDPOINT=$2
    MD=$3

    tmp_file=$(mktemp)

    echo "NAME = \"${NAME}\"" >> ${tmp_file}
    echo "ENDPOINT = \"${ENDPOINT}\"" >> ${tmp_file}
    echo "MARKET_MAD = \"${MD}\"" >> ${tmp_file}

    res=$(onemarket list | grep "${NAME}")
    if [[ $? -ne 0 ]]
    then
        res=$(onemarket create ${tmp_file})
        if [[ ${?} -eq 0 ]]
        then
            rm ${tmp_file}
        else
            echo "Error adding market ${NAME}"
            echo "   ${res}"
            echo "${tmp_file}"
            return 3
        fi
    fi
    return 0
}

DEP=$(CreoleGet activer_deploiement_automatique non)

if [[ $DEP == "oui" ]]
then
    # Adding Main eole market
    add_market "Eole Hâpy Market" "https://lab1.labs.eole.education/" "one"
    if [[ ${?} -eq 0 ]]
    then
        service opennebula restart 
        exit $?
    else
        exit 2
    fi
fi
exit 0