#!/bin/bash

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

    tmp_file="$(mktemp)"

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

    if onemarket show "${NAME}" >/dev/null
    then
        echo "Le magasin '${NAME}' est déjà présent"
        return 0
    else 
        if res=$(onemarket create "${tmp_file}")
        then
            rm "${tmp_file}"
            echo "Le magasin '${NAME}' est ajouté"
            # il faut redémarrer OpenNebula dans ce cas
            service opennebula restart 
            return "$?"
        else
            echo "Error adding market ${NAME}"
            echo "   ${res}"
            echo "${tmp_file}"
            return 3
        fi
    fi
}

# Adding Main eole market
add_market "Eole Hâpy Market" "https://magasin.eole.education/" "one"

exit 0
