# 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=libresample
pkgver=master
pkgrel=0
pkgdesc="Real-time library for sample rate conversion"
url="https://github.com/minorninth/libresample"
archs=("x86_64" "armeabi-v7a" "arm64-v8a")
license=("BSD")
source="https://github.com/minorninth/$pkgname.git"
downloadpackage=false
autounpack=false
buildtools=configure

commitid=7cb7f9c3f72d4e6774d964dc324af827192df7c3
cloneflag=true
builddir=$pkgname-${pkgver}
source envset.sh

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 [ $ARCH == "armeabi-v7a" ]; then
        setarm32ENV
        host=arm-linux
    elif [ $ARCH == "arm64-v8a" ]; then
        setarm64ENV
        host=aarch64-linux
    elif [ $ARCH == "x86_64" ]; then
        setx86_64ENV
        host=x86_64-linux
    else
        echo "${ARCH} not support"
        return -1
    fi
    mkdir -p $builddir/$ARCH-build
}

build() {
    cd $builddir
    ./configure --host=${host} "$@" > $buildlog 2>&1
    $MAKE -j4 >> $buildlog 2>&1
    ret=$?
    cp -f ./include/libresample.h ./${ARCH}-build/
    cp -f ./libresample.a ./${ARCH}-build/
    cp -f ./tests/testresample ./${ARCH}-build/
    cd $OLDPWD
    return $ret
}

package() {
    cd $builddir
    
    mkdir -p $LYCIUM_ROOT/usr/libresample/$ARCH/include
    cp ./include/libresample.h $LYCIUM_ROOT/usr/libresample/$ARCH/include
    mkdir -p $LYCIUM_ROOT/usr/libresample/$ARCH/lib
    cp $ARCH-build/libresample.a $LYCIUM_ROOT/usr/libresample/$ARCH/lib
    cd $OLDPWD

    if [ $ARCH == "armeabi-v7a" ]; then
        unsetarm32ENV
    elif [ $ARCH == "arm64-v8a" ]; then
        unsetarm64ENV
    elif [ $ARCH == "x86_64" ]; then
        unsetx86_64ENV
    else
        echo "${ARCH} not support"
        return -1
    fi
}

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

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