#!/bin/sh

if [ -z "$1" -o -z "$2" ]; then
    echo ""
    echo " Usage: updateSVN <src-root> <target-resources-dir>"
    echo ""
    exit 1
fi

OWD=`pwd`
cd "$1"

if [ -e .svn ]; then
    SVN=svn
    for paths in /usr/local/bin /opt/R/arm64/bin; do
	if [ -e "$paths/svn" ]; then
	    SVN="$paths/svn"
	fi
    done
    SVNREV=`$SVN info | sed -n 's/Revision:[ \t]*//p'`
    if [ -z "$SVNREV" -a -f .svn/entries ]; then
	SVNREV=`sed -n 's/.*revision="\(.*\)".*/\1/p' .svn/entries|head -n 1`
	if [ -z "$SVNREV" ]; then
	    # SVN 1.4 changed the format - this is a pure guess!! (line 4 = revision)
	    SVNREV=`sed -n -e '4 p' .svn/entries`
	fi
    fi
elif [ -e REVISION ]; then
    SVNREV=`cat REVISION`
elif [ -e .git ]; then
    SVNREV=`git rev-parse --short HEAD`
fi
echo "Revision: $SVNREV"

if [ -z "$SVNREV" ]; then
    echo "Cannot determine SVN revision."
    exit 2
fi

if [ ! -e "$1/Info.plist" ]; then
    echo "Cannot find Info.plist"
    exit 3;
fi

# we no longer use InfoPlist.strings
#sed "s/%SVN%/$SVNREV/g" "$1/English.lproj/InfoPlist.strings" > "$2/English.lproj/InfoPlist.strings"
# Info.plist is actually one level below resources
#sed "s/%SVN%/$SVNREV/g" "$1/Info.plist" > "$2/../Info.plist"
# we have to use the target Info.plist because it has been pre-processed by Xcode
sed "s/%SVN%/$SVNREV/g" "$2/../Info.plist" > "$2/../Info.plist.final"
if [ -e "$2/../Info.plist.final" ]; then
    rm -f "$2/../Info.plist"
    mv "$2/../Info.plist.final" "$2/../Info.plist";
fi

exit 0