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

APP=Iridium
LOWERAPP=iridium-browser

wget -qO - https://downloads.iridiumbrowser.de/ubuntu/iridium-release-sign-01.pub|sudo apt-key add -
sudo apt-add-repository -y 'https://downloads.iridiumbrowser.de/ubuntu main'
sudo apt-get update
sudo apt-get clean
sudo apt-get -y install iridium-browser

mkdir -p $APP/$APP.AppDir
cd $APP/$APP.AppDir
find /var/cache/apt/archives/*deb -exec dpkg -x {} . \; || true

cp ./usr/share/applications/$LOWERAPP.desktop .
rm -rf ./usr/share/icons/48x48/apps || true
find ./usr/share/icons -path *256* -name $LOWERAPP.png -exec cp {} . \; || true
find ./usr/share/icons -path *512* -name $LOWERAPP.png -exec cp {} . \; || true

get_apprun

# Copy in the indirect dependencies
copy_deps ; copy_deps ; copy_deps # Three runs to ensure we catch indirect ones

# Workaround for:
# run-parts: failed to open directory
# /etc/iridium-browser/customizations: No such file or directory
mv etc usr/et
sed -i -e 's|/etc|./et|g' ./usr/bin/iridium-browser
( cd ./usr/et/ ; ln -sf /etc/lsb-release . )

# Workaround for:
# ././/lib/iridium-browser/iridium-browser: ./lib/x86_64-linux-gnu/libnssutil3.so:
# version `NSSUTIL_3.21' not found (required by /usr/lib/x86_64-linux-gnu/libnss3.so)
rm -rf ./usr/lib/x86_64-linux-gnu/libnssutil3.so

move_lib

delete_blacklisted

# Workaround for:
# libsoftokn3.so: cannot open shared object file: No such file or directory
# [7746:7778:0427/202822:ERROR:nss_util.cc(209)] Error initializing NSS without a persistent database: NSS error code: -5925
rm ./usr/lib/x86_64-linux-gnu/libnss3.so

# patch_usr
# Patching only the executable files seems not to be enough for darktable
find usr/ -type f -exec sed -i -e "s|/usr|././|g" {} \;

# Workaround for:
# GLib-GIO-ERROR **: Settings schema 'org.gtk.Settings.FileChooser' is not installed
# when trying to use the file open dialog
# AppRun exports usr/share/glib-2.0/schemas/ which might be hurting us here
( mkdir -p usr/share/glib-2.0/schemas/ ; cd usr/share/glib-2.0/schemas/ ; ln -s /usr/share/glib-2.0/schemas/gschemas.compiled . )

VERSION=$(find /var/cache/apt/archives/ -name $LOWERAPP"_*" | head -n 1 | cut -d "~" -f 1 | cut -d "_" -f 2 | cut -d "-" -f 1 | sed -e 's|1%3a||g' )
echo $VERSION

get_desktopintegration $LOWERAPP

# Go out of AppImage
cd ..

generate_appimage
