#!/bin/bash

##########################################################################
# CONFIGURATION SETTINGS
##########################################################################

# The directory where the __htdocs__ of the standard (English) mahara
# installation is located.
STANDARD_DIR="/usr/src/mahara/mahara/htdocs"

# The directory where the language pack mahara installation is
# located. This has to be the directory where the __root__ lang directory
# is located.
LANGPACK_DIR="/usr/src/mahara/mahara-es"

# The code of your language pack (es, eu, fr, ...)
LANGPACK="es"


##########################################################################
#
# NO USER SERVICEABLE PARTS BELOW THIS LINE :-)
#
##########################################################################

check_mahara_dir()
{
	# Do a minimal check to see if $1 looks like a mahara installation
	if [ ! -d $1 ]
	then
		echo "${1} is not a directory!"
		return 1
	elif [ \( ! -d ${1}/lang \) -a \( ! -f ${1}/init.php \) ]
	then
		echo "${1} doesn't look like a mahara installation!"
		return 1
	fi
	return 0
}

verbose=0
if [ "$1" == "-v" ]
then
	verbose=1
fi

# Use forward slashes in paths, even if we are on Windows. msysgit (ang Cygwin) have
# less trouble with them.
STANDARD_DIR=`echo $STANDARD_DIR | tr '\\\\' '/' `
LANGPACK_DIR=`echo $LANGPACK_DIR | tr '\\\\' '/'`

check_mahara_dir $STANDARD_DIR || exit 1
check_mahara_dir $LANGPACK_DIR || exit 1

std_tmp=${TMP:=/tmp}/std.$$
lng_tmp=$TMP/lng.$$
diff_tmp=$TMP/diff.$$

echo ""
echo "Missing or superflous files in '${LANGPACK}' language pack..."
echo "======================================================================"
echo "    '+' = missing from standard version"
echo "    '-' = doesn't exist in standard version any more"

(cd $STANDARD_DIR ;
 find . -type f -path "*/lang/en.utf8/*" -exec bash -c "i={}; echo \${i/en.utf8/${LANGPACK}.utf8}" ";" | sort > $std_tmp )
(cd $LANGPACK_DIR ;
 find . -type f -path "*/lang/${LANGPACK}.utf8/*" -print | sort > $lng_tmp)

diff -u $lng_tmp $std_tmp | grep "^[-+][^-+]" > $diff_tmp
if [ -s $diff_tmp ]; then
	# Remove the leading './' string from the files' path
	# and leave a space betwen the +/- and the path (same as
	# below for language strings).
	sed -e 's#^\([+-]\)\./\(.*$\)#\1 \2#' < $diff_tmp
else
	echo "-->> No missing or superflous files"
fi
rm -f $std_tmp $lng_tmp $diff_tmp


echo ""
echo "Missing or superflous language strings in '${LANGPACK}' language pack..."
echo "======================================================================"
echo "    '+' = missing from standard version";
echo "    '-' = doesn't exist in standard version any more";
strings=0
for i in `find $STANDARD_DIR -type f -path "*/lang/en.utf8/*" -name "*.php" -print`
do
 	if php -d display_errors=off <<-EOF
		<?php
		\$verbose = $verbose;
		if(!function_exists('get_config')) {
			function get_config(\$value) {
				return " . get_config('\$value') . ";
			}
		}
		define ('INTERNAL', true);

		\$file_std = '${i#${STANDARD_DIR}/*}';
		\$file_lng = str_replace('en.utf8', '${LANGPACK}.utf8', \$file_std);

		include ('${STANDARD_DIR}/'.\$file_std);
		\$std_keys = \$string;
		unset(\$string);
		include ('${LANGPACK_DIR}/'.\$file_lng);
		\$lng_keys = \$string;

		\$diff_keys = array_diff_key(\$std_keys, \$lng_keys);
		\$diff_keys2 = array_diff_key(\$lng_keys, \$std_keys);
		if (!empty(\$diff_keys) || !empty(\$diff_keys2)) {
			echo "File: ".\$file_lng."\n";
			foreach (\$diff_keys as \$key => \$value) {
				if (\$verbose) {
					print "+ \\\$string['".\$key."'] = \$std_keys[\$key];\n";
				} else {
					print "+ \\\$string['".\$key."']\n";
				}
			}
			foreach (\$diff_keys2 as \$key => \$value) {
				if (\$verbose) {
					print "- \\\$string['".\$key."'] = \$lng_keys[\$key];\n";
				} else {
					print "- \\\$string['".\$key."']\n";
				}
			}
			exit(1);

		}
		exit(0);
		?>
EOF
	then
		strings=$strings
	else 
		strings=$((strings + 1))
		echo ""
	fi
done
if [ $strings -eq 0 ]
then
	echo "-->> No missing or superflous language strings"
fi
