#!/bin/sh

set -e

if [ -z "$1" ] || [ "$1" = --help ]; then
    cat <<'EOF' 1>&2
Upload a release tarball to the Charliecloud packages repository using wget(1).

Usage:

  $ misc/tar-upload.sh VERSION
EOF
    exit 1
fi

version=$1
echo "version: $version"

tarball=charliecloud-$version.tar.gz
echo "tarball: $tarball"

if ! [ -f "$tarball" ]; then
    echo "tarball does not exist" 1>&2
    exit 1
fi

printf 'GitLab PAT (not echoed)> '
stty_bak=$(stty -g)
stty -echo echonl
read -r pat
stty "$stty_bak"

url="https://gitlab.com/api/v4/projects/62049685/packages/generic/tar/${version}/charliecloud-${version}.tar.gz"
echo "PUTting: $url"

wget --method=PUT \
     --header='Content-Type: application/octet-stream' \
     --header="PRIVATE-TOKEN: $pat" \
     --body-file="$tarball" \
     "$url"
