# Copyright (c) 2026 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>
# Maintainer: Jeff Han <hanjinfei@foxmail.com>

pkgname=FreeImage
pkgver=3.19.10
pkgrel=0
pkgdesc="FreeImage is an Open Source library project for developers who would like to support popular graphics image formats like PNG, BMP, JPEG, TIFF and others as needed by today's multimedia applications. FreeImage is easy to use, fast, multithreading safe, and cross-platform (works with Windows, Linux and Mac OS X)."
url="https://github.com/danoli3/FreeImage"
archs=("armeabi-v7a" "arm64-v8a")
license=("FreeImage Public License - Version 1.0")
depends=()
makedepends=()

source="https://github.com/danoli3/$pkgname/archive/refs/tags/$pkgver.tar.gz"

autounpack=true
downloadpackage=true

builddir=$pkgname-${pkgver}
packagename=$builddir.tar.gz
patchflag=true

prepare() {
    mkdir -p $builddir/$ARCH-build
    if $patchflag
    then
        cd $builddir
        # delete main func in so (BUILD_LIBTIFF && BUILD_OPENEXR src file have main func), fix openexr build bug
        patch -p1 < $PKGBUILD_ROOT/FreeImage_oh_pkg.patch > $publicbuildlog 2>&1
        patchflag=false

        local cmake_file="${PWD}/CMakeLists.txt"

        wget -O ../libtiff-4.7.1.zip https://gitee.com/mirrors/libtiff/repository/archive/v4.7.1.zip >> $publicbuildlog 2>&1
        unzip ../libtiff-4.7.1.zip -d ../ >> $publicbuildlog 2>&1
        local tiff_src_dir="${PWD}/../libtiff-v4.7.1/libtiff"
        local tiff_dest_dir="${PWD}/Source/LibTIFF4"
        for file in "$tiff_dest_dir"/*; do
            if [ -f "$file" ]; then
                filename=$(basename "$file")
                src_file="$tiff_src_dir/$filename"
                if [ -f "$src_file" ]; then
                    cp -f "$src_file" "$file"
                fi
            fi
        done
        cp -f "${tiff_src_dir}/tiffvers.h.cmake.in" ${tiff_dest_dir}
        if [ -f "$cmake_file" ]; then
            sed -i '/^if(BUILD_LIBTIFF)$/a\
                set(LIBTIFF_MAJOR_VERSION 4)\
                set(LIBTIFF_MINOR_VERSION 7)\
                set(LIBTIFF_MICRO_VERSION 1)\
                set(LIBTIFF_VERSION "4.7.1")\
                set(LIBTIFF_RELEASE_DATE 20250911)\
                \
                configure_file(\
                    ${CMAKE_CURRENT_SOURCE_DIR}/Source/LibTIFF4/tiffvers.h.cmake.in\
                    ${CMAKE_CURRENT_SOURCE_DIR}/Source/LibTIFF4/tiffvers.h\
                    @ONLY\
                )' "$cmake_file"
        fi


        wget -O ../zlib-1.3.1.2.zip https://gitee.com/mirrors/zlib/repository/archive/v1.3.1.2.zip >> $publicbuildlog 2>&1
        unzip ../zlib-1.3.1.2.zip -d ../ >> $publicbuildlog 2>&1
        local zlib_src_dir="${PWD}/../zlib-v1.3.1.2"
        local zlib_dest_dir="${PWD}/Source/ZLib"
        for file in "$zlib_dest_dir"/*; do
            if [ -f "$file" ]; then
                filename=$(basename "$file")
                src_file="$zlib_src_dir/$filename"
                if [ -f "$src_file" ]; then
                    cp -f "$src_file" "$file"
                fi
            fi
        done


        wget -O ../libpng-1.6.53.zip https://gitee.com/mirrors/libpng/repository/archive/v1.6.53.zip >> $publicbuildlog 2>&1
        unzip ../libpng-1.6.53.zip -d ../ >> $publicbuildlog 2>&1
        local libpng_src_dir="${PWD}/../libpng-v1.6.53"
        local libpng_dest_dir="${PWD}/Source/LibPNG"
        for file in "$libpng_dest_dir"/*; do
            if [ -f "$file" ]; then
                filename=$(basename "$file")
                src_file="$libpng_src_dir/$filename"
                if [ -f "$src_file" ]; then
                    cp -f "$src_file" "$file"
                fi
            fi
        done
        cp -rf "$libpng_src_dir/arm" "$libpng_dest_dir"
        if [ -f "$cmake_file" ]; then
            sed -i '/\.\/Source\/LibPNG\/pngstruct\.h/a\
            Source/LibPNG/arm/arm_init.c\
            Source/LibPNG/arm/filter_neon_intrinsics.c\
            Source/LibPNG/arm/filter_neon.S\
            Source/LibPNG/arm/palette_neon_intrinsics.c' "$cmake_file"
        fi


        wget -O ../openjpeg-2.5.4.zip https://gitee.com/mirrors/openjpeg/repository/archive/v2.5.4.zip >> $publicbuildlog 2>&1
        unzip ../openjpeg-2.5.4.zip -d ../ >> $publicbuildlog 2>&1
        local openjpeg_src_dir="${PWD}/../openjpeg-v2.5.4/src/lib/openjp2"
        local openjpeg_dest_dir="${PWD}/Source/LibOpenJPEG"
        if [ -d "$openjpeg_src_dir" ] && [ -d "$openjpeg_dest_dir" ]; then
            for file in "$openjpeg_dest_dir"/*; do
                if [ -f "$file" ]; then
                    filename=$(basename "$file")
                    src_file="$openjpeg_src_dir/$filename"
                    if [ -f "$src_file" ]; then
                        cp -f "$src_file" "$file"
                    fi
                fi
            done
        fi
        cp -f "$openjpeg_src_dir/thread.h" "$openjpeg_dest_dir"
        cp -f "$openjpeg_src_dir/thread.c" "$openjpeg_dest_dir"
        cp -f "$openjpeg_src_dir/tls_keys.h" "$openjpeg_dest_dir"
        cp -f "$openjpeg_src_dir/opj_common.h" "$openjpeg_dest_dir"
        cp -f "$openjpeg_src_dir/mqc_inl.h" "$openjpeg_dest_dir"
        cp -f "$openjpeg_src_dir/sparse_array.h" "$openjpeg_dest_dir"
        cp -f "$openjpeg_src_dir/sparse_array.c" "$openjpeg_dest_dir"
        cp -f "$openjpeg_src_dir/opj_malloc.c" "$openjpeg_dest_dir"
        cp -f "$openjpeg_src_dir/ht_dec.c" "$openjpeg_dest_dir"
        cp -f "$openjpeg_src_dir/t1_ht_luts.h" "$openjpeg_dest_dir"

        if [ -f "$cmake_file" ]; then
            sed -i 's|Source/LibOpenJPEG/tgt.c ./Source/CacheFile.h|Source/LibOpenJPEG/tgt.c Source/LibOpenJPEG/thread.c Source/LibOpenJPEG/sparse_array.c Source/LibOpenJPEG/opj_malloc.c Source/LibOpenJPEG/ht_dec.c ./Source/CacheFile.h|' "$cmake_file"
            sed -i 's|\./Source/LibOpenJPEG/tgt.c ./Source/MapIntrospector.h|\./Source/LibOpenJPEG/tgt.c ./Source/LibOpenJPEG/thread.c ./Source/LibOpenJPEG/sparse_array.c ./Source/LibOpenJPEG/opj_malloc.c Source/LibOpenJPEG/ht_dec.c ./Source/MapIntrospector.h|' "$cmake_file"
        fi
        sed -i '237a#include "raw.h"' "${PWD}/Source/LibOpenJPEG/opj_includes.h"
        cd $OLDPWD
    fi
}

build() {
    cd $builddir
    local cmake_args=(
        "$@"
        -DBUILD_LIBPNG=ON
        -DBUILD_ZLIB=ON
        -DBUILD_LIBTIFF=ON
        -DBUILD_LIBRAWLITE=ON
        -DBUILD_SHARED_LIBS=ON
        -DBUILD_OPENEXR=ON
        -DCMAKE_CROSSCOMPILING=ON
        -DBUILD_TESTS=ON
        -DCMAKE_C_FLAGS="-s"
        -DCMAKE_CXX_FLAGS="-s"
        -B$ARCH-build
        -S./
    )

    ${OHOS_SDK}/native/build-tools/cmake/bin/cmake "${cmake_args[@]}" > $buildlog 2>&1
    $MAKE VERBOSE=1 -C $ARCH-build >> $buildlog 2>&1
    ret=$?
    cd $OLDPWD
    return $ret

}

package() {
    cd $builddir
    $MAKE -C $ARCH-build install >> $buildlog 2>&1
    ret=$?
    cd $OLDPWD
    return $ret
}

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

# 清理环境
cleanbuild() {
    rm -rf ${PWD}/$builddir #${PWD}/$packagename
    rm -rf ${PWD}/libpng* ${PWD}/libtiff* ${PWD}/zlib* ${PWD}/openjpeg*
}