#!/bin/bash

plugin_isupdatable() {
	return 0
}

plugin_isbumpable() {
	return 0
}

plugin_ispushabletoaur() {
	return 0
}

plugin_getpkgfiles() {
	echo "${PKGDIR}/${PKGNAME}/files"
}

plugin_bump() { 
	local -r PKGREL=$(readSetting "${PKGBUILD}" "pkgrel" 0)
	sed -i "s/pkgrel=.*/pkgrel=$((PKGREL+1))/" "${PKGBUILD}"
}

plugin_getlatestversion() {
	local -r GITEABASEURL=$(readSetting "${PKGSETTINGS}" "giteaurl" "")
	local -r GITEAUSER=$(readSetting "${PKGSETTINGS}" "giteauser" "")
	local -r GITEAREPO=$(readSetting "${PKGSETTINGS}" "gitearepo" "")
	local -r GITBRANCH=$(readSetting "${PKGSETTINGS}" "gitbranch" "")
	local -r GITCOMMIT=$(readSetting "${PKGSETTINGS}" "gitcommit" "")
	local -r GITEAURL="${GITEABASEURL}/${GITEAUSER}/${GITEAREPO}.git"
	local -r ONLINECOMMIT=$(git ls-remote "${GITEAURL}" | grep "${GITBRANCH}$" | cut -f 1)
	if [[ "${ONLINECOMMIT}" == "" ]]; then
		echo "Error in Gitea API"
		return 1
	fi
	if [[ "${GITCOMMIT}" == "${ONLINECOMMIT}" ]]; then
		readSetting "${PKGSETTINGS}" "version" "0"
	else
		local -r PKGVERSION=$(readSetting "${PKGSETTINGS}" "pkgver" "0")
		local -r PKGREVISION=$(readSetting "${PKGSETTINGS}" "pkgrev" "0")
		local -r PKGNEWREVISION=$((PKGREVISION + 1))
		echo "${PKGVERSION}.r${PKGNEWREVISION}.${ONLINECOMMIT:0:7}-1"
	fi
}

plugin_update() {
	local -r GITEABASEURL=$(readSetting "${PKGSETTINGS}" "giteaurl" "")
	local -r GITEAUSER=$(readSetting "${PKGSETTINGS}" "giteauser" "")
	local -r GITEAREPO=$(readSetting "${PKGSETTINGS}" "gitearepo" "")
	local -r GITBRANCH=$(readSetting "${PKGSETTINGS}" "gitbranch" "")
	local -r GITCOMMIT=$(readSetting "${PKGSETTINGS}" "gitcommit" "")
	local -r GITEAURL="${GITEABASEURL}/${GITEAUSER}/${GITEAREPO}.git"
	local -r ONLINECOMMIT=$(git ls-remote "${GITEAURL}" | grep "${GITBRANCH}$" | cut -f 1)
	if [[ "${ONLINECOMMIT}" == "" ]]; then
		echo "Error in Gitea API"
		return 1
	fi
	if [[ "${GITCOMMIT}" == "${ONLINECOMMIT}" ]]; then
		return 0
	fi
	
	writeSetting "${PKGSETTINGS}" "gitcommit" "${ONLINECOMMIT}"
	local -r PKGREVISION=$(readSetting "${PKGSETTINGS}" "pkgrev" "0")
	writeSetting "${PKGSETTINGS}" "pkgrev" "$((PKGREVISION + 1))"
	writeSetting "${PKGSETTINGS}" "pkgrel" "1"
	
	writeSetting "${PKGBUILD}" "_giteaurl" "${GITEABASEURL}"
	writeSetting "${PKGBUILD}" "_giteauser" "${GITEAUSER}"
	writeSetting "${PKGBUILD}" "_gitearepo" "${GITEAREPO}"
	writeSetting "${PKGBUILD}" "_gitcommit" "${ONLINECOMMIT}"
	writeSetting "${PKGBUILD}" "pkgname" "${PKGNAME}"
	writeSetting "${PKGBUILD}" "pkgrel" "1"
	local -r PKGVERSION=$(readSetting "${PKGSETTINGS}" "pkgver" "0")
	local -r PKGNEWREVISION=$((PKGREVISION + 1))
	writeSetting "${PKGBUILD}" "pkgver" "${PKGVERSION}.r${PKGNEWREVISION}.${ONLINECOMMIT:0:7}"
}