# Luajit does rolling releases from their git repository. They define their
# SEMVER as: MAJOR.MINOR.COMMIT_POSIX_TIMESTAMP 
#
# This script reads MAJOR & MINOR from the specfile and pulld the corresponding
# branch to get the latest commit to get its ID and timestamp and updates the
# specfile with these values.
#
# It also generates the download URL for the tarball for that commit, and
# generates its checksum to update luajit.signatures.json.
#
# The tarball must be manually uploaded to the source storage using the link
# generated by the script.

SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )

LUAJIT_VERSION_MAJOR=$(awk '/%global luajit_version_major/ { print $NF }' *.spec)
LUAJIT_VERSION_MINOR=$(awk '/%global luajit_version_minor/ { print $NF }' *.spec)

LUAJIT_BRANCH="v$LUAJIT_VERSION_MAJOR.$LUAJIT_VERSION_MINOR"

echo "-- create temp folder"
TEMPDIR=$(mktemp -d)

function cleanup {
    echo "+++ cleanup -> remove $TEMPDIR"
    rm -rf $TEMPDIR
}
trap cleanup EXIT

pushd $TEMPDIR

# Check source repository
git clone --depth 1 https://github.com/LuaJIT/LuaJIT luajit
pushd luajit
git checkout "$LUAJIT_BRANCH"
GIT_COMMIT_ID=$(git rev-parse HEAD)
GIT_COMMIT_TIMESTAMP=$(git show -s --format=%ct)
LUAJIT_VERSION="$LUAJIT_VERSION_MAJOR.$LUAJIT_VERSION_MINOR.$GIT_COMMIT_TIMESTAMP"
TARBALL_NAME="luajit-$LUAJIT_VERSION.tar.gz"
DOWNLOAD_LINK="https://github.com/LuaJIT/LuaJIT/archive/$GIT_COMMIT_ID/$TARBALL_NAME"
popd

# Check test repository
git clone --depth 1 https://github.com/LuaJIT/LuaJIT-test-cleanup.git luajit-test
pushd luajit-test
TEST_GIT_COMMIT_ID=$(git rev-parse HEAD)
TEST_TARBALL_NAME="luajit-test-$TEST_GIT_COMMIT_ID.tar.gz"
TEST_DOWNLOAD_LINK="https://github.com/LuaJIT/LuaJIT-test-cleanup/archive/$TEST_GIT_COMMIT_ID/$TEST_TARBALL_NAME"
popd

wget $DOWNLOAD_LINK
CHECKSUM=$(sha256sum "$TARBALL_NAME" | cut -f 1 -d ' ')

wget $TEST_DOWNLOAD_LINK
TEST_CHECKSUM=$(sha256sum "$TEST_TARBALL_NAME" | cut -f 1 -d ' ')

popd

echo "Updating specfile..." 
sed "s/%global upstream_commit .*/%global upstream_commit $GIT_COMMIT_ID/" -i $SCRIPT_DIR/*.spec
sed "s/%global upstream_commit_timestamp .*/%global upstream_commit_timestamp $GIT_COMMIT_TIMESTAMP/" -i $SCRIPT_DIR/*.spec
sed "s/%global upstream_test_commit .*/%global upstream_test_commit $TEST_GIT_COMMIT_ID/" -i $SCRIPT_DIR/*.spec

echo "Updating signatures..."
cat << EOF > luajit.signatures.json
{
    "Signatures": {
        "$TARBALL_NAME": "$CHECKSUM",
        "$TEST_TARBALL_NAME": "$TEST_CHECKSUM"
    }
}
EOF

echo ""
echo "SUCCESSFULLY UPDATED LUAJIT RELEASE:"
echo "Latest commit:           $GIT_COMMIT_ID"
echo "Latest commit timestamp: $GIT_COMMIT_TIMESTAMP"
echo "Latest test repo commit: $TEST_GIT_COMMIT_ID"
echo "DOWNLOAD LINKS:"
echo "$DOWNLOAD_LINK"
echo "$TEST_DOWNLOAD_LINK"

