#!/bin/bash -x

# We do not have to build this ourselves since the upstream project
# does a great job in providing binaries that run on a great number of systems
# other upstream projects could learn from Mozilla - how do they do it?

set +e

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

APP=Thunderbird
LOWERAPP=${APP,,}

mkdir -p ./$APP/$APP.AppDir/usr
cd ./$APP

wget -c --trust-server-names "https://download.mozilla.org/?product=thunderbird-latest&os=linux64"
VERSION=$(ls thunderbird-*.tar.bz2 | cut -d "-" -f 2 | sed -e 's|.tar.bz2||g')

cd ./$APP.AppDir
tar xfj ../*.tar.bz2

mv thunderbird usr/bin

find . -name default256.png -exec cp \{\} thunderbird.png \;

cat > thunderbird.desktop <<EOF
[Desktop Entry]
Type=Application
Name=Thunderbird Mail
Icon=thunderbird
Exec=thunderbird %u
Categories=Application;Office;Network;Email;
MimeType=x-scheme-handler/mailto;application/x-xpinstall;
StartupNotify=true
EOF

get_apprun

# Thunderbird does not ship with appdata yet
mkdir -p usr/share/metainfo/
wget -c "https://raw.githubusercontent.com/hughsie/fedora-appstream/master/appdata-extra/desktop/mozilla-thunderbird.appdata.xml" -O usr/share/metainfo/thunderbird.appdata.xml

# https://github.com/hughsie/fedora-appstream/issues/165
sed -i -e 's|mozilla-thunderbird.desktop|thunderbird.desktop|g' usr/share/metainfo/thunderbird.appdata.xml

# Conform to https://github.com/hughsie/appstream-glib#guidelines-for-applications
mkdir -p usr/share/icons/hicolor/256x256/apps/
cp usr/bin/chrome/icons/default/default256.png usr/share/icons/hicolor/256x256/apps/thunderbird.png
mkdir -p usr/share/applications
cp thunderbird.desktop usr/share/applications

get_desktopintegration $LOWERAPP

rm -rf thunderbird

cd ..

generate_appimage
