#!/bin/bash -x

# https://download-chromium.appspot.com/
# This is a raw build of Chromium for Linux x64, right off the trunk. It may be tremendously buggy.
# Hence we take the "Last Known Good Revision".

set +e

mkdir -p chromium/chromium.AppDir
cd chromium/

wget -q https://github.com/probonopd/AppImages/raw/master/functions.sh -O ./functions.sh
. ./functions.sh

wget -c --trust-server-names "https://download-chromium.appspot.com/dl/Linux_x64?type=snapshots"
# wget -c --trust-server-names "https://download-chromium.appspot.com/dl/Linux_x64?type=continuous"
wget -c --trust-server-names http://mirrors.kernel.org/ubuntu/pool/main/u/udev/libudev0_151-12.3_amd64.deb
wget -c --trust-server-names http://ftp.us.debian.org/debian/pool/main/libx/libxss/libxss1_1.2.0-2_amd64.deb

cd chromium.AppDir/

unzip ../chrome-linux.zip
find ../ -name *.deb -exec dpkg -x \{\} . \;

find -name product_logo_48.png -exec cp \{\} chromium.png \;

mkdir -p usr/bin
cd usr/bin
ln -s ../../chrome-linux/chrome-wrapper chrome

cd -

cat > ./AppRun <<\EOF
#!/bin/sh
HERE=$(dirname $(readlink -f "${0}"))
export LD_LIBRARY_PATH="${HERE}"/usr/lib:$PATH
"${HERE}"/usr/bin/chrome $@
EOF

chmod a+x ./AppRun

find . -type f -executable -exec strip {} \;

cat > ./chromium.desktop <<\EOF
[Desktop Entry]
Name=Chromium
Icon=chromium
Exec=chromium %u
Categories=Network;WebBrowser;
StartupNotify=true
EOF

VERSION=$(strings ./chrome-linux/chrome | grep -e Chrome_Linux -C 5 | grep -e "^[0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+")

# mkdir -p usr/share/metainfo/
# wget -c --trust-server-names https://github.com/hughsie/fedora-appstream/raw/master/appdata-extra/desktop/chromium.appdata.xml -O usr/share/metainfo/chromium.appdata.xml

cd ..

generate_type2_appimage
