# Note: Etcher now provides an official AppImage,
# so this is for historic reasons only. Use the official Etcher AppImage from their website instead.
#
# This is an example for an Electron-based app that is not a text editor.
# We are using their precompiled binaries.

APP=Etcher

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

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

# Get the current version from the GitHub Releases page
# since on http://www.etcher.io/ it is not accessible
# (probably hidden deep inside some JavaScript?), stupid.

VERSION=$(wget "https://github.com/resin-io/etcher/releases" -q -O - | grep tag-name | head -n 1 | cut -d '>' -f 2 | cut -d '<' -f 1)
VERSION=${VERSION:1} # Remove first character which is "v"

URL="https://resin-production-downloads.s3.amazonaws.com/etcher/${VERSION}/Etcher-linux-x64.tar.gz"

wget -c --trust-server-names "$URL"

cd $APP.AppDir

tar xf ../Etcher-*

mkdir -p usr/bin
mv etcher-release/Etcher-linux-x64/* usr/bin/
rm -r etcher-release/

get_apprun

cat > Etcher.desktop <<EOF
[Desktop Entry]
Name=Etcher
Exec=Etcher
Description=A better way to flash OS images to SD cards
Icon=etcher
Type=Application
EOF

wget -q "https://github.com/resin-io/etcher/raw/master/assets/icon.png" -O etcher.png

# Add desktop integration - doesn't play well with apps run as root yet
# XAPP=Etcher
# wget -O ./usr/bin/$XAPP.wrapper https://raw.githubusercontent.com/probonopd/AppImageKit/master/desktopintegration
# chmod a+x ./usr/bin/$XAPP.wrapper
# sed -i -e "s|Exec=$XAPP|Exec=$XAPP.wrapper|g" $XAPP.desktop

cd .. # Go out of AppDir

generate_appimage
