# Copyright (c) 2025 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: zws <zhouwusong05@chinasoftinc.com>
# Maintainer: zws <zhouwusong05@chinasoftinc.com>

pkgname=chttp
pkgver=master
pkgrel=0 
pkgdesc="一个简单的http/https库,c语言实现,用到了 openssl库,支持 get和post"
url="https://github.com/noxue/chttp"
archs=("armeabi-v7a" "arm64-v8a" "x86_64")
license=(MIT)
depends=("openssl-3.4.0")
makedepends=()
source="https://github.com/noxue/$pkgname.git"

downloadpackage=false
autounpack=false
buildtools=cmake
patchflag=true
cloneflag=true
commitid=a62ed90565c225f4732c919fd9b5d8d794b302d2

builddir=$pkgname-${pkgver}
prepare() {
    if [ $cloneflag == true ]; then
        git clone $source $builddir
        if [ $? -ne 0 ]; then
            return -1
        fi
        
        cd $builddir
        git reset --hard $commitid >> $publicbuildlog 2>&1
        if [ $? -ne 0 ]
        then
            return -1
        fi
        cloneflag=false
        cd $OLDPWD
    fi

    if $patchflag
    then
        cd $builddir
        #修改CMakeLists.txt文件完善编译配置和修改测试用例
        patch -p1 < "$PKGBUILD_ROOT/chttp_oh_pkg.patch" > $publicbuildlog 2>&1
        patchflag=false
        cd $OLDPWD
    fi
    mkdir -p $builddir/$ARCH-build
}

build() {
    cd $builddir
    ${OHOS_SDK}/native/build-tools/cmake/bin/cmake "$@" \
	-DCMAKE_C_FLAGS="-D_GNU_SOURCE" \
    -DOHOS_ARCH=$ARCH -B$ARCH-build -S./ -L > $buildlog 2>&1
    make -j4 -C $ARCH-build >> $buildlog 2>&1
    ret=$?
    cp ./lib/libchttp.a $ARCH-build/
    cp -rf ./bin/* $ARCH-build/
    cd $OLDPWD
    return $ret
}

package() {
    cd $builddir
    mkdir -p $LYCIUM_ROOT/usr/chttp/$ARCH/include
    cp -rf ./include/chttp/*.h $LYCIUM_ROOT/usr/chttp/$ARCH/include
    mkdir -p $LYCIUM_ROOT/usr/chttp/$ARCH/lib
    cp $ARCH-build/libchttp.a $LYCIUM_ROOT/usr/chttp/$ARCH/lib
    cd $OLDPWD
}


check() {
    echo "The test must be on an OpenHarmony device!"
}

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