#!/bin/bash

# Slackware build script for godot

# Copyright 2023-2024 Steven Voges <Oregon, USA>
# All rights reserved.
# Copyright 2025 Christoph Willing <Sydney, Australia>
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=godot
VERSION=${VERSION:-4.5}
SRCVER=${SRCVERSION:-$VERSION-stable}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
  GARCH="x86_32"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
  GARCH="x86_32"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
  GARCH="x86_64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
  GARCH="x86_32"
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$SRCVER
tar xvf $CWD/$PRGNAM-$SRCVER.tar.gz
cd $PRGNAM-$SRCVER
chown -R root:root .
find -L . \
 \( -perm 777 -o -perm 775 -o -perm 750 -o -perm 711 -o -perm 555 \
  -o -perm 511 \) -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 640 -o -perm 600 -o -perm 444 \
  -o -perm 440 -o -perm 400 \) -exec chmod 644 {} \;

# scons can use multiple jobs. For convenience, read MAKEFLAGS from the environment, if set
JOBS="$( echo $MAKEFLAGS | sed 's,.*-j *\([0-9][0-9]*\)*.*,\1,' )"
# or if no MAKEFLAGS, use the number of cores minus 1
JOBS="${JOBS:-$(( $( nproc ) - 1 ))}"
# or 1, if we really have one core
[ "$JOBS" = "0" ] && JOBS=1

export BUILD_NAME=SBo
# Configuration options taken from:
# https://github.com/godotengine/godot/blob/master/SConstruct
scons -j$JOBS \
  CFLAGS="$SLKCFLAGS" \
  CXXFLAGS="$SLKCFLAGS" \

  platform=linuxbsd \
  target=editor \
  arch=$GARCH \
  dev_build=no \
  optimize=none \
  debug_symbols=no \
  separate_debug_symbols=no \
  lto=none \
  production=yes \
  generate_apk=no \

  depreciated=yes \
  precision=single \
  minizip=yes \
  brotli=yes \
  xaudio2=no \
  vulkan=yes \
  opengl3=yes \
  d3d12=no \
  openxr=yes \
  use_volk=yes \
  disable_exceptions=yes \
  custom_modules="" \
  custom_madules_recursive=yes \

  dev_mode=no \
  tests=no \
  fast_unsafe=no \
  compiledb=no \
  verbose=no \
  progress=yes \
  warnings=all \
  werror=no \
  extra_suffix="" \
  object_prefix="" \
  vsproj=no \
  vsproj_name=godot \
  import_env_vars="" \
  disable_3d=no \
  disable_advanced_gui=no \
  build_profile="" \
  modules_enabled_by_default=yes \
  no_editor_splash=yes \
  system_certs_path=/etc/ssl/certs/ca-certificates.crt \
  use_precise_math_checks=no \
  scu_build=no \
  scu_limit=0 \

  builtin_brotli=no \
  builtin_certs=no \
  builtin_clipper2=yes \
  builtin_embree=yes \
  builtin_enet=yes \
  builtin_freetype=no \
  builtin_msdfgen=yes \
  builtin_glslang=no \
  builtin_graphite=no \
  builtin_harfbuzz=no \
  builtin_icu4c=no \
  builtin_libogg=no \
  builtin_libpng=no \
  builtin_libtheora=no \
  builtin_libvorbis=no \
  builtin_libwebp=no \
  builtin_wslay=yes \
  builtin_mbedtls=yes \
  builtin_miniupnpc=yes \
  builtin_openxr=yes \
  builtin_pcre2=no \
  builtin_pcre2_with_jit=no \
  builtin_recastnavigation=yes \
  builtin_rvo2_2d=yes \
  builtin_rvo2_3d=yes \
  builtin_squish=yes \
  builtin_xatlas=yes \
  builtin_zlib=no \
  builtin_zstd=no \

  use_llvm=no \
  colored=yes \
  pulseaudio=yes

install -Dvm0755 bin/${PRGNAM}.linuxbsd.editor.$GARCH $PKG/usr/bin/$PRGNAM
install -Dvm0644 misc/dist/linux/${PRGNAM}.6 $PKG/usr/man/man6/${PRGNAM}.6
install -Dvm0644 misc/dist/linux/org.godotengine.Godot.desktop $PKG/usr/share/applications/org.godotengine.godot.desktop
install -Dvm0644 misc/dist/linux/org.godotengine.Godot.xml $PKG/usr/share/mime/application/org.godotengine.godot.xml
install -Dvm0644 misc/dist/linux/org.godotengine.Godot.appdata.xml $PKG/usr/share/appdata/org.godotengine.godot.appdata.xml
install -Dvm0644 icon.svg $PKG/usr/share/icons/hicolor/scalable/apps/${PRGNAM}.svg
install -Dvm0644 icon_outlined.svg $PKG/usr/share/icons/hicolor/scalable/apps/${PRGNAM}_outlined.svg
install -Dvm0644 logo.svg $PKG/usr/share/icons/hicolor/scalable/apps/${PRGNAM}_logo.svg
install -Dvm0644 logo_outlined.svg $PKG/usr/share/icons/hicolor/scalable/apps/${PRGNAM}_logo_outlined.svg
install -Dvm0644 misc/dist/document_icons/gdscript.svg $PKG/usr/share/icons/hicolor/scalable/mimetypes/x-godot-gdscript.svg
install -Dvm0644 misc/dist/document_icons/project.svg $PKG/usr/share/icons/hicolor/scalable/mimetypes/x-godot-project.svg
install -Dvm0644 misc/dist/document_icons/resource.svg $PKG/usr/share/icons/hicolor/scalable/mimetypes/x-godot-resource.svg
install -Dvm0644 misc/dist/document_icons/scene.svg $PKG/usr/share/icons/hicolor/scalable/mimetypes/x-godot-scene.svg
install -Dvm0644 misc/dist/document_icons/shader.svg $PKG/usr/share/icons/hicolor/scalable/mimetypes/x-godot-shader.svg

find $PKG -print0 | xargs -0 file | grep -e "executable" -e "shared object" | grep ELF \
  | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null || true

find $PKG/usr/man -type f -exec gzip -9 {} \;
for i in $( find $PKG/usr/man -type l ) ; do ln -s $( readlink $i ).gz $i.gz ; rm $i ; done

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a \
  *.md *.txt \
  $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
