#!/bin/bash
#after 5build has finished.
#got most of this out of oe meta-browser recipe

VER='93.0.4577.82'
REV='7'
OSARCH='amd64'
TOPSFS="chromium_${VER}-${REV}_${OSARCH}"
BIN="chromium-${VER}/out/Default"

if [ ! -f ${BIN}/chrome ];then
 echo "ERROR: ${BIN}/chrome does not exist."
 exit
fi

if [ -d ./${TOPSFS} ];then
 rm -rf ./${TOPSFS}
 sync
fi

mkdir ${TOPSFS}
mkdir -p ${TOPSFS}/usr/lib/chromium/locales
mkdir -p ${TOPSFS}/usr/share/applications
mkdir -p ${TOPSFS}/usr/share/icons/hicolor/24x24/apps
mkdir -p ${TOPSFS}/usr/share/icons/hicolor/48x48/apps
mkdir -p ${TOPSFS}/usr/share/icons/hicolor/64x64/apps
mkdir -p ${TOPSFS}/usr/share/icons/hicolor/256x256/apps
mkdir ${TOPSFS}/usr/share/pixmaps
mkdir ${TOPSFS}/usr/share/metainfo
mkdir ${TOPSFS}/usr/bin

cp -a ${BIN}/chrome ${TOPSFS}/usr/lib/chromium/chromium
cp -a ${BIN}/chromedriver ${TOPSFS}/usr/lib/chromium/
cp -a ${BIN}/chrome_sandbox ${TOPSFS}/usr/lib/chromium/
chmod 4755 ${TOPSFS}/usr/lib/chromium/chrome_sandbox #set suid
ln -s chrome_sandbox ${TOPSFS}/usr/lib/chromium/chrome-sandbox
sync
strip --strip-unneeded ${TOPSFS}/usr/lib/chromium/chromium
strip --strip-unneeded ${TOPSFS}/usr/lib/chromium/chromedriver

cp -a ${BIN}/*.bin ${TOPSFS}/usr/lib/chromium/
cp -a ${BIN}/icudtl.dat ${TOPSFS}/usr/lib/chromium/
sync
#strip --strip-unneeded ${TOPSFS}/usr/lib/*.bin

echo -n '[Desktop Entry]
Version=1.0
Name=Chromium web browser
GenericName=Chromium
Comment=Chromium web browser
Exec=chromium
StartupNotify=true
Terminal=false
Icon=chromium.png
Type=Application
Categories=WebBrowser
MimeType=application/pdf;application/rdf+xml;application/rss+xml;application/xhtml+xml;application/xhtml_xml;application/xml;image/gif;image/jpeg;image/png;image/webp;text/html;text/xml;x-scheme-handler/http;x-scheme-handler/https;
' > ${TOPSFS}/usr/share/applications/chromium.desktop

cp -a chromium-${VER}/chrome/app/theme/chromium/product_logo_24.png ${TOPSFS}/usr/share/icons/hicolor/24x24/apps/chromium.png
cp -a chromium-${VER}/chrome/app/theme/chromium/product_logo_48.png ${TOPSFS}/usr/share/icons/hicolor/48x48/apps/chromium.png
cp -a chromium-${VER}/chrome/app/theme/chromium/product_logo_48.png ${TOPSFS}/usr/share/pixmaps/chromium.png
cp -a chromium-${VER}/chrome/app/theme/chromium/product_logo_64.png ${TOPSFS}/usr/share/icons/hicolor/64x64/apps/chromium.png
cp -a chromium-${VER}/chrome/app/theme/chromium/product_logo_256.png ${TOPSFS}/usr/share/icons/hicolor/256x256/apps/chromium.png

cp -a chromium-${VER}/chrome/installer/linux/common/wrapper ${TOPSFS}/usr/lib/chromium/chromium-wrapper
sed -i -e "s,@@CHANNEL@@,stable,g" ${TOPSFS}/usr/lib/chromium/chromium-wrapper
sed -i -e "s,@@PROGNAME@@,chromium,g" ${TOPSFS}/usr/lib/chromium/chromium-wrapper
ln -s ../lib/chromium/chromium-wrapper ${TOPSFS}/usr/bin/chromium

cp -a ${BIN}/chrome_*.pak ${TOPSFS}/usr/lib/chromium/
cp -a ${BIN}/resources.pak ${TOPSFS}/usr/lib/chromium/

cp -a ${BIN}/locales/*.pak ${TOPSFS}/usr/lib/chromium/locales/
ln -s locales ${TOPSFS}/usr/lib/chromium/remoting_locales

cp -a ${BIN}/*.so ${TOPSFS}/usr/lib/chromium/
cp -a ${BIN}/*.so.1 ${TOPSFS}/usr/lib/chromium/
sync
strip --strip-unneeded ${TOPSFS}/usr/lib/chromium/*.so
strip --strip-unneeded ${TOPSFS}/usr/lib/chromium/*.so.1

if [ -d ${BIN}/swiftshader ];then
 mkdir ${TOPSFS}/usr/lib/chromium/swiftshader
 cp -a ${BIN}/swiftshader/*.so ${TOPSFS}/usr/lib/chromium/swiftshader/
 strip --strip-unneeded ${TOPSFS}/usr/lib/chromium/swiftshader/*.so
 sync
fi

#don't think need these...
#for aEXE in brotli bytecode_builtins_list_generator cddl character_data_generator cppgen_plugin flatc generate_colors_info gen-regexp-special-case ipc_plugin make_top_domain_list_variables mksnapshot protoc protozero_plugin top_domain_generator torque v8_context_snapshot_generator
#do
# if [ -x ${BIN}/${aEXE} ];then
#  cp -a ${BIN}/${aEXE} ${TOPSFS}/usr/lib/chromium/
#  strip --strip-unneeded ${TOPSFS}/usr/lib/chromium/${aEXE}
#  sync
# fi
#done

for aEXE in chromedriver chrome-sandbox crashpad_handler
do
 if [ -x ${BIN}/${aEXE} ];then
  cp -a ${BIN}/${aEXE} ${TOPSFS}/usr/lib/chromium/
  strip --strip-unneeded ${TOPSFS}/usr/lib/chromium/${aEXE}
  sync
 fi
done

#for aSCRIPT in xdg-mime xdg-settings
#do
# if [ -x ${BIN}/${aSCRIPT} ];then
#  cp -a ${BIN}/${aSCRIPT} ${TOPSFS}/usr/lib/chromium/
# fi
#done

if [ -d ${BIN}/angledata ];then
 cp -a ${BIN}/angledata ${TOPSFS}/usr/lib/chromium/
fi

if [ -d ${BIN}/resources ];then
 cp -a ${BIN}/resources ${TOPSFS}/usr/lib/chromium/
fi

cp -a chromium.appdata.xml ${TOPSFS}/usr/share/metainfo/

sync
echo 'done'
echo "in easyos, now run: dir2fs ${TOPSFS}"
###end###
