#!/bin/sh
#
# Run a test or application in Wine in all available translations
#
# Copyright 2008 by Detlef Riekenberg
# License: LGPL v2
#
# WINETEST_DELAY: extra delay in seconds
# WINETEST_SIZE: prefered size of the virtual Desktop
#


appname=`basename "$0"`

MINIMAL_DELAY="3"

#if [ -z "$WINEPREFIX" ]
#then
#    WINEPREFIX="~/.wine"
#fi

#Where is the root of your wine source tree?
srcprefix="$WINEPREFIX/src/"

#When running from the build-tree
if [ -e "Make.rules.in" ]
then
    srcprefix=""
fi

if [ -z "$WINETEST_SIZE" ]
then
    WINETEST_SIZE="640x480"
fi

# select dir with rc-files (relative to srcprefix)
# needed, when running wine applications
rcdir=""
if [ "$1" = "--rcdir" ]
then
    shift
    rcdir="$1"
    shift
fi

fullname="$1"
    
if [ -z "$fullname" ]
then
    echo "usage: $appname [--rcdir relative_path/to/rc_files.rc] path/filename_to_run"
    exit
fi


testname=`basename "$fullname"`
if [ -z "$rcdir" ]
then
    testdir=`dirname "$fullname"`
    rcdir=`dirname "$testdir"`
fi


echo "testing $testname with resources in $rcdir"

if [ -n "$WINEPREFIX" ]
then

    all_rc=`ls 2>/dev/null $srcprefix$rcdir/*_*.rc $srcprefix$testdir/*_*.rc` 

    if [ -z "$all_rc" ]
    then
        all_rc=`ls 2>/dev/null $srcprefix$rcdir/??.rc $srcprefix$testdir/??.rc` 
    fi

    echo "#####"

    for data in $all_rc
    do
#	echo $data
	rcfile=`basename "$data" ".rc"`

	rcname=`echo "$rcfile" | cut -d "_" -f2 | tr "QWERTZUIOPASDFGHJKLYXCVBNM" "qwertzuiopasdfghjklyxcvbnm"`
	rcname_uc=`echo "$rcname" | tr  "qwertzuiopasdfghjklyxcvbnm" "QWERTZUIOPASDFGHJKLYXCVBNM"`
	if [ -n "$rcname" ]
	then
	    if [ "$rcname" = "en" ]
	    then
		rcname_uc="US"
	    fi
	    if [ "$rcname" = "sv" ]
	    then
		rcname_uc="SE"
	    fi

	    #locale -a | grep "$rcname"
	    all_lang=`locale -a | grep "$rcname""_""$rcname_uc"`

	    if [ -z "$all_lang" ]
	    then
		all_lang=`locale -a | grep "$rcname""_" | head -n 1`
	    else
    		if [ "$rcname" = "pt" ]
		then
		    all_lang="`locale -a | grep "pt_BR"` $all_lang"
		fi
	    fi

	    if [ -n "$all_lang" ]
	    then
    		for thislang in $all_lang
		    do
		        echo "$thislang" 

    LC_ALL="$thislang" wine explorer.exe /desktop="$rcfile($thislang),"$WINETEST_SIZE "$fullname" $2 $3 $4 $5  $6 & 
    		    sleep $MINIMAL_DELAY
    		    if [ -n "$WINETEST_DELAY" ]
    		    then
        		    sleep $WINETEST_DELAY
                fi
    		done
	    else
		echo "No locale installed for: $rcname"
	    fi
	fi
	    

    done

else
    echo "\$WINEPREFIX is empty"
fi


