#!/bin/bash

# Turn on verbose debugging output
set -x
# Make errors fatal
set -e

CEF_VERSION_X86="141.0.6+g5bb5565+chromium-141.0.7390.108"
CEF_VERSION_ARM="141.0.6+g5bb5565+chromium-141.0.7390.108"

# Pre-built CEF packages source
CEF_SITE="https://cef-builds.spotifycdn.com/"

# Pre-built viewer libraries packages we need:
LIBSSITE="http://sldev.free.fr/libraries/"
LIBSCACHE="/var/tmp/$(whoami)/install.cache/"
SDL2LIBS_X86="libSDL-2.32.10-linux64-20250904.tar.bz2"
SDL2LIBS_ARM="libSDL-2.32.10-linux-arm64-20250904.tar.bz2"
# Not used (see the note below as to why):
#GLIBLIBS="glib-2.58.3-linux64-20231214.tar.bz2"

arch=`uname -m`
if [ "$arch" == "x86_64" ] ; then
	OS="linux64"
	TARGET_ARCH="x86_64"
	BUILD_ARCH_FLAGS="-m64"
	PACKAGE_OS_NAME="linux64"
	CEF_VERSION="$CEF_VERSION_X86"
    SDL2LIBS=$SDL2LIBS_X86
elif [ "$arch" == "aarch64" ] ; then
	OS="linux-arm64"
	TARGET_ARCH="arm64"
	BUILD_ARCH_FLAGS="-march=armv8-a+fp+simd"
	PACKAGE_OS_NAME="linuxarm64"
	CEF_VERSION="$CEF_VERSION_ARM"
    SDL2LIBS=$SDL2LIBS_ARM
else
	echo "Sorry, 64 bits builds only, from now on !"
	exit 1
fi

CEF_PACKAGE="cef_binary_$CEF_VERSION"
CEF_SOURCE_DIR=$CEF_PACKAGE"_"$PACKAGE_OS_NAME"_minimal"
CEF_TARBALL="$CEF_SOURCE_DIR.tar.bz2"

CEF_MAJOR=`echo "$CEF_VERSION" | cut -d '.' -f 1`

DULLAHAN_SOURCE_DIR="src"

script=`readlink -f "$0" || echo "$0"`
top=`dirname "$script" || echo .`
echo "$top"
cd "$top"

stage="$top/stage"
if [ -d "$stage" ] ; then
	rm -rf "$stage"
fi
mkdir -p $stage

packages="$top/packages"
if [ -d "$packages" ] ; then
	rm -rf "$packages"
fi
mkdir -p $packages

# VirtualBox 6.1 shared folder code sets executable bit on text files... GRRR !
# So make sure our sources and text files are NOT set executable, should they
# have been copied from such a shared folder...
find . -name "*.h" -type f -exec chmod -x {} \;
find . -name "*.in" -type f -exec chmod -x {} \;
find . -name "*.cpp" -type f -exec chmod -x {} \;
find . -name "*.mm" -type f -exec chmod -x {} \;
find . -name "*.manifest" -type f -exec chmod -x {} \;
find . -name "*.plist" -type f -exec chmod -x {} \;
find . -name "*.txt" -type f -exec chmod -x {} \;
find . -name "*.diff" -type f -exec chmod -x {} \;
find . -name "README*" -type f -exec chmod -x {} \;

# NOTE: this causes conflicts, alas, because the CEF libraries link against
# the system's glib. You must therefore ensure that you build on a system with
# glib v2.58.3 (or newer, but the minimal version needed to run the resulting
# plugin will be the version you linked CEF/dullahan against)... :-(
# Make sure we have got the pre-built glib installed in packages/
#if ! [ -d "$packages/include/glib-2.0" ] ; then
#	if  ! [ -f $LIBSCACHE$GLIBLIBS ] ; then
#		mkdir -p "$LIBSCACHE"
#		pushd "$LIBSCACHE"
#		wget "$LIBSSITE$GLIBLIBS"
#		popd
#	fi
#	pushd "$packages"
#	tar xf "$LIBSCACHE$GLIBLIBS"
#	popd
#fi	

# Make sure we have got the pre-built SDL2 headers installed in packages/
if ! [ -d "$packages/include/SDL2" ] ; then
	if  ! [ -f $LIBSCACHE$SDL2LIBS ] ; then
		mkdir -p "$LIBSCACHE"
		pushd "$LIBSCACHE"
		wget "$LIBSSITE$SDL2LIBS"
		popd
	fi
	pushd "$packages"
	tar xf "$LIBSCACHE$SDL2LIBS"
	# We do not use the SDL2 libraries themselves, so remove them and only
	# keep the headers.
	rm -rf lib/debug/
	rm -f lib/release/libSDL*
	popd
fi	

if ! [ -d "$CEF_SOURCE_DIR" ] ; then
	if ! [ -f "$CEF_TARBALL" ] ; then
		wget `echo "$CEF_SITE$CEF_TARBALL" | sed -e 's;*;%2B;g'`
	fi
	tar xf $CEF_TARBALL
fi

# Build libcef_dll_wrapper
pushd "$CEF_SOURCE_DIR"
CEF_BASE_DIR=`pwd`

# Remove tests (required starting with CEF 105 for our custom CEF builds
# without GTK).
rm -rf tests/

if [ -d build ] ; then
	rm -rf build
fi
mkdir build
pushd build
cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release -DPROJECT_ARCH="$TARGET_ARCH" ..
let jobs=2
ncores=`cat /proc/cpuinfo | grep processor | wc -l`
if (( $ncores > 1 )) ; then
	jobs=$(($ncores / 2 + $ncores))
fi
make -j$jobs libcef_dll_wrapper
popd
popd

pushd "$DULLAHAN_SOURCE_DIR"
# Remove the old version file when it exists: it must be regenerated.
if [ -f dullahan_version.h ] ; then
	rm -f dullahan_version.h
fi
# Remove old builds and cmake cache files
if [ -d build ] ; then
	rm -rf build
fi
mkdir -p build
pushd build
# Build libdullahan.a and dullahan_host
cmake -G "Unix Makefiles" -DARCH:STRING="$BUILD_ARCH_FLAGS" -DCEF_BASE_DIR:STRING="$CEF_BASE_DIR" ..
make -j$jobs
popd
popd

# Copy files into stage/
mkdir -p "$stage/include/cef"
mkdir -p "$stage/lib/release"
mkdir -p "$stage/bin/release"

# Dullahan headers
cp -a "$DULLAHAN_SOURCE_DIR/dullahan.h" "$stage/include/cef/"
cp -a "$DULLAHAN_SOURCE_DIR/dullahan_version.h" "$stage/include/cef/"
cp -a "$DULLAHAN_SOURCE_DIR/build/libdullahan.a" "$stage/lib/release/"
cp -a "$DULLAHAN_SOURCE_DIR/build/dullahan_host" "$stage/bin/release/"

# Copy the CEF distribution files.

cp -a "$CEF_SOURCE_DIR/Release/"*.so "$stage/lib/release/"
if [ -d "$CEF_SOURCE_DIR/Release/swiftshader" ] ; then
	cp -a "$CEF_SOURCE_DIR/Release/swiftshader" "$stage/lib/release/"
fi
find "$stage/lib/release" -name "*.so" -exec strip -s {} \;
cp -a "$CEF_SOURCE_DIR/Release/"*.bin "$stage/lib/release/"
cp -a "$CEF_SOURCE_DIR/Resources/"* "$stage/lib/release/"
# We do not use the devtools
rm -f "$stage/lib/release/devtools_resources.pak"

# If it is CEF v77 or newer, patch libcef.so so that it won't force-create a
# "Downloads" directory whenever you have XDG_DOWNLOAD_DIR set to your $HOME
# directory (we replace "Downloads" by ".local", which is always present in
# all accounts under (modern) Linux). Since files downloads is disabled in the
# viewer, we really do not care about this "Downloads" directory anyway !
if (( $CEF_MAJOR >= 77 )) ; then
	# Seen in CEF 85+:
	perl -pi -e 's/\0Downloads\0/\0.local\0\0\0\0/' "$stage/lib/release/libcef.so"
	# Seen in CEF 81:
	perl -pi -e 's/\x04Downloads\0/\x04.local\0\0\0\0/' "$stage/lib/release/libcef.so"
fi

# We do not use the sandbox at all, so don't bother packaging it
#cp -a "$CEF_SOURCE_DIR/Release/chrome-sandbox" "$stage/lib/release/"
# Change the owner and permissions for the sandbox executable
#EXE="$stage/bin/release/chrome-sandbox"
#if [ "$UID" != "0" ] ; then
#	sudo -- chown root:root $EXE
#	sudo -- chmod 4755 $EXE
#else
#	chown root:root $EXE
#	chmod 4755 $EXE
#fi

# Wrapper library, for linking with the viewer plugin
cp -a "$CEF_SOURCE_DIR/build/libcef_dll_wrapper/libcef_dll_wrapper.a" "$stage/lib/release/"

if [ -d "$DULLAHAN_SOURCE_DIR/../LICENSES" ] ; then
	cp -a "$DULLAHAN_SOURCE_DIR/../LICENSES" "$stage/"
	pushd "$stage/LICENSES"
	chmod -x *
	popd
fi

# Create the tarball

pushd "$stage"
DATE=`date +%Y%m%d`
tarball="dullahan-cef$CEF_MAJOR-$OS-$DATE.tar.bz2"
tar cjf "../$tarball" .
popd
md5sum "$tarball"
