#!/bin/sh

printf "Content-Type: application/json\n\n"

if [ "$REQUEST_METHOD" = "POST" ]
then

    if [ -w "/home/hubclient/files/config.json" ] 
    then
        TMP=$(mktemp)
        cat - | jq .params | cat /home/hubclient/files/config.json - | jq -s add > "$TMP"
        cat "$TMP" > /home/hubclient/files/config.json
        rm "$TMP"
        printf "{}"
    else
         printf "{\"errorCode\":1,\"errorMessage\":\"Unable to write into config file.\"}"
    fi
elif [ "$REQUEST_METHOD" = "GET" ]
then
    printf "{\"params\":"
    cat /home/hubclient/files/config.json
    printf "}"
else
     printf "{\"errorCode\":1,\"errorMessage\":\"Unsupported method %s.\"}" "$REQUEST_METHOD"
fi
