#!/bin/bash -x

# In this case, the upstream project seems to already
# provide binaries that are supposed to run everywhere,
# so we merely package these as an AppImage

set +e

URL="https://atom.io/download/deb"
echo $URL

APP=Atom

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

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

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

cd $APP.AppDir/
dpkg -x ../atom*deb* .

get_apprun

# Move icon into place so that AppImageAssistant finds them
cp ./usr/share/pixmaps/atom.png .

# TODO: Get appdata
# mkdir -p ./usr/share/metainfo/
# wget -q "..." -O ./usr/share/metainfo/atom.appdata.xml

# Get desktop file
cp ./usr/share/applications/atom.desktop .
sed -i -e "s|/usr/share/atom/||g" atom.desktop

# Remove launcher and move in place
# TODO: Replace this by something cleaner
rm usr/bin/atom
mv usr/share/atom/* usr/bin/

# Add desktop integration
XAPP=atom
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

# Figure out $VERSION
VERSION=$(grep -r "atom@" ./usr/share/doc/atom/copyright | cut -d " " -f 2 | cut -d "@" -f 2)
echo $VERSION

# Go out of AppImage
cd ..

generate_type2_appimage
