#!/bin/bash

source .env
REPO=$1

ENDPOINT="https://$DEPLOY_HOST/api/$REPO/upload/"


if [ ! -f "$DEPLOY_FILE" ]; then
    echo "Error: file $DEPLOY_FILE not found"
    exit 1
fi

RESPONSE=$(curl -s -X POST \
     -H "Authorization: Token $DEPLOY_TOKEN" \
     -F "package_file=@$DEPLOY_FILE" \
     -w "%{http_code}" \
     "$ENDPOINT")

HTTP_CODE="${RESPONSE: -3}"

if [ "$HTTP_CODE" -eq 200 ] || [ "$HTTP_CODE" -eq 201 ]; then
    echo "Upload done to $REPO."
else
    echo "Error: failed to uploado to '$REPO'"
    echo "$RESPONSE"
    exit 1
fi