# 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: sunwei <sunwei218@h-partners.com>
# Maintainer: sunwei <sunwei218@h-partners.com>
pkgname=espeak-ng
pkgver=1.52.0
pkgrel=0
pkgdesc="espeak-ng is a compact, cross-platform, open-source speech synthesis software that can convert text into speech."
url="https://github.com/espeak-ng/espeak-ng"
archs=("armeabi-v7a" "arm64-v8a")
license=("GPL-3.0""Apache-2.0""BSD-2-Clause")
depends=()
makedepends=()

source="https://github.com/espeak-ng/$pkgname/archive/refs/tags/$pkgver.tar.gz"
autounpack=true
downloadpackage=true
buildhostespeak=true
buildtools="configure"

builddir=$pkgname-${pkgver}
packagename=$builddir.tar.gz
patch_flag=false
source envset.sh
host=

prepare() {
    if $buildhostespeak
    then
        cp -rf $builddir $builddir-host-build
        cd $builddir-host-build
        if [ -f ./autogen.sh ]; then
            ./autogen.sh > $publicbuildlog 2>&1
        fi
        mkdir -p mylib
        ./configure --prefix=$PWD/mylib > $publicbuildlog 2>&1
        if [ $? -ne 0 ]; then
            echo "Configure failed! Check $publicbuildlog"
            exit 1
        fi
        make VERBOSE=1 >> $publicbuildlog 2>&1
        make VERBOSE=1 install>> $publicbuildlog 2>&1
        make check >> $buildlog 2>&1
        buildhostespeak=false
        cd $OLDPWD
    fi
    cp -rf $builddir $pkgname-$ARCH-build
    if [ $ARCH == "arm64-v8a" ];then
        setarm64ENV
        host=aarch64-linux
    elif [ $ARCH == "armeabi-v7a" ];then
        setarm32ENV
        host=arm-linux
        export LDFLAGS="${OHOS_SDK}/native/llvm/lib/clang/$CLANG_VERSION/lib/arm-linux-ohos/a7_hard_neon-vfpv4/libclang_rt.builtins.a ${LDFLAGS}"
    else
        echo "${ARCH} not support"
        return -1
    fi
}

build() {
    cd $pkgname-$ARCH-build
    chmod +x ./autogen.sh
    ./autogen.sh > $buildlog 2>&1
    PKG_CONFIG_LIBDIR="${pkgconfigpath}" ./configure "$@" --with-klatt --with-extdict-cmn --host=$host SED=/bin/sed >> $buildlog 2>&1
    $MAKE VERBOSE=1 >> $buildlog 2>&1
    mv ./src/.libs/espeak-ng ./src/.libs/espeak-ng.${ARCH}
    cp -f ../../$pkgname/$builddir-host-build/src/.libs/espeak-ng ./src/.libs/espeak-ng
    ret=$?
    cd $OLDPWD
    return $ret
}

package() {
    cd $pkgname-$ARCH-build
    ${STRIP} --strip-all ./src/.libs/libespeak-ng.so.1.1.51
    $MAKE install-libLTLIBRARIES >> $buildlog 2>&1
    $MAKE install-espeak_ng_includeHEADERS >> $buildlog 2>&1
    ret=$?
    cd $OLDPWD
    if [ $ARCH == "arm64-v8a" ];then
        unsetarm64ENV
    elif [ $ARCH == "armeabi-v7a" ];then
        unsetarm32ENV
    else
        echo "${ARCH} not support"
        return -2
    fi
    unset host
    return $ret
}

check() {
    cd $pkgname-$ARCH-build
    sed -i '/^\.test\.check:$/,/^$/ {/^$/! s/^/# /}' Makefile
    sed -i 's/^check:.*$(TEST:.test=.check)/#&/' Makefile
    sed -i '/^tests\/ssml-fuzzer\.check:/,/^$/ {/^$/! s/^/# /}' Makefile
    make check >> $buildlog-$ARCH 2>&1
    mv ./src/.libs/espeak-ng.${ARCH} ./src/.libs/espeak-ng
    if [ -f ./src/espeak-ng ]; then
        sed -i 's|/usr/bin/sed|/bin/sed|g' ./src/espeak-ng
    fi
    sed -i 's|/usr/bin/sed|/bin/sed|g' ./tests/encoding.test
    sed -i 's|/usr/bin/sed|/bin/sed|g' ./tests/ssml-fuzzer.test
    cd $OLDPWD
    return $ret
    echo "The test must be on an OpenHarmony device!"
}

# 清理环境
cleanbuild(){
    rm -rf ${PWD}/$builddir $builddir-armeabi-v7a-build  $builddir-arm64-v8a-build
    return 0
}