#!/usr/bin/env bash
#
# svgtiny
# Libsvgtiny is an implementation of SVG Tiny, written in C
# http://www.netsurf-browser.org/projects/libsvgtiny/
#
# uses a makeifle build system

FORMULA_TYPES=("linux" "osx" "vs" "ios" "watchos" "catos" "xros" "tvos" "android" "emscripten" "msys2")
FORMULA_DEPENDS=("zlib" "libxml2")

# define the version by sha
VER=0.1.8
BUILD_ID=1
DEFINES=""

# tools for git use
GIT_URL=git://git.netsurf-browser.org/libsvgtiny.git
GIT_TAG=$VER

# download the source code and unpack it into LIB_NAME
function download() {
    git -c advice.detachedHead=false clone -b release/$VER --depth 1 git://git.netsurf-browser.org/libsvgtiny.git
    mv libsvgtiny svgtiny
    cd svgtiny

    # git -c advice.detachedHead=false clone -b release/0.4.2 --depth 1 git://git.netsurf-browser.org/libdom.git
    git -c advice.detachedHead=false clone -b master --depth 1 https://github.com/danoli3/libdom.git #0.2.5
    git -c advice.detachedHead=false clone -b release/0.2.5 --depth 1 git://git.netsurf-browser.org/libparserutils.git
    git -c advice.detachedHead=false clone -b release/0.4.3 --depth 1 git://git.netsurf-browser.org/libwapcaplet.git

    #if [ "$TYPE" == "vs" ]; then
    #dos2unix $FORMULA_DIR/libdom.patch
    # cd libdom
    # if git apply $FORMULA_DIR/libdompatch.diff  --check; then
    #  	git apply $FORMULA_DIR/libdompatch.diff
    # fi
    # cd ../
    # sed -i -e 's/restrict//g' libwapcaplet/src/libwapcaplet.c
    #else
    # Use custom Makefile
    #cp $FORMULA_DIR/Makefile .
    #gperf src/colors.gperf | sed -e 's/^\(const struct svgtiny_named_color\)/static \1/' > src/autogenerated_colors.c
    #dos2unix $FORMULA_DIR/libdom.patch
    # cd libdom
    # if git apply $FORMULA_DIR/libdom.patch  --check; then
    # 	git apply $FORMULA_DIR/libdom.patch
    # fi
    # cd ../
    # sed -i -e 's/restrict//g' libwapcaplet/src/libwapcaplet.c
    #fi

    cd libparserutils
    patch -up1 < $FORMULA_DIR/libparseutils.patch
    cd ..
}

# prepare the build environment, executed inside the lib src dir
function prepare() {
    #if [ "$TYPE" == "vs" ]; then
    rm -f ./CMakeLists.txt
    cp $FORMULA_DIR/CMakeLists.txt ./CMakeLists.txt
    cp -f $FORMULA_DIR/make-aliases.pl ./libparserutils/build/make-aliases.pl
    cp -f $FORMULA_DIR/autogenerated_colors.c ./src/autogenerated_colors.c
    cp $FORMULA_DIR/libwapcaplet.h libwapcaplet/include/libwapcaplet/
    # cp $FORMULA_DIR/expat.h libdom/include/dom/expat.h
    #else
    #	cp $FORMULA_DIR/Makefile .
    #fi
    #Generate Aliases.inc file
    cd libparserutils
    perl build/make-aliases.pl
    cd ..
    cp -rf libdom/bindings libdom/include/dom/

    apothecaryDepend download zlib
    apothecaryDepend prepare zlib
    apothecaryDepend build zlib
    apothecaryDepend copy zlib

    apothecaryDepend download libxml2
    apothecaryDepend prepare libxml2
    apothecaryDepend build libxml2
    apothecaryDepend copy libxml2

}

# executed inside the lib src dir
function build() {
    LIBS_ROOT=$(realpath $LIBS_DIR)
    if [ "$TYPE" == "msys2" ]; then

        MINGW_PREFIX="${MINGW_PREFIX:-/mingw64}"
        LIBXML2_ROOT="$MINGW_PREFIX"
        LIBXML2_INCLUDE_DIR="$LIBXML2_ROOT/include/libxml2"
        LIBXML2_LIBRARY="$LIBXML2_ROOT/lib/libxml2.a"

        mkdir -p "build_${TYPE}_${ARCH}"
        cd "build_${TYPE}_${ARCH}"
        DEFINES="-DLIBRARY_SUFFIX=${ARCH} \
	        -DCMAKE_BUILD_TYPE=Release \
	        -DCMAKE_C_STANDARD=${C_STANDARD} \
	        -DCMAKE_CXX_STANDARD=${CPP_STANDARD} \
	        -DCMAKE_CXX_STANDARD_REQUIRED=ON \
	        -DCMAKE_CXX_EXTENSIONS=OFF
	        -DBUILD_SHARED_LIBS=OFF \
	        -DCMAKE_INSTALL_PREFIX=Release \
	        -DCMAKE_INCLUDE_OUTPUT_DIRECTORY=include \
	        -DCMAKE_INSTALL_INCLUDEDIR=include"
        cmake .. ${DEFINES} \
            -DCMAKE_CXX_FLAGS="-DUSE_PTHREADS=1 -Iinclude" \
            -DCMAKE_C_FLAGS="-DUSE_PTHREADS=1 -Iinclude -Wno-implicit-function-declaration" \
            -DCMAKE_BUILD_TYPE=Release \
            -DCMAKE_INSTALL_LIBDIR="lib" \
            -DDO_XML_INSTALL=ON \
            -DCMAKE_SYSTEM_NAME=$TYPE \
            -DCMAKE_SYSTEM_PROCESSOR=$ABI \
            -DLIBXML2_ROOT=$LIBXML2_ROOT \
            -DCMAKE_POSITION_INDEPENDENT_CODE=ON \
            -DCMAKE_MINIMUM_REQUIRED_VERSION=3.22 \
            -DLIBXML2_INCLUDE_DIR=$LIBXML2_INCLUDE_DIR \
            -DLIBXML2_LIBRARY=$LIBXML2_LIBRARY
        cmake --build . --config Release -j${PARALLEL_MAKE}
        cd ..
    elif [ "$TYPE" == "linux" ]; then

        if [ $CROSSCOMPILING -eq 1 ]; then
            source $APOTHECARY_DIR/configure/${TYPE}${PLATFORM}_configure.sh $ABI
        fi

        LIBXML2_ROOT="$LIBS_ROOT/libxml2/"
        LIBXML2_INCLUDE_DIR="$LIBS_ROOT/libxml2/include"
        LIBXML2_LIBRARY="$LIBS_ROOT/libxml2/lib/$TYPE/$PLATFORM/libxml2.a"

        ZLIB_ROOT="$LIBS_ROOT/zlib/"
        ZLIB_INCLUDE_DIR="$LIBS_ROOT/zlib/include"
        ZLIB_LIBRARY="$LIBS_ROOT/zlib/lib/$TYPE/$PLATFORM/zlib.a"

        export PKG_CONFIG_PATH="/usr/local/lib/pkgconfig:${PKG_CONFIG_PATH}:${ZLIB_ROOT}/lib/$TYPE/$PLATFORM:${LIBXML2_ROOT}/lib/$TYPE/$PLATFORM"

        mkdir -p "build_${TYPE}_${PLATFORM}"
        cd "build_${TYPE}_${PLATFORM}"
        DEFINES="-DLIBRARY_SUFFIX=${ARCH} \
	        -DCMAKE_BUILD_TYPE=Release \
	        -DCMAKE_C_STANDARD=${C_STANDARD} \
	        -DCMAKE_CXX_STANDARD=${CPP_STANDARD} \
	        -DCMAKE_CXX_STANDARD_REQUIRED=ON \
	        -DCMAKE_CXX_EXTENSIONS=OFF
	        -DBUILD_SHARED_LIBS=OFF \
	        -DCMAKE_INSTALL_PREFIX=Release \
	        -DCMAKE_INCLUDE_OUTPUT_DIRECTORY=include \
	        -DCMAKE_INSTALL_INCLUDEDIR=include"
        cmake .. ${DEFINES} \
            -DCMAKE_CXX_FLAGS="-DUSE_PTHREADS=1 -Iinclude ${FLAG_RELEASE} -I${LIBXML2_INCLUDE_DIR} -I${ZLIB_INCLUDE_DIR}" \
            -DCMAKE_C_FLAGS="-DUSE_PTHREADS=1 -Iinclude -Wno-implicit-function-declaration ${FLAG_RELEASE} -I${LIBXML2_INCLUDE_DIR} -I${ZLIB_INCLUDE_DIR}" \
            -DCMAKE_BUILD_TYPE=Release \
            -DCMAKE_INSTALL_LIBDIR="lib" \
            -DDO_XML_INSTALL=ON \
            -DSKIP_EXAMPLE=1 \
            -DGCC_VERSION=${GCC_VERSION} \
            -DCMAKE_TOOLCHAIN_FILE=$APOTHECARY_DIR/toolchains/${TYPE}${PLATFORM}.toolchain.cmake \
            -DCMAKE_POSITION_INDEPENDENT_CODE=ON \
            -DCMAKE_MINIMUM_REQUIRED_VERSION=3.22 \
            -DCMAKE_SYSTEM_NAME=$TYPE \
            -DCMAKE_SYSTEM_PROCESSOR=$ABI \
            -DLIBXML2_ROOT=${LIBXML2_ROOT} \
            -DLIBXML2_INCLUDE_DIR=${LIBXML2_INCLUDE_DIR} \
            -DLIBXML2_LIBRARY=${LIBXML2_LIBRARY} \
            -DCMAKE_PREFIX_PATH="${LIBS_ROOT}" \
            -DCMAKE_VERBOSE_MAKEFILE=ON \
            -DBUILD_SHARED_LIBS=OFF
        cmake --build . --config Release -j${PARALLEL_MAKE}
        cd ..
    elif [ "$TYPE" == "vs" ]; then
        LIBXML2_ROOT="$LIBS_ROOT/libxml2/"
        LIBXML2_INCLUDE_DIR="$LIBS_ROOT/libxml2/include"
        LIBXML2_LIBRARY="$LIBS_ROOT/libxml2/lib/$TYPE/$PLATFORM/libxml2.lib"

        ZLIB_ROOT="$LIBS_ROOT/zlib/"
        ZLIB_INCLUDE_DIR="$LIBS_ROOT/zlib/include"
        ZLIB_LIBRARY="$LIBS_ROOT/zlib/lib/$TYPE/$PLATFORM/zlib.lib"

        echo "building svgtiny $TYPE | $ARCH | $VS_VER | vs: $VS_VER_GEN"
        echo "--------------------"
        GENERATOR_NAME="Visual Studio ${VS_VER_GEN}"
        mkdir -p "build_${TYPE}_${ARCH}"
        cd "build_${TYPE}_${ARCH}"
        rm -f CMakeCache.txt *.lib *.o
        env CXXFLAGS="-DUSE_PTHREADS=1 ${VS_C_FLAGS} ${FLAGS_RELEASE} ${EXCEPTION_FLAGS}"
        DEFINES="
	        -DCMAKE_C_STANDARD=${C_STANDARD} \
	        -DCMAKE_CXX_STANDARD=${CPP_STANDARD} \
	        -DCMAKE_CXX_STANDARD_REQUIRED=ON \
	        -DCMAKE_CXX_EXTENSIONS=OFF \
	        -DCMAKE_INCLUDE_OUTPUT_DIRECTORY=include \
	        -DCMAKE_INSTALL_INCLUDEDIR=include"
        cmake .. ${DEFINES} \
            -UCMAKE_CXX_FLAGS \
            -DCMAKE_CXX_FLAGS="-DUSE_PTHREADS=1 " \
            -DCMAKE_C_FLAGS="-DUSE_PTHREADS=1" \
            -DDO_XML_INSTALL=ON \
            -DCMAKE_CXX_FLAGS_RELEASE="-DUSE_PTHREADS=1 ${VS_C_FLAGS} ${FLAGS_RELEASE} ${EXCEPTION_FLAGS} -I${LIBXML2_INCLUDE_DIR} -I${ZLIB_INCLUDE_DIR}" \
            -DCMAKE_C_FLAGS_RELEASE="-DUSE_PTHREADS=1 ${VS_C_FLAGS} ${FLAGS_RELEASE} ${EXCEPTION_FLAGS} -I${LIBXML2_INCLUDE_DIR} -I${ZLIB_INCLUDE_DIR}" \
            -DCMAKE_BUILD_TYPE=Debug \
            -DCMAKE_INSTALL_LIBDIR="lib" \
            ${CMAKE_WIN_SDK} \
            -DBUILD_SHARED_LIBS=OFF \
            -DLIBXML2_ROOT=$LIBXML2_ROOT \
            -DLIBXML2_INCLUDE_DIR=$LIBXML2_INCLUDE_DIR \
            -DLIBXML2_LIBRARY=$LIBXML2_LIBRARY \
            -DCMAKE_PREFIX_PATH="${ZLIB_ROOT} ${LIBXML2_ROOT}" \
            -D CMAKE_VERBOSE_MAKEFILE=${VERBOSE_MAKEFILE} \
            -DCMAKE_POSITION_INDEPENDENT_CODE=ON \
            -DCMAKE_MINIMUM_REQUIRED_VERSION=3.22 \
            -DCMAKE_BUILD_TYPE=Release \
            -DCMAKE_INSTALL_PREFIX=. \
            -A "${PLATFORM}" \
            -G "${GENERATOR_NAME}"
        cmake --build . --config Release -j${PARALLEL_MAKE} --target install
        cmake .. ${DEFINES} \
            -UCMAKE_CXX_FLAGS \
            -DCMAKE_CXX_FLAGS="-DUSE_PTHREADS=1 " \
            -DCMAKE_C_FLAGS="-DUSE_PTHREADS=1" \
            -DDO_XML_INSTALL=ON \
            -DCMAKE_CXX_FLAGS_DEBUG="-DUSE_PTHREADS=1 ${VS_C_FLAGS} ${FLAGS_DEBUG} ${EXCEPTION_FLAGS} -I${LIBXML2_INCLUDE_DIR} -I${ZLIB_INCLUDE_DIR}" \
            -DCMAKE_C_FLAGS_DEBUG="-DUSE_PTHREADS=1 ${VS_C_FLAGS} ${FLAGS_DEBUG} ${EXCEPTION_FLAGS} -I${LIBXML2_INCLUDE_DIR} -I${ZLIB_INCLUDE_DIR}" \
            -DCMAKE_BUILD_TYPE=Debug \
            -DCMAKE_INSTALL_LIBDIR="lib" \
            ${CMAKE_WIN_SDK} \
            -DBUILD_SHARED_LIBS=OFF \
            -DLIBXML2_ROOT=$LIBXML2_ROOT \
            -DLIBXML2_INCLUDE_DIR=$LIBXML2_INCLUDE_DIR \
            -DLIBXML2_LIBRARY=$LIBXML2_LIBRARY \
            -DCMAKE_PREFIX_PATH="${ZLIB_ROOT} ${LIBXML2_ROOT}" \
            -D CMAKE_VERBOSE_MAKEFILE=${VERBOSE_MAKEFILE} \
            -DCMAKE_POSITION_INDEPENDENT_CODE=ON \
            -DCMAKE_MINIMUM_REQUIRED_VERSION=3.22 \
            -DCMAKE_BUILD_TYPE=Debug \
            -DCMAKE_INSTALL_PREFIX=. \
            -A "${PLATFORM}" \
            -G "${GENERATOR_NAME}"
        cmake --build . --config Debug -j${PARALLEL_MAKE} --target install
        cd ..
    elif [ "$TYPE" == "android" ]; then
        source $APOTHECARY_DIR/configure/android_configure.sh $ABI cmake

        LIBXML2_ROOT=$(realpath "$LIBS_ROOT/libxml2/")
        LIBXML2_INCLUDE_DIR=$(realpath "$LIBS_ROOT/libxml2/include")
        LIBXML2_LIBRARY="$LIBS_ROOT/libxml2/lib/$TYPE/$ABI/libxml2.a"

        ZLIB_ROOT="$LIBS_ROOT/zlib/"
        ZLIB_INCLUDE_DIR="$LIBS_ROOT/zlib/include"
        ZLIB_LIBRARY="$LIBS_ROOT/zlib/lib/$TYPE/$PLATFORM/zlib.a"

        DEFINES="
            -DCMAKE_C_STANDARD=${C_STANDARD} \
            -DCMAKE_CXX_STANDARD=${CPP_STANDARD} \
            -DCMAKE_CXX_STANDARD_REQUIRED=ON \
            -DCMAKE_CXX_EXTENSIONS=OFF \
            -DCMAKE_INCLUDE_OUTPUT_DIRECTORY=include \
            -DCMAKE_INSTALL_INCLUDEDIR=include"

        export PKG_CONFIG_PATH="/usr/local/lib/pkgconfig:${PKG_CONFIG_PATH}:${ZLIB_ROOT}/lib/$TYPE/$PLATFORM:${LIBXML2_ROOT}/lib/$TYPE/$PLATFORM"

        mkdir -p build_${TYPE}_${ABI}
        cd build_${TYPE}_${ABI}
        rm -f CMakeCache.txt *.a *.o
        cmake .. ${DEFINES} \
            -DCMAKE_TOOLCHAIN_FILE=$APOTHECARY_DIR/toolchains/android.toolchain.cmake \
            -DPLATFORM=$PLATFORM \
            -DANDROID_PLATFORM=${ANDROID_PLATFORM} \
            -DCMAKE_PREFIX_PATH="${LIBS_ROOT}" \
            -DANDROID_ABI=${ABI} \
            -DANDROID_API=${ANDROID_API} \
            -DANDROID_TOOLCHAIN=clang \
            -DANDROID_NDK_ROOT=$ANDROID_NDK_ROOT \
            -DDO_XML_INSTALL=ON \
            -DBUILD_SHARED_LIBS=OFF \
            -DCMAKE_POSITION_INDEPENDENT_CODE=ON \
            -DCMAKE_MINIMUM_REQUIRED_VERSION=3.22 \
            -DCMAKE_PREFIX_PATH="${LIBS_ROOT}" \
            -DLIBXML2_ROOT=$LIBXML2_ROOT \
            -DLIBXML2_INCLUDE_DIR=$LIBXML2_INCLUDE_DIR \
            -DLIBXML2_LIBRARY=$LIBXML2_LIBRARY \
            -DCMAKE_INSTALL_PREFIX=Release \
            -DCMAKE_CXX_FLAGS="-DUSE_PTHREADS=1 -fvisibility-inlines-hidden -std=c++${CPP_STANDARD} -frtti ${FLAG_RELEASE} -I${LIBXML2_INCLUDE_DIR} -I${ZLIB_INCLUDE_DIR}" \
            -DCMAKE_C_FLAGS="-DUSE_PTHREADS=1 -fvisibility-inlines-hidden -std=c${C_STANDARD} -Wno-implicit-function-declaration -frtti ${FLAG_RELEASE} -I${LIBXML2_INCLUDE_DIR} -I${ZLIB_INCLUDE_DIR}" \
            -DCMAKE_INCLUDE_OUTPUT_DIRECTORY=include \
            -DCMAKE_INSTALL_INCLUDEDIR=include \
            -DCMAKE_CXX_EXTENSIONS=OFF \
            -DBUILD_SHARED_LIBS=OFF \
            -DCMAKE_INSTALL_PREFIX=Release \
            -DCMAKE_INCLUDE_OUTPUT_DIRECTORY=include \
            -DCMAKE_INSTALL_INCLUDEDIR=include
        cmake --build . --config Release -j${PARALLEL_MAKE}
        cd ..

    elif [[ "$TYPE" =~ ^(osx|ios|tvos|xros|catos|watchos)$ ]]; then

        LIBXML2_ROOT=$(realpath "$LIBS_ROOT/libxml2/")
        LIBXML2_INCLUDE_DIR=$(realpath "$LIBS_ROOT/libxml2/include")
        LIBXML2_LIBRARY="$LIBS_ROOT/libxml2/lib/$TYPE/$PLATFORM/libxml2.a"

        ZLIB_ROOT="$LIBS_ROOT/zlib/"
        ZLIB_INCLUDE_DIR="$LIBS_ROOT/zlib/include"
        ZLIB_LIBRARY="$LIBS_ROOT/zlib/lib/$TYPE/$PLATFORM/zlib.a"

        echo "building $TYPE | $PLATFORM"
        mkdir -p build_${TYPE}_${PLATFORM}
        cd build_${TYPE}_${PLATFORM}
        rm -f CMakeCache.txt *.a *.o
        mkdir -p $1/include/libxml2
        mkdir -p $1/lib/$TYPE/$PLATFORM/
        cmake .. \
            -DCMAKE_INSTALL_PREFIX=Release \
            -D CMAKE_VERBOSE_MAKEFILE=${VERBOSE_MAKEFILE} \
            -D BUILD_SHARED_LIBS=OFF \
            -DCMAKE_PREFIX_PATH="${ZLIB_ROOT} ${LIBXML2_ROOT}" \
            -DSKIP_EXAMPLE=1 \
            -DCMAKE_BUILD_TYPE=Release \
            -DCMAKE_C_STANDARD=${C_STANDARD} \
            -DCMAKE_CXX_STANDARD=${CPP_STANDARD} \
            -DDO_XML_INSTALL=ON \
            -DCMAKE_CXX_STANDARD_REQUIRED=ON \
            -DCMAKE_CXX_EXTENSIONS=ON \
            -DDEPLOYMENT_TARGET=${MIN_SDK_VER} \
            -DCMAKE_TOOLCHAIN_FILE=$APOTHECARY_DIR/toolchains/ios.toolchain.cmake \
            -DCMAKE_INSTALL_PREFIX=Release \
            -DCMAKE_CXX_FLAGS="-DUSE_PTHREADS=1 -fvisibility-inlines-hidden -std=c++${CPP_STANDARD} -frtti ${FLAG_RELEASE} -I${LIBXML2_INCLUDE_DIR} -I${ZLIB_INCLUDE_DIR}" \
            -DCMAKE_C_FLAGS="-DUSE_PTHREADS=1 -fvisibility-inlines-hidden -std=c${C_STANDARD} -Wno-implicit-function-declaration -frtti ${FLAG_RELEASE} -I${LIBXML2_INCLUDE_DIR} -I${ZLIB_INCLUDE_DIR}" \
            -DCMAKE_INCLUDE_OUTPUT_DIRECTORY=include \
            -DCMAKE_INSTALL_INCLUDEDIR=include \
            -DLIBXML2_ROOT=$LIBXML2_ROOT \
            -DLIBXML2_INCLUDE_DIR=$LIBXML2_INCLUDE_DIR \
            -DLIBXML2_LIBRARY=$LIBXML2_LIBRARY \
            -DPLATFORM=$PLATFORM \
            -DENABLE_BITCODE=OFF \
            -DENABLE_ARC=OFF \
            -DCMAKE_POSITION_INDEPENDENT_CODE=ON \
            -DCMAKE_MINIMUM_REQUIRED_VERSION=3.22 \
            -DENABLE_VISIBILITY=OFF

        cmake --build . --config Release -j${PARALLEL_MAKE}

        cd ..
    elif [ "$TYPE" == "emscripten" ]; then
        mkdir -p build_${TYPE}_${PLATFORM}
        LIBXML2_ROOT="$LIBS_ROOT/libxml2/"
        LIBXML2_INCLUDE_DIR="$LIBS_ROOT/libxml2/include"
        LIBXML2_LIBRARY="$LIBS_ROOT/libxml2/lib/$TYPE/$PLATFORM/libxml2.a"

        ZLIB_ROOT="$LIBS_ROOT/zlib/"
        ZLIB_INCLUDE_DIR="$LIBS_ROOT/zlib/include"
        ZLIB_LIBRARY="$LIBS_ROOT/zlib/lib/$TYPE/$PLATFORM/zlib.a"

        export PKG_CONFIG_PATH="/usr/local/lib/pkgconfig:${PKG_CONFIG_PATH}:${ZLIB_ROOT}/lib/$TYPE/$PLATFORM:${LIBXML2_ROOT}/lib/$TYPE/$PLATFORM"

        cd build_${TYPE}_${PLATFORM}
        rm -f CMakeCache.txt *.a *.o *.js
        $EMSDK/upstream/emscripten/emcmake cmake .. \
            -DCMAKE_TOOLCHAIN_FILE=$EMSDK/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake \
            -B . \
            -DCMAKE_BUILD_TYPE=Release \
            -DCMAKE_C_STANDARD=${C_STANDARD} \
            -DCMAKE_CXX_STANDARD=${CPP_STANDARD} \
            -DCMAKE_CXX_STANDARD_REQUIRED=ON \
            -DDO_XML_INSTALL=ON \
            -DCMAKE_CXX_FLAGS="-std=c++${CPP_STANDARD} ${FLAG_RELEASE}" \
            -DCMAKE_C_FLAGS="-std=c${C_STANDARD} ${FLAG_RELEASE}" \
            -DCMAKE_CXX_EXTENSIONS=OFF \
            -DBUILD_SHARED_LIBS=OFF \
            -DCMAKE_INSTALL_PREFIX=Release \
            -DCMAKE_INCLUDE_OUTPUT_DIRECTORY=include \
            -DCMAKE_INSTALL_INCLUDEDIR=include \
            -DCMAKE_PREFIX_PATH="${LIBS_ROOT}" \
            -DLIBXML2_ROOT=$LIBXML2_ROOT \
            -DCMAKE_POSITION_INDEPENDENT_CODE=ON \
            -DCMAKE_MINIMUM_REQUIRED_VERSION=3.22 \
            -DUSE_XML2=ON \
            -DLIBXML2_INCLUDE_DIR=$LIBXML2_INCLUDE_DIR \
            -DLIBXML2_LIBRARY=$LIBXML2_LIBRARY
        $EMSDK/upstream/emscripten/emmake make -j${PARALLEL_MAKE}
        # $EMSDK/upstream/emscripten/emmake make install
        # cmake --build . --config Release -j${PARALLEL_MAKE}
        cd ..
    fi
}

# executed inside the lib src dir, first arg $1 is the dest libs dir root
function copy() {
    # prepare headers directory if needed
    mkdir -p $1/include

    # prepare libs directory if needed
    mkdir -p $1/lib/$TYPE
    cp -Rv include/* $1/include

    . "$SECURE_SCRIPT"

    if [ "$TYPE" == "vs" ]; then
        mkdir -p $1/lib/$TYPE/$PLATFORM/
        cp -Rv "build_${TYPE}_${ARCH}/include/" $1/
        cp -v "build_${TYPE}_${ARCH}/Release/svgtiny.lib" $1/lib/$TYPE/$PLATFORM/svgtiny.lib
        cp -v "build_${TYPE}_${ARCH}/Debug/svgtiny.lib" $1/lib/$TYPE/$PLATFORM/svgtinyD.lib
        secure "$1/lib/$TYPE/$PLATFORM/libsvgtiny.lib" "svgtiny.pkl" "$VERSION" "$DEFINES" "$BUILD_ID" "$FORMULA_DEPENDS"
    elif [[ "$TYPE" =~ ^(osx|ios|tvos|xros|catos|watchos)$ ]]; then
        mkdir -p $1/lib/$TYPE/$PLATFORM/
        cp -v "build_${TYPE}_${PLATFORM}/libsvgtiny.a" $1/lib/$TYPE/$PLATFORM/libsvgtiny.a
        secure "$1/lib/$TYPE/$PLATFORM/libsvgtiny.a" "svgtiny.pkl" "$VERSION" "$DEFINES" "$BUILD_ID" "$FORMULA_DEPENDS"
    elif [ "$TYPE" == "android" ]; then
        mkdir -p $1/lib/$TYPE/$ABI
        cp -f "build_${TYPE}_${ABI}/libsvgtiny.a" $1/lib/$TYPE/$PLATFORM/libsvgtiny.a
        secure "$1/lib/$TYPE/$PLATFORM/libsvgtiny.a" "svgtiny.pkl" "$VERSION" "$DEFINES" "$BUILD_ID" "$FORMULA_DEPENDS"
    elif [ "$TYPE" == "emscripten" ]; then
        mkdir -p $1/lib/$TYPE/$PLATFORM
        cp -f "build_${TYPE}_$PLATFORM/libsvgtiny.a" $1/lib/$TYPE/$PLATFORM/svgtiny.a
        secure "$1/lib/$TYPE/$PLATFORM/svgtiny.a" "svgtiny.pkl" "$VERSION" "$DEFINES" "$BUILD_ID" "$FORMULA_DEPENDS"
    elif [ "$TYPE" == "msys2" ]; then
        mkdir -p $1/lib/$TYPE/${ARCH}
        cp -f "build_${TYPE}_${ARCH}/libsvgtiny.a" $1/lib/$TYPE/libsvgtiny.a
        secure "$1/lib/$TYPE/$PLATFORM/libsvgtiny.a" "svgtiny.pkl" "$VERSION" "$DEFINES" "$BUILD_ID" "$FORMULA_DEPENDS"
    elif [ "$TYPE" == "linux" ]; then
        mkdir -p $1/lib/$TYPE/${PLATFORM}/
        cp -f "build_${TYPE}_${PLATFORM}/libsvgtiny.a" $1/lib/$TYPE/$PLATFORM/libsvgtiny.a
        secure "$1/lib/$TYPE/$PLATFORM/libsvgtiny.a" "svgtiny.pkl" "$VERSION" "$DEFINES" "$BUILD_ID" "$FORMULA_DEPENDS"
    fi

    # copy license file
    if [ -d "$1/license" ]; then
        rm -rf $1/license
    fi
    mkdir -p $1/license
    cp -v COPYING $1/license/
}

# executed inside the lib src dir
function clean() {
    if [ "$TYPE" == "vs" ]; then
        if [ -d "build_${TYPE}_${PLATFORM}" ]; then
            rm -r build_${TYPE}_${PLATFORM}
        fi
    elif [[ "$TYPE" =~ ^(osx|ios|tvos|xros|catos|watchos|emscripten|linux)$ ]]; then
        if [ -d "build_${TYPE}_${PLATFORM}" ]; then
            rm -r build_${TYPE}_${PLATFORM}
        fi
    fi
}

function load() {
    . "$LOAD_SCRIPT"
    LOAD_RESULT=$(loadsave ${TYPE} "svgtiny" ${ARCH} ${VER} "$LIBS_DIR_REAL/$1/lib/$TYPE/$PLATFORM" ${BUILD_ID})
    PREBUILT=$(echo "$LOAD_RESULT" | tail -n 1)
    if [ "$PREBUILT" -eq 1 ]; then
        echo 1
    else
        echo 0
    fi
}
