#!/bin/bash
#
#  Copyright (c) 2014,2015 Samuel Degrande
#
#  This file is part of Freedroid
#
#  Freedroid 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.
#
#  Freedroid 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 Freedroid; see the file COPYING. If not, write to the
#  Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
#  MA  02111-1307  USA
#

# This script analyzes a set of files to generate the list of the files
# containing marked strings (to create a POTFILES.in).
#
# Inspired by the update-pot script of the gitg project.

declare -a include
declare -a exclude

while getopts ":i:x:" opt ; do
	case $opt in
		"i")
			include[${#include[@]}]=$OPTARG;;
		"x")
			exclude[${#exclude[@]}]=$OPTARG;;
		":")
			echo "arg needed for \"$OPTARG\" option" >&2
			exit 1;;
		\?)
			echo "\"$OPTARG\" option is invalid" >&2
			exit 1;;
	esac
done

shift $((OPTIND-1))
if [ $# -ne 0 ] ; then
	echo "$0: too many arguments" >&2
	exit 1
fi

top=$(git rev-parse --show-toplevel)

# Get the list of the files to inspect from 'git ls-files'.
#
# The order of the generated list depends of factors such as the OS or
# the locale. A different order means a different POTFILES.in, and so
# a re-generation of the PO files.
# To avoid unwanted modifications of PO files, we sort the list in a
# well known order: files first and then directories ("width first
# traversal"), using byte ordering (hence the 'LC_ALL=C')
declare -A filesmap # Key: directory name - Value: list of files in that dir
dirlist=""
for f in $(cd "$top" && git ls-files --no-empty-directory --exclude-standard ${include[@]}) ; do
  dirn=$(dirname $f)
  filesmap[$dirn]="${filesmap[$dirn]} $f"
  dirlist="$dirlist $dirn"
done
allfiles=""
for d in $(echo $dirlist | tr " " "\n" | LC_ALL=C sort -u -f) ; do
	allfiles="$allfiles $(echo ${filesmap[$d]} | tr " " "\n" | LC_ALL=C sort -f)"
done
unset filesmap

# Only keep files containing gettext markers

echo "# List of source files containing translatable strings, automatically"
echo "# generated by update-potfiles. Do not edit by hand."

for f in $allfiles ; do
	# Remove exclude files
	excluded=0
	for exc in ${exclude[@]} ; do
		if [[ " $f" == " ${exc}" || " $f" =~ " ${exc}/" ]] ; then
			excluded=1
			break
		fi
	done
	[ $excluded -eq 1 ] && continue

	# Markers to check depend on the file type
	case ${f##*.} in
		# source files
		"c" | "h" )
			if grep -E '_\(\"' "$top/$f" &>/dev/null; then
				echo $f;
			fi;;
		# lua files
		"lua" )
			if grep -E '_\"|_\[\[' "$top/$f" &>/dev/null; then
				echo $f;
			fi;;
		# data files
		"dat" | "droids" )
			if grep -E '_\"' "$top/$f" &>/dev/null; then
				echo $f;
			fi;;
	esac
done
