#!/bin/sh

<<LICENSE

Copyright (C) 2019  Gioacchino Mazzurco <gio@eigenlab.org>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License version 3 as
published by the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

LICENSE

echo "Content-Type: application/json"
echo

[ "$REQUEST_METHOD" != "POST" ] && exit 22
[ -z "$PATH_INFO" ] && exit 22
PATH_INFO="${PATH_INFO#/}"
echo "$PATH_INFO" | grep -q '^[a-zA-Z0-9_-]*$' || exit 22

case "$QUERY_STRING" in
  *"gzip"*)
    zcat - | shared-state reqsync "$PATH_INFO" | gzip -
    ;;
  *)
    cat - | shared-state reqsync "$PATH_INFO"
    ;;
esac
