#! /bin/bash
#
# Multi-channel signed distance field atlas generator
# This is a utility for generating compact font atlases using MSDFgen.
#
# The atlas generator loads a subset of glyphs from a TTF or OTF font file,
# generates a distance field for each of them, and tightly packs them into
# an atlas bitmap (example below). The finished atlas and/or its layout
# metadata can be exported as an Artery Font file, a plain image file,
# a CSV sheet or a structured JSON file.
# https://github.com/Chlumsky/msdf-atlas-gen
#
# it uses cmake build system, and depends on msdfgen (also downloaded)
#
# linux notes:
# should you not have libtinyxml2-dev installed with cmake files
# check with for example (Debian, Ubuntu): dpkg -L libtinyxml2-dev
# if cmake files are present
#
# if not then install libtinyxml2 from https://github.com/leethomason/tinyxml2.git
#
# for example:
# $ cd your/favorite/persistent/build/directory
# $ git clone https://github.com/leethomason/tinyxml2.git
# $ cd tinyxml2
# $ mkdir build && cd build
# $ cmake ..
# $ make
# $ sudo make install
#
# emscripten notes:
# additionally to the depencies below
# you may have to build freetype from source
# and with that also bzip2
# $
# $ # tinyxml2:
# $ cd your/favorite/persistent/build/directory
# $ git clone https://github.com/leethomason/tinyxml2.git
# $ cd tinyxml2
# $ mkdir build && cd build
# $ emcmake cmake ..
# $ emmake make
# $ emmake make install
# $
# $ # zlib:
# $ cd your/favorite/persistent/build/directory
# $ wget https://www.zlib.net/zlib-1.2.13.tar.gz
# $ tar -xf zlib-1.2.13.tar.gz
# $ cd zlib-1.2.13.tar.gz
# $ mkdir build && cd build
# $ emcmake cmake ..
# $ emmake make
# $ emmake make install
# $
# $ # libpng:
# $ cd your/favorite/persistent/build/directory
# $ wget https://sourceforge.net/projects/libpng/files/libpng16/1.6.39/libpng-1.6.39.tar.xz/download -O libpng-1.6.39.tar.xz
# $ tar -xf libpng-1.6.39.tar.xz
# $ cd libpng-1.6.39
# $ mkdir build && cd build
# $ emcmake cmake ..
# $ emmake make
# $ emmake make install


# array of build types supported by this formula
# you can delete this to implicitly support *all* types
FORMULA_TYPES=( "osx" "linux" "linux64" "vs" "msys2" "ios" "android" "emscripten" )

# define the version
VER=1.2.2

# tools for git use
GIT_URL=https://github.com/Chlumsky/msdf-atlas-gen.git
GIT_TAG="v${VER}"

# download the source code and unpack it into LIB_NAME
function download() {
    echo "download msdf-atlas-gen"
    wget --quiet https://pointer.click/files/msdf-atlas-gen.tar.gz -O msdf-atlas-gen.tar.gz
    mkdir -p msdf-atlas-gen
    tar -xzf msdf-atlas-gen.tar.gz -C msdf-atlas-gen --strip-components=1
    rm msdf-atlas-gen.tar.gz
}

# prepare the build environment, executed inside the lib src dir
function prepare() {
    mkdir -p lib/$TYPE
}

# executed inside the lib src dir
function build() {
    if [ "$TYPE" == "linux64" ] ; then
        cd lib/$TYPE
        cmake \
            -DMSDF_ATLAS_BUILD_STANDALONE=OFF \
            -DMSDF_ATLAS_USE_VCPKG=OFF \
            -DMSDF_ATLAS_USE_SKIA=OFF \
            -DMSDF_ATLAS_NO_ARTERY_FONT=OFF \
            -DMSDF_ATLAS_MSDFGEN_EXTERNAL=OFF \
            -DMSDF_ATLAS_INSTALL=OFF \
            -DMSDF_ATLAS_DYNAMIC_RUNTIME=OFF \
            -DBUILD_SHARED_LIBS=OFF \
            -DMSDFGEN_CORE_ONLY=OFF \
            -DMSDFGEN_BUILD_STANDALONE=OFF \
            -DMSDFGEN_USE_VCPKG=OFF \
            -DMSDFGEN_USE_OPENMP=OFF \
            -DMSDFGEN_USE_CPP11=ON \
            -DMSDFGEN_USE_SKIA=OFF \
            -DMSDFGEN_INSTALL=OFF \
            -DMSDFGEN_DYNAMIC_RUNTIME=OFF \
            ../..
        make
    elif [ "$TYPE" == "emscripten" ] ; then
        cd lib/$TYPE
        emcmake cmake \
            -DCMAKE_CXX_FLAGS="-pthread" \
            -DMSDF_ATLAS_BUILD_STANDALONE=OFF \
            -DMSDF_ATLAS_USE_VCPKG=OFF \
            -DMSDF_ATLAS_USE_SKIA=OFF \
            -DMSDF_ATLAS_NO_ARTERY_FONT=OFF \
            -DMSDF_ATLAS_MSDFGEN_EXTERNAL=OFF \
            -DMSDF_ATLAS_INSTALL=OFF \
            -DMSDF_ATLAS_DYNAMIC_RUNTIME=OFF \
            -DBUILD_SHARED_LIBS=OFF \
            -DMSDFGEN_CORE_ONLY=OFF \
            -DMSDFGEN_BUILD_STANDALONE=OFF \
            -DMSDFGEN_USE_VCPKG=OFF \
            -DMSDFGEN_USE_OPENMP=OFF \
            -DMSDFGEN_USE_CPP11=ON \
            -DMSDFGEN_USE_SKIA=OFF \
            -DMSDFGEN_INSTALL=OFF \
            -DMSDFGEN_DYNAMIC_RUNTIME=OFF \
            ../..
        emmake make
    else
	    echoWarning "TODO: $TYPE build"
    fi
}

# executed inside the lib src dir, first arg $1 is the dest libs dir root
function copy() {
    if [ "$TYPE" == "linux64" ] || [ "$TYPE" == "emscripten" ] ; then
        # lib
        cd lib/$TYPE
        dest_lib=$1/lib/$TYPE
        mkdir -p $dest_lib
        find . -iname "*.a" -exec cp {} $dest_lib/ \;

        # include
        cd ../..
        dest_include=$1/include
        mkdir -p $dest_include
        mkdir -p $1/license
        cp LICENSE.txt $1/license/
        cp ./*.h $dest_include/
        cp -r artery-font-format $dest_include/
        cp -r msdfgen $dest_include/
        rm -rf $dest_include/msdfgen/cmake
        cp -r msdf-atlas-gen $dest_include/

        # post
        sed -i 's/MSDFGEN_PUBLIC //g' $dest_include/msdfgen/core/generator-config.h
        sed -i 's/MSDF_ATLAS_PUBLIC //g' $dest_include/msdf-atlas-gen/Charset.h
    else
	    echoWarning "TODO: $TYPE copy"
    fi
}

# executed inside the lib src dir
function clean() {
    if [ "$TYPE" == "linux64" ] ; then
        cd lib/$TYPE
        make clean
    elif [ "$TYPE" == "emscripten" ] ; then
        cd lib/$TYPE
        emmake make clean
    else
	    echoWarning "TODO: $TYPE clean"
    fi
}
