# The Opera EULA chrome://about/eula allows:
# "to reproduce and distribute the Software without modification,
# provided that any such reproduction or distribution of the Software by you:
# (a) must be in an application repository for a desktop
# Open Source OS distribution (expressly excluding distribution
# for embedded Open Source OS);
# (b) must be made available free of charge for end-users;
# (c) must be subject to and distributed with a copy of this Agreement; and
# (d) no automatic modification of the default search engines in the
# Software settings is done at any time, including but not limited
# to after the Software is installed.

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

REDIR=$(wget -U x86_64 -q -O - "http://www.opera.com/download/get/?partner=www&opsys=Linux&product=Opera%20developer" | grep download/get | head -n 1 | grep -Poe "href.*?\ " | cut -d '"' -f 2)

APP=Opera
LOWERAPP=${APP,,}

mkdir -p $APP/$APP.AppDir

cd $APP/

wget -c --trust-server-names "http://www.opera.com/$REDIR"

VERSION=$(ls *deb | cut -d "_" -f 2 | head -n 1)

cd $APP.AppDir

dpkg -x ../*.deb .

get_apprun

cp ./usr/share/applications/$LOWERAPP*.desktop $LOWERAPP.desktop

(cd usr/bin ; ln -s opera-developer opera)

# Workaround for:
# error: file contains key "TargetEnvironment" in group
# "Desktop Action NewWindow", but keys extending the format should start with "X-"
sed -i -e 's|TargetEnvironment|X-TargetEnvironment|g' $LOWERAPP.desktop
sed -i -e 's|-developer||g' $LOWERAPP.desktop

cp ./usr/share/icons/hicolor/128x128/apps/$LOWERAPP*.png $LOWERAPP.png
rm -rf ./usr/share/icons/hicolor/48x48
rm -rf home/ || true # Likely a bug

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

move_lib

delete_blacklisted

# Workaround for:
# libsoftokn3.so: cannot open shared object file: No such file or directory
rm -f ./usr/lib/x86_64-linux-gnu/libnss3.so

patch_usr

echo $VERSION

get_desktopintegration $LOWERAPP

# Go out of AppImage
cd ..

generate_appimage
