#!/usr/bin/env bash

# Get the plugin slug from this git repository.
PLUGIN_SLUG="${PWD##*/}"

# Get the current release version
TAG=$(sed -e "s/refs\/tags\///g" <<< $GITHUB_REF)

# Replace the version in these 2 files.
sed -i -e "s/__STABLE_TAG__/$TAG/g" ./readme.txt
sed -i -e "s/__STABLE_TAG__/$TAG/g" "./$PLUGIN_SLUG.php"

# Get the SVN data from wp.org in a folder named `svn`
svn co --depth immediates "https://plugins.svn.wordpress.org/$PLUGIN_SLUG" ./svn

svn update --set-depth infinity ./svn/trunk
svn update --set-depth infinity ./svn/assets
svn update --set-depth infinity ./svn/tags/$TAG

# Copy files from repository to `svn/trunk`
cp -R ./* ./svn/trunk

# Copy the images from `assets` to `svn/assets`
cp -R ./wp-org-assets/* ./svn/assets

# 3. Switch to SVN directory
cd ./svn

# Remove `wp-org-assets` folder from `trunk`
rm -rf ./trunk/wp-org-assets

# Remove `node_modules` folder from `trunk`
rm -rf ./trunk/node_modules

# Prepare the files for commit in SVN
svn add --force trunk
svn add --force assets

# Create the version tag in svn
svn cp trunk tags/$TAG

# Prepare the tag for commit
svn add --force tags

# Commit files to wordpress.org.
svn ci  --message "Release $TAG" \
        --username $SVN_USERNAME \
        --password $SVN_PASSWORD \
        --non-interactive
