Akira TAGOH 0cdec4
#! /bin/bash -efu
Akira TAGOH 0cdec4
Akira TAGOH 0cdec4
debug() {
Akira TAGOH 5bfbc5
    if [ -n "$DEBUG" ]; then
Akira TAGOH 5bfbc5
	echo "$*" >&2
Akira TAGOH 5bfbc5
    fi
Akira TAGOH 0cdec4
}
Akira TAGOH 0cdec4
Akira TAGOH 0cdec4
msg_usage() {
Akira TAGOH 5bfbc5
    cat <<_EOF_
Akira TAGOH 0cdec4
Run language coverage test.
Akira TAGOH 0cdec4
Akira TAGOH 0cdec4
Usage:
Akira TAGOH 0cdec4
$PROG <options>
Akira TAGOH 0cdec4
Akira TAGOH 0cdec4
Options:
Akira TAGOH 0cdec4
-h, --help		Display this help and exit
Akira TAGOH 0cdec4
-v, --verbose		Turn on debug
Akira TAGOH 0cdec4
-l, --lang=LANG		Test LANG language coverage (default: en)
Akira TAGOH 0cdec4
-p, --path=PATH		Test fonts on PATH
Akira TAGOH b4962d
-n, --name=NAME		Set NAME to store a log file.
Akira TAGOH 0cdec4
-a, --artifactsdir=DIR	test environment dir to store artifacts
Akira TAGOH a18ed3
-e, --exclude=FILE	Exclude FILE to check.
Akira TAGOH a18ed3
-i, --include=FILE	Include File to check.
Akira TAGOH 0cdec4
_EOF_
Akira TAGOH 0cdec4
}
Akira TAGOH 0cdec4
Akira TAGOH 0cdec4
PROG="${PROG:-${0##*/}}"
Akira TAGOH 0cdec4
DEBUG="${DEBUG:-}"
Akira TAGOH 0cdec4
OPT_LANG="${OPT_LANG:-en}"
Akira TAGOH 5bfbc5
OPT_PATH=()
Akira TAGOH 0cdec4
OPT_ARTIFACTS_DIR="${OPT_ARTIFACTS_DIR:-}"
Akira TAGOH a18ed3
OPT_EXCLUDE=()
Akira TAGOH a18ed3
OPT_INCLUDE=()
Akira TAGOH b4962d
OPT_NAME="${OPT_NAME:-}"
Akira TAGOH 0cdec4
Akira TAGOH b4962d
opt=$(getopt -n "$0" --options "hvl:p:n:a:e:i:" --longoptions "help,verbose,lang:,path:,name:,artifactsdir:,exclude:,include:" -- "$@")
Akira TAGOH 0cdec4
eval set -- "$opt"
Akira TAGOH 0cdec4
while [[ $# -gt 0 ]]; do
Akira TAGOH 5bfbc5
    case "$1" in
Akira TAGOH b4962d
	-n|--name)
Akira TAGOH 5bfbc5
	    OPT_NAME="$2"
Akira TAGOH 5bfbc5
	    shift 2
Akira TAGOH 5bfbc5
	    ;;
Akira TAGOH a18ed3
	-i|--include)
Akira TAGOH 5bfbc5
	    OPT_INCLUDE+=("$2")
Akira TAGOH 5bfbc5
	    shift 2
Akira TAGOH 5bfbc5
	    ;;
Akira TAGOH a18ed3
	-e|--exclude)
Akira TAGOH 5bfbc5
	    OPT_EXCLUDE+=("$2")
Akira TAGOH 5bfbc5
	    shift 2
Akira TAGOH 5bfbc5
	    ;;
Akira TAGOH 0cdec4
	-a|--artifactsdir)
Akira TAGOH 5bfbc5
	    OPT_ARTIFACTS_DIR="$2"
Akira TAGOH 5bfbc5
	    shift 2
Akira TAGOH 5bfbc5
	    ;;
Akira TAGOH 0cdec4
	-p|--path)
Akira TAGOH 5bfbc5
	    OPT_PATH+=("$2")
Akira TAGOH 5bfbc5
	    shift 2
Akira TAGOH 5bfbc5
	    ;;
Akira TAGOH 0cdec4
	-l|--lang)
Akira TAGOH 5bfbc5
	    OPT_LANG="$2"
Akira TAGOH 5bfbc5
	    shift 2
Akira TAGOH 5bfbc5
	    ;;
Akira TAGOH 0cdec4
	-v|--verbose)
Akira TAGOH 5bfbc5
	    DEBUG="-v"
Akira TAGOH 5bfbc5
	    shift
Akira TAGOH 5bfbc5
	    ;;
Akira TAGOH 0cdec4
	-h|--help)
Akira TAGOH 5bfbc5
	    msg_usage
Akira TAGOH 5bfbc5
	    exit 0
Akira TAGOH 5bfbc5
	    ;;
Akira TAGOH 0cdec4
	--)
Akira TAGOH 5bfbc5
	    shift
Akira TAGOH 5bfbc5
	    ;;
Akira TAGOH 0cdec4
	*)
Akira TAGOH 5bfbc5
	    msg_usage
Akira TAGOH 5bfbc5
	    exit 1
Akira TAGOH 5bfbc5
    esac
Akira TAGOH 0cdec4
done
Akira TAGOH 0cdec4
Akira TAGOH 0cdec4
if [ -z "$OPT_ARTIFACTS_DIR" ] || [ -z "$OPT_LANG" ] || [ -z "$OPT_PATH" ]; then
Akira TAGOH 5bfbc5
    echo "Use: $PROG -h for help."
Akira TAGOH 5bfbc5
    exit 0
Akira TAGOH 0cdec4
fi
Akira TAGOH 0cdec4
Akira TAGOH 5bfbc5
STR_TEST_DASHED=$(echo "${OPT_NAME:-$OPT_LANG}" | sed -e 's/\//-/g')
Akira TAGOH 0cdec4
Akira TAGOH 0cdec4
clean_exit() {
Akira TAGOH 5bfbc5
    rc=$?;
Akira TAGOH 5bfbc5
    trap - SIGINT SIGTERM SIGABRT EXIT
Akira TAGOH 5bfbc5
    echo "Run test $OPT_LANG: done. Test's exit code: $rc"
Akira TAGOH 5bfbc5
    for pid in $(ps -o pid --no-headers --ppid $$); do
Akira TAGOH 5bfbc5
	if [ -n "$(ps -p $pid -o pid=)" ]; then
Akira TAGOH 5bfbc5
	    kill -s HUP $pid
Akira TAGOH 0cdec4
	fi
Akira TAGOH 5bfbc5
    done
Akira TAGOH 5bfbc5
    local log_file_name="$STR_TEST_DASHED.log"
Akira TAGOH 5bfbc5
    local log_file_path="$OPT_ARTIFACTS_DIR/$log_file_name"
Akira TAGOH 5bfbc5
    local status
Akira TAGOH 5bfbc5
    if [[ $rc -eq 127 ]]; then
Akira TAGOH 5bfbc5
	status="ERROR"
Akira TAGOH 5bfbc5
    elif grep -q "RESULT: WARN" "$log_file_path"; then
Akira TAGOH 5bfbc5
	status="ERROR"
Akira TAGOH 5bfbc5
    elif grep -q "RESULT: FAIL" "$log_file_path"; then
Akira TAGOH 5bfbc5
	status="FAIL"
Akira TAGOH 5bfbc5
    elif grep -q "RESULT: PASS" "$log_file_path"; then
Akira TAGOH 5bfbc5
	status="PASS"
Akira TAGOH 5bfbc5
    elif grep -q "WARN" "$log_file_path"; then
Akira TAGOH 5bfbc5
	status="ERROR"
Akira TAGOH 5bfbc5
    elif grep -q "FAIL" "$log_file_path"; then
Akira TAGOH 5bfbc5
	status="FAIL"
Akira TAGOH 5bfbc5
    elif grep -q "PASS" "$log_file_path"; then
Akira TAGOH 5bfbc5
	status="PASS"
Akira TAGOH 5bfbc5
    else
Akira TAGOH 5bfbc5
	status="ERROR"
Akira TAGOH 5bfbc5
    fi
Akira TAGOH 5bfbc5
    echo "$status $OPT_LANG" >> "$OPT_ARTIFACTS_DIR/test.log"
Akira TAGOH 5bfbc5
    mv "$log_file_path" "$OPT_ARTIFACTS_DIR/${status}-${log_file_name}"
Akira TAGOH 5bfbc5
    local results="$OPT_ARTIFACTS_DIR/results.yml"
Akira TAGOH 5bfbc5
    local result=$(echo $status | tr '[:upper:]' '[:lower:]')
Akira TAGOH 5bfbc5
    test -f "$results" || echo 'results:' > "$results"
Akira TAGOH 5bfbc5
    printf '%s\n' '' \
Akira TAGOH 5bfbc5
	   "- test: $OPT_LANG" \
Akira TAGOH 5bfbc5
	   "  result: $result" \
Akira TAGOH 5bfbc5
	   "  logs:" \
Akira TAGOH 5bfbc5
	   "  - ${status}_${log_file_name}" \
Akira TAGOH 5bfbc5
	   >> "$results"
Akira TAGOH 5bfbc5
    exit 0
Akira TAGOH 0cdec4
}
Akira TAGOH 0cdec4
trap clean_exit SIGINT SIGTERM SIGABRT EXIT
Akira TAGOH 0cdec4
Akira TAGOH 0cdec4
mkdir -p "$OPT_ARTIFACTS_DIR"
Akira TAGOH 0cdec4
export OUTPUTFILE="$(realpath "$OPT_ARTIFACTS_DIR")/$STR_TEST_DASHED-out.log"
Akira TAGOH 5bfbc5
logfile="$OPT_ARTIFACTS_DIR/$STR_TEST_DASHED.log"
Akira TAGOH 5bfbc5
logfile="$(realpath "$logfile")"
Akira TAGOH 5bfbc5
exec > >(tee -a "$logfile") 2>&1
Akira TAGOH 5bfbc5
Akira TAGOH 5bfbc5
expand_regex() {
Akira TAGOH 5bfbc5
    local p ret=()
Akira TAGOH 5bfbc5
    local regex="$1"
Akira TAGOH 5bfbc5
    shift
Akira TAGOH 5bfbc5
    debug "Expanding $regex"
Akira TAGOH 5bfbc5
    for p; do
Akira TAGOH 5bfbc5
	set +f
Akira TAGOH 5bfbc5
	debug "$p: $regex"
Akira TAGOH 5bfbc5
	 (cd $p;
Akira TAGOH 5bfbc5
	  local x=$(find -regextype posix-egrep -regex "./$regex" -print|sed -e 's,^\./,,g')
Akira TAGOH 5bfbc5
	  debug "$x"
Akira TAGOH 5bfbc5
	  ret+=($x)
Akira TAGOH 5bfbc5
	  set -f
Akira TAGOH 5bfbc5
	  echo -n ${ret[@]}
Akira TAGOH 5bfbc5
	 )
Akira TAGOH 5bfbc5
    done
Akira TAGOH 5bfbc5
    echo -n ${ret[@]}
Akira TAGOH 5bfbc5
}
Akira TAGOH 5bfbc5
Akira TAGOH 5bfbc5
iv=()
Akira TAGOH 5bfbc5
ev=()
Akira TAGOH 5bfbc5
x=()
Akira TAGOH 5bfbc5
for p in ${OPT_INCLUDE[@]}; do
Akira TAGOH 5bfbc5
    x=$(expand_regex $p ${OPT_PATH[@]})
Akira TAGOH 5bfbc5
    if [ "x$x" == "x" ]; then
Akira TAGOH 5bfbc5
	echo "RESULT: WARN: No matches on \"$p\". maybe typo or something changed?"
Akira TAGOH 5bfbc5
	continue
Akira TAGOH 5bfbc5
    fi
Akira TAGOH 5bfbc5
    iv=("${iv[@]}" "${x[@]}")
Akira TAGOH 5bfbc5
done
Akira TAGOH 5bfbc5
for p in ${OPT_EXCLUDE[@]}; do
Akira TAGOH 5bfbc5
    x=$(expand_regex $p ${OPT_PATH[@]})
Akira TAGOH 5bfbc5
    if [ "x$x" == "x" ]; then
Akira TAGOH 5bfbc5
	echo "RESULT: WARN: No matches on \"$p\". maybe typo or something changed?"
Akira TAGOH 5bfbc5
	continue
Akira TAGOH 5bfbc5
    fi
Akira TAGOH 5bfbc5
    ev=("${ev[@]}" "${x[@]}")
Akira TAGOH 5bfbc5
done
Akira TAGOH 5bfbc5
OPT_EXCLUDE=(${ev[@]})
Akira TAGOH 5bfbc5
OPT_INCLUDE=(${iv[@]})
Akira TAGOH 5bfbc5
Akira TAGOH 5bfbc5
debug "Path: ${OPT_PATH[@]}"
Akira TAGOH 5bfbc5
debug "Lang: $OPT_LANG"
Akira TAGOH 5bfbc5
debug "Artifacts dir: $OPT_ARTIFACTS_DIR"
Akira TAGOH 5bfbc5
debug "Exclude: ${#OPT_EXCLUDE[@]}: ${OPT_EXCLUDE[@]}"
Akira TAGOH 5bfbc5
debug "Include: ${#OPT_INCLUDE[@]}: ${OPT_INCLUDE[@]}"
Akira TAGOH 0cdec4
Akira TAGOH 0cdec4
contains() {
Akira TAGOH 5bfbc5
    local e match="$1"
Akira TAGOH 5bfbc5
    shift
Akira TAGOH 5bfbc5
    for e; do [[ "$e" == "$match" ]] && return 1; done
Akira TAGOH 5bfbc5
    return 0
Akira TAGOH 0cdec4
}
Akira TAGOH 0cdec4
Akira TAGOH 0cdec4
debug "Check language coverage"
Akira TAGOH 5bfbc5
ret=0
Akira TAGOH 0cdec4
set +f
Akira TAGOH 5bfbc5
for p in ${OPT_PATH[@]}; do
Akira TAGOH 5bfbc5
    for i in `find $p -regex '.*/*\.\(t1\)?\(ttf\)?\(otf\)?\(ttc\)?\(pcf.*\)?\(pfa\)?'`; do
Akira TAGOH 5bfbc5
	set -f
Akira TAGOH afe354
	debug "$i"
Akira TAGOH 0cdec4
	if test -f $i; then
Akira TAGOH 5bfbc5
	    n=`basename $i`
Akira TAGOH 5bfbc5
	    set +e
Akira TAGOH 5bfbc5
	    contains "$n" "${OPT_EXCLUDE[@]}"
Akira TAGOH 5bfbc5
	    ret=$?
Akira TAGOH 5bfbc5
	    set -e
Akira TAGOH 5bfbc5
	    if [ $ret -eq 1 ]; then
Akira TAGOH 5bfbc5
		debug "ignoring $i"
Akira TAGOH 5bfbc5
		continue
Akira TAGOH 5bfbc5
	    fi
Akira TAGOH 5bfbc5
	    if [ ${#OPT_INCLUDE[@]} -ne 0 ]; then
Akira TAGOH 0cdec4
		set +e
Akira TAGOH 5bfbc5
		contains "$n" "${OPT_INCLUDE[@]}"
Akira TAGOH 0cdec4
		ret=$?
Akira TAGOH 0cdec4
		set -e
Akira TAGOH 5bfbc5
		if [ $ret -eq 0 ]; then
Akira TAGOH 5bfbc5
		    debug "$i isn't targeted file"
Akira TAGOH 5bfbc5
		    continue
Akira TAGOH 0cdec4
		fi
Akira TAGOH 5bfbc5
		NOT_MATCHED=("${NOT_MATCHED[@]/$n}")
Akira TAGOH 5bfbc5
	    fi
Akira TAGOH 5bfbc5
	    debug "  $i"
Akira TAGOH 5bfbc5
	    res=`fc-validate -l $OPT_LANG $i || :`
Akira TAGOH 5bfbc5
	    if echo $res | grep -q Missing; then
Akira TAGOH 5bfbc5
		echo "RESULT: FAIL: $i doesn't satisfy $OPT_LANG language coverage."
Akira TAGOH 5bfbc5
		ret=1
Akira TAGOH 5bfbc5
	    else
Akira TAGOH 5bfbc5
		echo "RESULT: PASS: $i satisfy $OPT_LANG language coverage."
Akira TAGOH 5bfbc5
	    fi
Akira TAGOH 0cdec4
	fi
Akira TAGOH 5bfbc5
    done
Akira TAGOH 0cdec4
done
Akira TAGOH 0cdec4
Akira TAGOH 5bfbc5
exit $ret