#!/bin/bash
# For A.N.R.GHG Publishing Helper, 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.
# Last modified: 2023-04-14T2243+0200
#
# 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. Next to the plugin set up a sibling folder for JS development.
#
# 3. Initialize an NPM project: `npm init -y`.
#
# 4. Install the WordPress package:
#    `npm install --save-dev --save-exact @wordpress/scripts`.
#
# 5. Open `package.json` and add:
#    "scripts": {
#      "build": "wp-scripts build",
#      "start": "wp-scripts start"
#    },
#
# 6. 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.
# _____________________________________________________________
#
# 7. Install Composer from: https://getcomposer.org/download/
#    for instance Composer version 2.2.14 2022-06-06 16:32:50
#
# 8. Get PHPCS from https://github.com/squizlabs/PHP_CodeSniffer
#    php composer.phar global require "squizlabs/php_codesniffer=*"
#
# 9. 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 since 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.sh
}
#
# 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
	echo -e "\n    The 4 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
fi
