# 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>, wen fan <wenfan001@chinasoftinc.com>
# Maintainer: Jeff Han <hanjinfei@foxmail.com>

pkgname=graphicsmagick
pkgver=1.3.46
pkgrel=
pkgdesc="GraphicsMagick is the swiss army knife of image processing."
url=http://www.graphicsmagick.org
archs=(armeabi-v7a arm64-v8a)
license=(MIT)
depends=(libpng jpeg lcms2 jasper libwebp libxml2 jbigkit freetype2 zlib_1_3_1 ImageMagick_7_1_1_35)
makedepends=()

builddir=GraphicsMagick-${pkgver}
packagename=${builddir}.tar.xz

#source=https://jaist.dl.sourceforge.net/project/${pkgname}/${pkgname}/${pkgver}/${packagename}?viasf=1
source="https://sourceforge.net/projects/graphicsmagick/files/graphicsmagick/1.3.46/GraphicsMagick-${pkgver}.tar.xz"

autounpack=true
downloadpackage=true
buildtools=configure

source envset.sh
host=

prepare() {
    g++ --version
    strings /usr/lib/x86_64-linux-gnu/libstdc++.so.6 | grep "LIBCXX"
    echo "*************************************************"
    mkdir -p ${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

    return 0
}

build() {
    cd ${builddir}/${ARCH}-build
    ../configure "$@" --host=${host} --without-gs --without-x \
    LDFLAGS="-L${LYCIUM_ROOT}/usr/jasper/${ARCH}/lib \
        -L${LYCIUM_ROOT}/usr/jbigkit/${ARCH}/lib \
        -L${LYCIUM_ROOT}/usr/libwebp/${ARCH}/lib \
        -L${LYCIUM_ROOT}/usr/lcms2/${ARCH}/lib \
        -L${LYCIUM_ROOT}/usr/libxml2/${ARCH}/lib \
        -L${LYCIUM_ROOT}/usr/xz/${ARCH}/lib \
        -L${LYCIUM_ROOT}/usr/zlib_1_3_1/${ARCH}/lib \
        -L${LYCIUM_ROOT}/usr/freetype2/${ARCH}/lib \
        -L${LYCIUM_ROOT}/usr/libjpeg-turbo/${ARCH}/lib \
        -L${LYCIUM_ROOT}/usr/libpng/${ARCH}/lib" \
    CPPFLAGS="-I${LYCIUM_ROOT}/usr/lcms2/${ARCH}/include \
        -I${LYCIUM_ROOT}/usr/libwebp/${ARCH}/include \
        -I${LYCIUM_ROOT}/usr/jbigkit/${ARCH}/include \
        -I${LYCIUM_ROOT}/usr/jasper/${ARCH}/include \
        -I${LYCIUM_ROOT}/usr/zlib_1_3_1/${ARCH}/include \
        -I${LYCIUM_ROOT}/usr/libxml2/${ARCH}/include \
        -I${LYCIUM_ROOT}/usr/libjpeg-turbo/${ARCH}/include \
        -I${LYCIUM_ROOT}/usr/jpeg/${ARCH}/include" \
    > ${buildlog} 2>&1 || return -1

    ${MAKE} >> ${buildlog} 2>&1 || return -1

    # 编译生成测试文件
    ${MAKE} check >> ${buildlog} 2>&1
    cd ${OLDPWD}
    return 0
}

package() {
    cd ${builddir}/${ARCH}-build
    ${MAKE} install >> ${buildlog} 2>&1 || return -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

    return 0
}

check() {
    echo "The test must be on an OpenHarmony device!"
    cd ${builddir}/${ARCH}-build

    # 在测试时只进行测试操作,不编译
    sed -i.bak 's/^check-TESTS:.*/check-TESTS:/' Makefile
    cd ${OLDPWD}
    return 0
}

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