#!/bin/bash

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

CEF_VERSION="100.0.14_g4e5ba66_chromium-100.0.4896.75"
LL_CEF_BUILD="570842"
CEF_TARBALL="cef_bin-${CEF_VERSION}-darwin64-${LL_CEF_BUILD}.tar.bz2"
LL_CEF_URL="https://automated-builds-secondlife-com.s3.amazonaws.com/ct2/98266/868485/$CEF_TARBALL"

CEF_SOURCE_DIR="cef_bin-${CEF_VERSION}-macos"
DULLAHAN_SOURCE_DIR="src"
CEF_MAJOR=`echo "$CEF_VERSION" | cut -d '.' -f 1`

top=`pwd`

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

if ! [ -d "$CEF_SOURCE_DIR" ] ; then
	if ! [ -f "$CEF_TARBALL" ] ; then
		curl -O "$LL_CEF_URL"
	fi
	tar xjf "$CEF_TARBALL"
	mv -f cef "$CEF_SOURCE_DIR"
fi

# Build libcef_dll_wrapper
pushd "$CEF_SOURCE_DIR"
CEF_BASE_DIR=`pwd`
if [ -d build ] ; then
	rm -rf build
fi
mkdir build
pushd build
cmake -G Xcode -DPROJECT_ARCH="x86_64" ..
xcodebuild -project cef.xcodeproj -target libcef_dll_wrapper -configuration Release
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 Xcode -DARCH:STRING="-m64" -DCEF_BASE_DIR:STRING="$top/$CEF_SOURCE_DIR" ..
xcodebuild -project dullahan.xcodeproj -configuration Release -target dullahan
xcodebuild -project dullahan.xcodeproj -configuration Release -target DullahanHelper
popd
popd

mkdir -p "$stage/include/cef"
mkdir -p "$stage/lib/release"

# Copy the CEF distribution files.
cp -a "$CEF_SOURCE_DIR/Release/Chromium Embedded Framework.framework" "$stage/lib/release/"

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

# Copy the static Dullahan library
cp -a src/build/Release/libdullahan.a "$stage/lib/release/"

# Copy the helper as four different executables (this is specific to macOS).
cp -R src/build/Release/DullahanHelper.app "$stage/lib/release/"
cp -R src/build/Release/DullahanHelper.app "$stage/lib/release/DullahanHelper (GPU).app"
mv -f "$stage/lib/release/DullahanHelper (GPU).app/Contents/MacOS/DullahanHelper" "$stage/lib/release/DullahanHelper (GPU).app/Contents/MacOS/DullahanHelper (GPU)"
cp -R "src/build/Release/DullahanHelper.app" "$stage/lib/release/DullahanHelper (Renderer).app"
mv -f "$stage/lib/release/DullahanHelper (Renderer).app/Contents/MacOS/DullahanHelper" "$stage/lib/release/DullahanHelper (Renderer).app/Contents/MacOS/DullahanHelper (Renderer)"
cp -R "src/build/Release/DullahanHelper.app" "$stage/lib/release/DullahanHelper (Plugin).app"
mv -f "$stage/lib/release/DullahanHelper (Plugin).app/Contents/MacOS/DullahanHelper" "$stage/lib/release/DullahanHelper (Plugin).app/Contents/MacOS/DullahanHelper (Plugin)"
# Patch the Info.plist file corresponding to each renamed executable.
sed -i -e 's/DullahanHelper/DullahanHelper (GPU)/' "$stage/lib/release/DullahanHelper (GPU).app/Contents/Info.plist"
sed -i -e 's/DullahanHelper/DullahanHelper (Renderer)/' "$stage/lib/release/DullahanHelper (Renderer).app/Contents/Info.plist"
sed -i -e 's/DullahanHelper/DullahanHelper (Plugin)/' "$stage/lib/release/DullahanHelper (Plugin).app/Contents/Info.plist"

# Dullahan headers
cp -a "$DULLAHAN_SOURCE_DIR/dullahan.h" "$stage/include/cef/"
cp -a "$DULLAHAN_SOURCE_DIR/dullahan_version.h" "$stage/include/cef/"

if [ -d LICENSES ] ; then
	cp -a LICENSES "$stage/"
fi

# Create the tarball

pushd "$stage"
DATE=`date +%Y%m%d`
tar cjf "../dullahan-cef$CEF_MAJOR-darwin64-$DATE.tar.bz2" .
popd
sleep 1
md5 -r "dullahan-cef$CEF_MAJOR-darwin64-$DATE.tar.bz2"
