# Copyright (c) 2023 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Contributor: Jeff Han <hanjinfei@foxmail.com>, DongZhengDong <dzhengdong10@gmail.com>
# Maintainer: Jeff Han <hanjinfei@foxmail.com>

pkgname=rsync
pkgver=v3.3.0
pkgrel=0
pkgdesc="Rsync is an open-source, fast, and multi-functional tool that is used to achieve full or incremental synchronization backup of local or remote data. It can be run on multiple operating system platforms."
url="https://github.com/RsyncProject/rsync"
archs=("armeabi-v7a" "arm64-v8a")
license=("GPL 3.0")
depends=("openssl_1_1_1w" "xxHash" "zstd" "zlib_1_3_1")
makedepends=()
source="https://codeload.github.com/RsyncProject/$pkgname/zip/refs/tags/$pkgver"

autounpack=true
downloadpackage=true
patch_flag=true
buildtools="configure"

builddir=$pkgname-3.3.0
packagename=$builddir.zip

source envset.sh
host=
prepare() {
    if $patch_flag
    then
        cd $builddir	
		#此处pathch是因为交叉编译的时候编译脚本运行main函数会失败导致宏未定义,是为了宏能正确定义和修改原库测试脚本在鸿蒙上可以正确运行		
        patch -p1 < `pwd`/../rsync_oh_pkg.patch > $publicbuildlog 2>&1
        patch_flag=false
        cd $OLDPWD
    fi
    cp -arf $builddir $builddir-$ARCH-build
    if [ $ARCH == "armeabi-v7a" ]
    then
        setarm32ENV
        host=arm-linux
    elif [ $ARCH == "arm64-v8a" ]
    then
        setarm64ENV
        host=aarch64-linux
    else
        echo "${ARCH} not support"
	return -1
    fi
}

build() {
    cd $builddir-$ARCH-build
    CPPFLAGS="-I${LYCIUM_ROOT}/usr/openssl_1_1_1w/$ARCH/include -I${LYCIUM_ROOT}/usr/xxHash/$ARCH/include -I${LYCIUM_ROOT}/usr/zstd/$ARCH/include" \
    LDFLAGS="-L${LYCIUM_ROOT}/usr/openssl_1_1_1w/$ARCH/lib -L${LYCIUM_ROOT}/usr/xxHash/$ARCH/lib -L${LYCIUM_ROOT}/usr/zstd/$ARCH/lib" \
    ./configure "$@" --host=$host --disable-md2man  --disable-lz4 > $buildlog 2>&1
    $MAKE V=1 >> $buildlog 2>&1
    ret=$?
    cd $OLDPWD
    return $ret
}

package() {
    cd $builddir-$ARCH-build
    $MAKE install >> $buildlog 2>&1
    cd $OLDPWD
}

check() {
    cd $builddir-$ARCH-build
	# 由于鸿蒙系统创建文件夹无法正确的识别空格和')'需要修改
    sed -i.bak 's|A weird)name|Aweirdname|g' testsuite/00-hello.test
    sed -i.bak 's|+ file3|+file3|g' testsuite/exclude.test
    $MAKE test >> $buildlog 2>&1
    rm -rf testtmp/* >> $buildlog 2>&1
    cd $OLDPWD
    unset host
    if [ $ARCH == "armeabi-v7a" ]
    then
        unsetarm32ENV
    elif [ $ARCH == "arm64-v8a" ]
    then
        unsetarm64ENV
    else
        echo "${ARCH} not support"
	return -1
    fi
    echo "The test must be on an OpenHarmony device!"
}

cleanbuild() {
    rm -rf ${PWD}/$builddir-armeabi-v7a-build ${PWD}/$builddir-arm64-v8a-build ${PWD}/$builddir
}