#!/bin/bash

set -ex

gpg --keyserver keys.openpgp.org --recv 7C9E68152594688862D62AF62D9AE806EC1592E2

VERS=$(wget https://dl.gitea.io/gitea/version.json -q -O - | jq -r '.latest.version')

mkdir -p ~/gitea/

if [ ! -f ~/"gitea/gitea-$VERS-linux-amd64.xz" ]; then
    rm -rf ~/"gitea/gitea-*-linux-amd64.xz"
    wget "https://dl.gitea.io/gitea/$VERS/gitea-$VERS-linux-amd64.xz" -O ~/"gitea/gitea-$VERS-linux-amd64.xz"
fi
if [ ! -f ~/"gitea/gitea-$VERS-linux-amd64.xz.asc" ]; then
    rm -rf ~/"gitea/gitea-*-linux-amd64.xz.asc"
    wget "https://dl.gitea.io/gitea/$VERS/gitea-$VERS-linux-amd64.xz.asc" -O ~/"gitea/gitea-$VERS-linux-amd64.xz.asc"
fi

gpg --verify ~/"gitea/gitea-$VERS-linux-amd64.xz.asc" ~/"gitea/gitea-$VERS-linux-amd64.xz"

cp -a ~/"gitea/gitea-$VERS-linux-amd64.xz" .
xz -d "gitea-$VERS-linux-amd64.xz"
mv "gitea-$VERS-linux-amd64" "$IMAGE_NAME_RISOTTO_IMAGE_DIR_TMP/usr/bin/gitea"
chmod +x "$IMAGE_NAME_RISOTTO_IMAGE_DIR_TMP/usr/bin/gitea"
