#!/bin/bash

if [ ! -e /usr/bin/node ];then
 echo "ERROR: host system needs 'nodejs' package installed, with /usr/bin/node"
 exit
fi
if [ ! -e /usr/bin/ninja ];then
 echo "ERROR: host system needs /usr/bin/ninja"
 exit
fi
if [ ! -e /usr/bin/gcc ];then
 echo "ERROR: host system needs /usr/bin/gcc"
 exit
fi

if [ ! -d depot_tools ];then
 echo "ERROR: a terminal must be opened in the 'chromium-project' folder and"
 echo "       this script run from the terminal window."
 exit
fi

if [ ! -f chromium-93.0.4577.82.tar.xz ];then
 echo "ERROR: chromium-93.0.4577.82.tar.xz has not been downloaded."
 exit
fi

ARCH="`arch`"
case "$ARCH" in
 x86_64) ARCH='amd64' ;;
 aarch64) ARCH='arm64' ;;
 *) exit ;;
esac

#if [ ! -f debian_sid_${ARCH}_sysroot.tar.xz ];then
# echo "ERROR: debian_sid_${ARCH}_sysroot.tar.xz has not been downloaded."
# exit
#fi

PWD="`pwd`"

PATH="${PATH}:${PWD}/depot_tools"

if [ ! "$JAVA_HOME" ];then
 echo 'ERROR: jdk not installed and $JAVA_HOME not set.'
 exit
fi
export PATH="${JAVA_HOME}/bin:${PATH}"

#start from scratch...
if [ -d chromium-93.0.4577.82 ];then
 rm -rf chromium-93.0.4577.82
 sync
fi

tar -xf chromium-93.0.4577.82.tar.xz
if [ $? -ne 0 ];then
 echo "ERROR: expanding chromium-93.0.4577.82.tar.xz"
 if [ -d chromium-93.0.4577.82 ];then
  rm -rf chromium-93.0.4577.82
 fi
 exit
fi
sync
if [ ! -d chromium-93.0.4577.82 ];then
 echo "ERROR: folder chromium-93.0.4577.82 does not exist"
 exit
fi

cd chromium-93.0.4577.82

../apply-patches
if [ $? -ne 0 ];then
 echo "ERROR: patches did not all apply."
 exit
fi

../apply-ungoogled-patches

#important, has a patch to fix text rendering in sandbox...
../apply-oe-patches

export CC=gcc
export CXX=g++

mkdir -p third_party/node/linux/node-linux-x64/bin
ln -s /usr/bin/node third_party/node/linux/node-linux-x64/bin/node

#don't need this...
##expand the debian sysroot...
#mkdir -p build/linux/debian_sid_${ARCH}-sysroot
#cd build/linux/debian_sid_${ARCH}-sysroot
#tar -xf ../../../../debian_sid_${ARCH}_sysroot.tar.xz
#cd ../../../


#stop warning msg when run with --no-sandbox
#sed -i -e 's%IDS_BAD_FLAGS_WARNING_MESSAGE%IDS_BAD_FLAGS_WARNING_MESSAGExxx%' chrome/app/generated_resources.grd
sed -i -e 's%ShowBadFlagsInfoBar(web_contents%// ShowBadFlagsInfoBar(web_contents%' chrome/browser/ui/startup/bad_flags_prompt.cc

#IDS_MISSING_GOOGLE_API_KEYS in chrome/app/chromium_strings.grd
# and browser/ui/startup/google_api_keys_infobar_delegate.cc

#stop fail that jre exists...
sed -i -e 's%^assert%# assert%' third_party/closure_compiler/compiler.py

#jre has to be copied here...
cp -a -f --remove-destination ${JAVA_HOME}/bin/* third_party/jdk/current/bin/
cp -a -f --remove-destination ${JAVA_HOME}/lib/* third_party/jdk/current/lib/

#pdfium compile fails, got it from 95.x...
cd third_party
mv -f pdfium pdfiumHIDE
tar -xf ../../pdfium-95.0.4638.10.tar.gz
sync
mv -f pdfium-95.0.4638.10 pdfium
cd ..

#create build folder...
gn gen out/Default --args="rtc_use_pipewire=false use_sysroot=false safe_browsing_mode=0 use_gnome_keyring=false ozone_platform_wayland=false"
if [ $? -ne 0 ];then
 echo "ERROR: creating out/Default"
fi
###end###
