#!/bin/bash
function Install_Service_Repo {
    endpoint=$1
    platform=$2

    content=$(wget -q -O - "$endpoint/$platform" || curl -L "$endpoint/$platform")
    retcode=$?
    if [ $retcode -ne 0 ]
    then
        Write_Log "Error receiving source list. Retcode [$retcode], Content [$content]" "ERROR"
        Write_Log "Please ensure, that the following URL is available on a client [$endpoint/$platform]" "WARNING"
        return $retcode
    fi
    
    body=$(echo "$content" | sed  -E 's/\{\"body\":\"(.*)\",\"path\":\"(.*)\"\}/\1/')
    path=$(echo "$content" | sed  -E 's/\{\"body\":\"(.*)\",\"path\":\"(.*)\"\}/\2/')

    ### Remove software lists

    # Linux_deb
    if [[ $platform -eq 2 ]]
    then
        source_list=$(echo -e "$body" | grep 'service_repo')
    fi

    # Linux_rpm
    if [[ $platform -eq 3 ]]
    then
        # shellcheck disable=SC2001
        source_list=$(echo "$body" | sed 's/.*\(\[colibri_service_repo.*\)/\1/g')
    fi

    # Linux_alt
    if [[ $platform -eq 4 ]]
    then
        source_list=$(echo -e "$body" | grep 'service_repo')
    fi

    Write_Log "Install service repo with content [$source_list] to the file [$path]"
    ### Write Sources List
    echo -e "$source_list" > "$path"
}