#!/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 GITHUBUSER=$(readSetting "${PKGSETTINGS}" "githubuser" "")
	local -r GITHUBREPO=$(readSetting "${PKGSETTINGS}" "githubrepo" "")
	local GITHUBTAGNAME=""
	if ! GITHUBTAGNAME=$(curl -sL "https://api.github.com/repos/${GITHUBUSER}/${GITHUBREPO}/tags" | jq -r ".[0].name"); then
		echo "Error in Github API"
		return 1
	fi
	if [ "${GITHUBTAGNAME}" == "" ]; then
		echo "Error in Github API"
		return 1
	fi
	local -r GITHUBVERSION=${GITHUBTAGNAME//[[:alpha:]-]/}
	if [ "${GITHUBVERSION}" == "" ]; then
		echo "Error in Github API"
		return 1
	fi
	local -r PKGVERSION=$(readSetting "${PKGBUILD}" "pkgver" "0")
	if [[ "${PKGVERSION}" == "${GITHUBVERSION}" ]]; then
		readSetting "${PKGSETTINGS}" "version" "0"
	else
		echo "${GITHUBVERSION}-1"
	fi
}

plugin_update() {
	local -r GITHUBUSER=$(readSetting "${PKGSETTINGS}" "githubuser" "")
	local -r GITHUBREPO=$(readSetting "${PKGSETTINGS}" "githubrepo" "")
	local GITHUBTAGNAME=""
	if ! GITHUBTAGNAME=$(curl -sL "https://api.github.com/repos/${GITHUBUSER}/${GITHUBREPO}/tags" | jq -r ".[0].name"); then
		echo "Error in Github API"
		return 1
	fi
	if [ "${GITHUBTAGNAME}" == "" ]; then
		echo "Error in Github API"
		return 1
	fi
	local -r GITHUBVERSION=${GITHUBTAGNAME//[[:alpha:]-]/}
	if [ "${GITHUBVERSION}" == "" ]; then
		echo "Error in Github API"
		return 1
	fi
	local -r PKGVERSION=$(readSetting "${PKGBUILD}" "pkgver" "0")
	if [[ "${PKGVERSION}" == "${GITHUBVERSION}" ]]; then
		return 0
	fi
	
	writeSetting "${PKGSETTINGS}" "pkgrel" "1"

	writeSetting "${PKGBUILD}" "_githubuser" "${GITHUBUSER}"
	writeSetting "${PKGBUILD}" "_githubrepo" "${GITHUBREPO}"
	writeSetting "${PKGBUILD}" "_pkgtagname" "${GITHUBTAGNAME}"
	writeSetting "${PKGBUILD}" "pkgname" "${PKGNAME}"
	writeSetting "${PKGBUILD}" "pkgrel" "1"
	writeSetting "${PKGBUILD}" "pkgver" "${GITHUBVERSION}"
	
	updatePkgSums "${PKGDIR}"/"${PKGNAME}"/files
}