#!/bin/bash
if [ -z "$1" ]; then
    echo "Usage1: patch_from_svn PATCHFILE (generated by 'svn diff -c REVISION')"
    echo "Usage2: patch_from_svn SVN_DIR REVISION"
    exit 1
fi
PATCHFILE=$1
if [ -d "$1/.svn" ]; then
    if [ -z "$2" ]; then
        echo "Second argument must be a SVN revision"
        exit 1
    fi
    CURRENT_DIR=`pwd`
    SVN_DIR=$1
    REV=$2
    PATCHFILE=$CURRENT_DIR/$REV.patch
    echo "*** Generating diff of $SVN_DIR@$REV"
    cd $SVN_DIR && svn diff . -c $REV > $PATCHFILE
    cd $CURRENT_DIR
fi

MODIFIED_PATCHFILE=$(basename $(basename $PATCHFILE .patch) .diff).brpc_os.patch

# guess prefix of test files
TEST_PREFIX="test_"
TEST_SUFFIX=
if fgrep -q " bthread/" $PATCHFILE; then
    TEST_PREFIX="bthread_"
    TEST_SUFFIX="_unittest"
elif fgrep -q " bvar/" $PATCHFILE; then
    TEST_PREFIX="bvar_"
    TEST_SUFFIX="_unittest"
fi

cat $PATCHFILE | sed -e 's/src\/baidu\/rpc\//src\/brpc\//g' \
            -e 's/\<baidu\/rpc\//brpc\//g' \
            -e 's/\<baidu\-rpc\([^-]\)/brpc\1/g' \
            -e 's/\<src\/brpc\/test\/test_\(.*\)\.cpp/test\/brpc_\1_unittest.cpp/g' \
            -e 's/\<test\/test_bthread\.cpp/test\/bthread_unittest.cpp/g' \
            -e 's/\<test\/test_object_pool\.cpp/test\/object_pool_unittest.cpp/g' \
            -e 's/\<test\/test_resource_pool\.cpp/test\/resource_pool_unittest.cpp/g' \
            -e "s/\<test\/test_\(.*\)\.cpp/test\/${TEST_PREFIX}\1${TEST_SUFFIX}.cpp/g" \
            -e 's/\<namespace \+baidu *{/namespace brpc {/g' \
            -e 's/\<namespace \+rpc *{//g' \
            -e 's/} *\/\/ \+namespace \+baidu/} \/\/ namespace brpc/g' \
            -e 's/} *\/\/ \+namespace \+rpc\>//g' \
            -e 's/\<baidu::rpc::/brpc::/g' \
            -e 's/\<rpc::/brpc::/g' \
            -e 's/\<base::/butil::/g' \
            -e 's/\<BAIDU_RPC_/BRPC_/g' \
            -e 's/\<protocol\/\(.*\)\.proto/src\/\1.proto/g' \
            -e 's/TEST_F(HttpMessageTest/TEST(HttpMessageTest/g' \
            -e 's/TEST_F(URITest/TEST(URITest/g' \
            -e 's/ bthread_cond\.cpp/ src\/bthread\/condition_variable.cpp/g' \
            -e 's/ bthread_\([^.]*\)\.cpp/ src\/bthread\/\1.cpp/g' \
            -e 's/ bthread_\([^.]*\)\.h/ src\/bthread\/\1.h/g' \
            -e 's/ bthread\.\(h\|cpp\)/ src\/bthread\/bthread.\1/g' \
            -e 's/<\(brpc\/[^>]*\)>/"\1"/g' \
            -e 's/<\(bvar\/[^>]*\)>/"\1"/g' \
            -e 's/<base\/\([^>]*\)>/"butil\/\1"/g' \
            -e 's/<\(bthread\/[^>]*\)>/"\1"/g' \
            -e 's/<\(mcpack2pb\/[^>]*\)>/"\1"/g' \
            -e 's/\<protobuf_json\>/json2pb/g' \
            -e 's/src\/json_to_pb/src\/json2pb\/json_to_pb/g' \
            -e 's/src\/pb_to_json/src\/json2pb\/pb_to_json/g' \
            -e 's/test\/test_protobuf_json/test\/brpc_protobuf_json_unittest/g' \
            -e 's/ base\// src\/butil\//g' \
            -e 's/ bthread\// src\/bthread\//g' \
            -e 's/ bvar\// src\/bvar\//g' \
            > $MODIFIED_PATCHFILE
EXTRA_ARGS=
if [ -z "$DO_RUN" ]; then
    EXTRA_ARGS="--dry-run $EXTRA_ARGS"
    echo "*** This is a dry-run. To really apply, run: DO_RUN=1 tools/patch_from_svn $1"
fi
patch -p0 -u $EXTRA_ARGS < $MODIFIED_PATCHFILE
