#!/bin/bash
download_url='https://developers.yubico.com/yubioath-flutter/Releases'
app='yubico-authenticator'
platform='linux'

# get version in spec file
SPEC_DATA=$(cat yubico-authenticator.spec)
SPEC_REGEX="Version:\s+(([0-9]|\.)*)"
#echo ${SPEC_DATA}
[[ $SPEC_DATA =~ $SPEC_REGEX ]]
current_version=${BASH_REMATCH[1]}

# Get the latest linux tar.gz available
HTML=$(curl -L -s ${download_url})
URL_REGEX="${app}-(([0-9]|\.)*)-${platform}\.tar\.gz"
[[ $HTML =~ $URL_REGEX ]]
latest_version=${BASH_REMATCH[1]}

# check to see if we've built this version last
if [[ "$latest_version" == "" ]]; then
    echo "Error returning latest version from website."
    exit
fi

echo "Current version: ${current_version}"
echo "Latest version: ${latest_version}"

# check to see if we've built this version last
if [[ "$current_version" == "$latest_version" ]]; then
    exit
fi

echo "version does not match"
# change version in spec file and build
sed -i "s/${current_version}/${latest_version}/" yubico-authenticator.spec
./build
