#!/bin/bash
# Operating file name: build.sh
# *** Please do not edit this file in the plugin folder. ***
# * This file may be edited in the build folder, see below. *
# For A.N.R.GHG Publishing Toolkit, with NPM and Webpack,
# or PHPCS, or WP-I18N for the POT, PO and MO files.
#
# 2021-08-30T0820+0200
# 2022-01-01T1019+0100
# 2022-01-16T1822+0100 Add support for Thank You message block.
# 2022-01-27T0327+0100 Add support for References block.
# 2022-06-04T2058+0200 Move build configuration to plugin folder.
# 2023-04-14T0910+0200 Streamline translated PO file updates.
# 2023-10-07T0530+0200 Add support for Include partial block.
# 2025-12-07T1834+0100 Renamed from `build.sh` to `build.txt`.
# Last modified: 2025-12-08T2343+0100
#
# Copyright 2021–2026 ANRGHG
# This file is not part of the A.N.R.GHG Publishing Toolkit.
# A copy of this file is provided in compliance with Open Source standards.
# For this purpose, the file extension is changed from .sh to .txt in
# compliance with WordPress plugin security policies prohibiting application files.
# A.N.R.GHG Publishing Toolkit is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
# A.N.R.GHG Publishing Toolkit is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with A.N.R.GHG Publishing Toolkit.  If not, see <https://www.gnu.org/licenses/>.
# @see COPYING.txt
#
# The final block scripts are built by Node Package Manager based on
# extended JavaScript (JSX) as in the React framework, per WordPress
# instructions and guidelines except that Docker is not used because
# it was uninstallable. The build environment is Webpack, following:
# https://awhitepixel.com/blog/wordpress-gutenberg-complete-guide-development-environment/
#
# Following this tutorial, to set up the build environment, please:
#
# 1. Download and install Node.js from https://nodejs.org/en/ — or:
#    Type `sudo apt install nodejs`, then check `node --version`.
#
# 2. Download and install Node.
#    `sudo apt install nodejs-legacy`
#
# 3. Download and install the Node Package Manager.
#    `sudo apt install npm`
#
# 4. Next to the plugin set up a sibling folder for JS development.
#
# 5. Initialize an NPM project: `npm init -y`.
#
# 6. Install the WordPress package:
#    `npm install --save-dev --save-exact @wordpress/scripts`.
#
# 7. In the $HOME directory, open the file package.json, and
#    in the "scripts" object, replace the "test" object with
#    the "build" and "start" objects:
#
#        "scripts": {
#          "build": "wp-scripts build",
#          "start": "wp-scripts start"
#        },
#
# 8. Set up the `webpack.config.js` to point to the sources in
#    the plugin folder. The compiled block production files are
#    output in the development folder, `build/` subfolder, from
#    whence the relevant files are copied over to the plugin.
# _____________________________________________________________
#
#  9. Install Composer from: https://getcomposer.org/download/ or:
#     `sudo apt install composer`
#     for example Composer version 2.2.14 2022-06-06 16:32:50
#
# 10. Get PHPCS from https://github.com/squizlabs/PHP_CodeSniffer
#     php composer.phar global require "squizlabs/php_codesniffer=*"
#
# 11. git clone -b master https://github.com/WordPress/WordPress-Coding-Standards.git wpcs
#     and configure PHPCS: ./vendor/bin/phpcs --config-set installed_paths ~/wpcs
#     ./vendor/bin/phpcs --standard=WordPress [path-to-code] > [path-to-output]
# _______________________________________________________________________________________
#
# The configuration files package[-lock].json, webpack.config.js and
# the build.sh script are editable in the build folder. Any copies
# in the plugin folder are informative, ensuring transparency and an
# open source project conformant documentation, as the GitHub repo
# of this plugin (https://github.com/anrghg) is not for development.
#
# Alias in .bashrc:
#  alias anrghg='~/[path-to-build-dir]/build.sh'
#
cd $(dirname $0)
announce () {
	echo -e "    Running $(dirname $0)/build.sh\n"
}
update () {
	echo -e "    Updating copies in the '../anrghg/' folder from '$(dirname $0):"
	echo -e "        package.json"
	cp package.json ../anrghg/package.json
	echo -e "        package-lock.json"
	cp package-lock.json ../anrghg/package-lock.json
	echo -e "        webpack.config.js"
	cp webpack.config.js ../anrghg/webpack.config.js
	echo -e "        build.sh\n"
	cp build.sh ../anrghg/build.txt
}
#
# Display available arguments:
if [ "$#" == 0 ]; then
	announce
	echo -e "    Available arguments and options:\n"
	echo -e "    npm        Build Node package with Webpack.\n"
	echo -e "    cs         PHPCS WPCS main assessment."
	echo -e "    pot        Make or update the Portable Object Message Catalog."
	echo -e "    po         That, and update the translations."
	echo -e "    mo         Activate the translations by compiling the machine readable objects."
fi
#
# Run Node Package Manager build process:
if [ "$1" == "npm" ]; then
	announce
	update
	cd ../anrghg-jsx
	echo -e "    npm run build"
	npm run build
	cp build/anrghg-thanks-block.min.js     ../anrghg/admin/blocks/anrghg-thanks-block.min.js
	cp build/anrghg-contents-block.min.js   ../anrghg/admin/blocks/anrghg-contents-block.min.js
	cp build/anrghg-section-block.min.js    ../anrghg/admin/blocks/anrghg-section-block.min.js
	cp build/anrghg-references-block.min.js ../anrghg/admin/blocks/anrghg-references-block.min.js
	cp build/anrghg-include-block.min.js    ../anrghg/admin/blocks/anrghg-include-block.min.js
	echo -e "\n    The 5 minified JS files have been copied over to ../anrghg/admin/blocks/.\n"
fi
#
# Update config w/o running Node Package Manager build process.
if [ "$1" == "up" ]; then
	announce
	update
fi
#
# Run PHPCS assessment with reports output in VSCode.
if [ "$1" == "cs" ]; then
	announce
	if [ "$2" == "" ]; then
		echo -e "    PHPCS-WPCS main reports for display in VSCode.\n"
		~/vendor/bin/phpcs -s --standard=WordPress ../anrghg/ > phpcs-reports/cs-anrghg.txt
		code phpcs-reports/cs-anrghg.txt
	fi
fi
#
# Generate the Portable Object Message Catalog.
# https://developer.wordpress.org/cli/commands/i18n/make-pot/
# 2022-02-19T1743+0100
make_pot () {
	cd ../anrghg
	echo -e "    WP I18N (re-)makes the Portable Object Message Catalog:\n"
	echo -e "    wp i18n make-pot . languages/anrghg.pot\n"
	wp i18n make-pot . languages/anrghg.pot
	sed -i 's/^\(#\..\+\) of the plugin/\1/g' languages/anrghg.pot
}
if [ "$1" == "pot" ]; then
	announce
	make_pot
fi
#
# Update both the Portable Object Message Catalog
# and the translated PO files.
# https://stuff.mit.edu/afs/sipb/project/gtk/gtk_v2.0/doc/gettext/gettext_6.html
# 2022-02-20T0159+0100
if [ "$1" == "po" ]; then
	merge_backup () {
		idversion=$(grep 'Project-Id-Version' anrghg.pot)
		idversion=$(sed 's/\\n/\\\\n/' <<< $idversion)
		echo -e "\n    $1:"
		msgmerge anrghg-$1.po anrghg.pot --update
		if [[ -f anrghg-$1.po~ ]]; then
			mv anrghg-$1.po~ ../../anrghg-jsx/BAK_trash/anrghg-$1.po~
		fi
		sed -i "s/^\"Project-Id-Version:.\+$/$idversion/" anrghg-$1.po
	}
	announce
	cd ../anrghg
	make_pot
	cd languages
	echo -e "\n    Updating existing messages while adding new ones."
	echo -e "    Backup files are moved to BAK_trash/, overwritten next time."
	merge_backup en_GB
	merge_backup en_US
	merge_backup es_ES
	merge_backup fr_FR
fi
#
# Compile the Machine Object files.
# 2022-02-19T2130+0100
if [ "$1" == "mo" ]; then
	compile_mo () {
		echo -e "\n    $1:"
		msgfmt --verbose anrghg-$1.po -o anrghg-$1.mo
	}
	announce
	echo -e "    Compiling the Machine Object files:"
	cd ../anrghg/languages
	compile_mo en_GB
	compile_mo en_US
	compile_mo es_ES
	compile_mo fr_FR
	echo -e "\n    Supporting all existing related sublocales (the exact"
	echo -e "    MO file must exist for the translations to display!):\n"
	set -x
	cp anrghg-en_GB.mo anrghg-en_AU.mo
	cp anrghg-en_GB.mo anrghg-en_CA.mo
	cp anrghg-en_GB.mo anrghg-en_NZ.mo
	cp anrghg-en_GB.mo anrghg-en_SA.mo
	cp anrghg-es_ES.mo anrghg-es_AR.mo
	cp anrghg-es_ES.mo anrghg-es_CL.mo
	cp anrghg-es_ES.mo anrghg-es_CO.mo
	cp anrghg-es_ES.mo anrghg-es_CR.mo
	cp anrghg-es_ES.mo anrghg-es_DO.mo
	cp anrghg-es_ES.mo anrghg-es_EC.mo
	cp anrghg-es_ES.mo anrghg-es_GT.mo
	cp anrghg-es_ES.mo anrghg-es_MX.mo
	cp anrghg-es_ES.mo anrghg-es_PE.mo
	cp anrghg-es_ES.mo anrghg-es_PR.mo
	cp anrghg-es_ES.mo anrghg-es_UY.mo
	cp anrghg-es_ES.mo anrghg-es_VE.mo
	cp anrghg-fr_FR.mo anrghg-fr_BE.mo
	cp anrghg-fr_FR.mo anrghg-fr_CA.mo
	set -
	echo -e "    Updating MO files complete.\n"
fi
