#!/bin/bash -e

#
#  JoeyDev
#  Copyright (C) 2018-2023 Scott Duensing <scott@kangaroopunch.com>
#
#  This software is provided 'as-is', without any express or implied
#  warranty.  In no event will the authors be held liable for any damages
#  arising from the use of this software.
#
#  Permission is granted to anyone to use this software for any purpose,
#  including commercial applications, and to alter it and redistribute it
#  freely, subject to the following restrictions:
#
#  1. The origin of this software must not be misrepresented; you must not
#     claim that you wrote the original software. If you use this software
#     in a product, an acknowledgment in the product documentation would be
#     appreciated but is not required.
#  2. Altered source versions must be plainly marked as such, and must not be
#     misrepresented as being the original software.
#  3. This notice may not be removed or altered from any source distribution.
#


ROOT=$1
INSTALLED=${ROOT}/thirdparty-installed
THIRDPARTY=${ROOT}/thirdparty


pushd "${ROOT}" || exit &> /dev/null

  mkdir -p ${INSTALLED}/include

  pushd ${INSTALLED} || exit &> /dev/null

  if [[ ! -f ${INSTALLED}/memwatch/memwatch.c ]]; then
    echo Unpacking Dependency:  memwatch...
    tar xjf ${THIRDPARTY}/memwatch.tar.bz2
    cp -f ${INSTALLED}/memwatch/memwatch.h ${INSTALLED}/include/.
  fi

  if [[ ! -f ${INSTALLED}/include/stb_ds.h ]]; then
    echo Installing Dependency:  stb_ds...
    cp -f ${THIRDPARTY}/stb_ds.h ${INSTALLED}/include/.
  fi

  if [[ ! -f ${INSTALLED}/include/stb_image.h ]]; then
    echo Installing Dependency:  stb_image...
    cp -f ${THIRDPARTY}/stb_image.h ${INSTALLED}/include/.
  fi

  if [[ ! -f ${INSTALLED}/lib/libz.a ]]; then
    echo Building Dependency:  libz...
    tar xzf ${THIRDPARTY}/zlib-1.2.13.tar.gz
    pushd zlib-1.2.13 || exit &> /dev/null
      ./configure \
        --static \
        --prefix=${INSTALLED}
      make
      make install
    popd || true &> /dev/null
  fi

  if [[ ! -f ${INSTALLED}/lib/libgpg-error.a ]]; then
    echo Building Dependency:  libgpg-error...
    tar xjf ${THIRDPARTY}/libgpg-error-1.46.tar.bz2
    pushd libgpg-error-1.46 || exit &> /dev/null
      ./configure \
        --enable-static \
        --disable-shared \
        --disable-doc \
        --disable-tests \
        --disable-languages \
        --prefix=${INSTALLED}
      make
      make install
    popd || true &> /dev/null
  fi

  if [[ ! -f ${INSTALLED}/lib/libgcrypt.a ]]; then
    echo Building Dependency:  libgcrypt...
    tar xjf ${THIRDPARTY}/libgcrypt-1.10.1.tar.bz2
    pushd libgcrypt-1.10.1 || exit &> /dev/null
      ./configure \
        --enable-static \
        --disable-shared \
        --disable-doc \
        --with-libgpg-error-prefix=${INSTALLED} \
        --prefix=${INSTALLED}
      make
      make install
    popd || true &> /dev/null
  fi

  if [[ ! -f ${INSTALLED}/lib/libssh2.a ]]; then
    echo Building Dependency:  libssh2...
    tar xzf ${THIRDPARTY}/libssh2-1.10.0.tar.gz
    pushd libssh2-1.10.0 || exit &> /dev/null
      ./configure \
        --enable-static \
        --disable-shared \
        --with-crypto=libgcrypt \
        --with-libz-prefix=${INSTALLED} \
        --with-libgcrypt-prefix=${INSTALLED} \
        --prefix=${INSTALLED}
      make
      make install
    popd || true &> /dev/null
  fi

  if [[ ! -f ${INSTALLED}/lib/libcurl.a ]]; then
    echo Building Dependency:  libcurl...
    tar xjf ${THIRDPARTY}/curl-7.87.0.tar.bz2
    pushd curl-7.87.0 || exit &> /dev/null
      ./configure \
        --enable-static \
        --disable-shared \
        --without-ssl \
        --without-libidn2 \
        --without-libpsl \
        --without-brotli \
        --without-zstd \
        --prefix=${INSTALLED}
      make
      make install
    popd || true &> /dev/null
  fi

  if [[ ! -f ${INSTALLED}/lib/scintilla.a ]]; then
    echo Building Dependency:  Scintilla...
    tar xzf ${THIRDPARTY}/scintilla531.tgz
    pushd scintilla/gtk || exit &> /dev/null
      GTK3=1 make
      cp -f ../bin/scintilla.a ${INSTALLED}/lib/.
      cp -f ../include/* ${INSTALLED}/include/.
    popd || true &> /dev/null
  fi

  if [[ ! -f ${INSTALLED}/lib/liblexilla.a ]]; then
    echo Building Dependency:  Lexilla...
    tar xzf ${THIRDPARTY}/lexilla520.tgz
    pushd lexilla/src || exit &> /dev/null
      make
      cp -f ../bin/liblexilla.a ${INSTALLED}/lib/.
      cp -f ../include/* ${INSTALLED}/include/.
    popd || true &> /dev/null
  fi

  popd || true &> /dev/null

	echo Generating UI Embedded Code...
	mkdir -p ui/generated
	glib-compile-resources \
		--sourcedir=ui/ \
		--target=ui/generated/resources.c \
		--generate-source \
		ui/joeydev.gresource.xml

popd || true &> /dev/null
