#!/bin/bash

DRONE_SERVER=${DRONE_SERVER:="https://ci.makerforce.io"}
DRONE_TOKEN=${DRONE_TOKEN:=""}

echo
echo "This script updates drone-gitea-release token on all Drone repos"
echo

path_to_httpie=$(which http)
if [ ! -x "$path_to_httpie" ] ; then
	echo "HTTPie not installed. Please install it at (https://httpie.org/doc#installation)"
fi

path_to_jq=$(which jq)
if [ ! -x "$path_to_jq" ] ; then
	echo "JQ not installed. Please install it at (https://stedolan.github.io/jq/download/)"
fi

if [ -z $DRONE_TOKEN ]; then
	echo "Enter Drone token ($DRONE_SERVER/account/token)"
	echo -n "> "
	read DRONE_TOKEN
	echo
fi

echo "Enter drone-gitea-release token"
echo -n "> "
read gitea_token
echo
echo

echo "Fetching repo list..."
repo_list=$(http GET $DRONE_SERVER/api/user/repos Authorization:"Bearer $DRONE_TOKEN" | jq .[].slug -r)

for r in $repo_list; do

	echo "Updating gitea_token in $r..."
	http --check-status -b PATCH \
		$DRONE_SERVER/api/repos/$r/secrets/gitea_token \
		Authorization:"Bearer $DRONE_TOKEN" \
		data="$gitea_token" \
		pull_request:=false \
	|| http --check-status -b POST \
		$DRONE_SERVER/api/repos/$r/secrets \
		Authorization:"Bearer $DRONE_TOKEN" \
		name="gitea_token" \
		data="$gitea_token" \
		pull_request:=false

done

