# 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: baijn <1225837220@qq.com>
# Maintainer: baijn <1225837220@qq.com>

pkgname=rapidxml
pkgver=9872a2fae2912697bd9f3a3f499485f3afca6cc0
pkgrel=0
pkgdesc="RapidXml is an attempt to create the fastest XML DOM parser possible, while retaining useability, portability and reasonable W3C compatibility."
url="https://github.com/dwd/rapidxml"
archs=("armeabi-v7a" "arm64-v8a")
license=("BSL-1.0")
depends=()
makedepends=()
source="https://github.com/dwd/$pkgname/archive/$pkgver.zip"

downloadpackage=true
autounpack=true
buildtools="cmake"
builddir=$pkgname-${pkgver}
packagename=$builddir.zip

linuxflag=true

prepare() {
    mkdir -p $builddir/$ARCH-build
    if $linuxflag
    then
        cd $builddir
        mkdir build
        cmake -S . -B build > $buildlog 2>&1
        make -C build >> $buildlog 2>&1
        #只需要生成一次
        linuxflag=false
        cd $OLDPWD
    fi
}

build() {
    cd $builddir
    ${OHOS_SDK}/native/build-tools/cmake/bin/cmake "$@" \
        -B$ARCH-build -S./ > $buildlog 2>&1
    cd $OLDPWD
    cd $builddir/$ARCH-build/CMakeFiles/rapidxml-test.dir
    #替换用例执行路径
    sed -i.bak "s|\/tpc_c_cplusplus\/thirdparty\/rapidxml\/rapidxml-9872a2fae2912697bd9f3a3f499485f3afca6cc0\/${ARCH}-build\/rapidxml-test|\/tpc_c_cplusplus\/thirdparty\/rapidxml\/rapidxml-9872a2fae2912697bd9f3a3f499485f3afca6cc0\/build\/rapidxml-test|" build.make
    cd $OLDPWD
    cd $builddir
    $MAKE VERBOSE=1 -C $ARCH-build  >> $buildlog 2>&1
    ret=$?
    cd $OLDPWD
    return $ret
}

package() {
    mkdir -p $LYCIUM_ROOT/usr/$pkgname/$ARCH/include
    cp $builddir/*.hpp $LYCIUM_ROOT/usr/$pkgname/$ARCH/include
}

check() {
    echo "The test must be on an OpenHarmony device!"
    if [ $ARCH == "armeabi-v7a" ]
    then
        cp ${OHOS_SDK}/native/llvm/lib/arm-linux-ohos/libc++_shared.so $builddir/$ARCH-build
    elif [ $ARCH == "arm64-v8a" ]
    then
        cp ${OHOS_SDK}/native/llvm/lib/aarch64-linux-ohos/libc++_shared.so $builddir/$ARCH-build
    else
        echo "${ARCH} not support"
        return -1
    fi
    cd $builddir/$ARCH-build
    #替换.cmake文件中为正确的路径
    sed -i.bak "/^add_test/s/build/${ARCH}-build/" rapidxml-test[1]_tests.cmake
    cd $OLDPWD
    cd $builddir/$ARCH-build/CMakeFiles/rapidxml-test.dir
    #替换用例执行路径
    sed -i.bak "s|\/tpc_c_cplusplus\/thirdparty\/rapidxml\/rapidxml-9872a2fae2912697bd9f3a3f499485f3afca6cc0\/build\/rapidxml-test|\/tpc_c_cplusplus\/thirdparty\/rapidxml\/rapidxml-9872a2fae2912697bd9f3a3f499485f3afca6cc0\/${ARCH}-build\/rapidxml-test|" build.make
    cd $OLDPWD
}

cleanbuild() {
    rm -rf ${PWD}/$builddir
}