# We don't have to build this one from source
# because upstream is providing binaries that seem to be usable for
# creating an AppImage; hence this recipe is very short and quick

# This recipe takes the official deb and converts it into an AppImage
# that should run on most not-too-outdated Linux distributions (not just
# debian-derived ones).

APP=Brackets
LOWERAPP=${APP,,}

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

cd ./$APP/

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

# We get this app and almost all its dependencies via apt-get
# but not using the host system's information about what is
# installed on the system but our own assumptions instead

generate_status

echo "deb http://ftp.de.debian.org/debian/ oldstable main contrib non-free
" > sources.list

LINK=$(wget -q https://github.com/adobe/brackets/releases -O - | grep download | grep 64-bit.deb | head -n 1 | cut -d '"' -f 2)
URL="https://github.com${LINK}"
echo $URL
# wget -c  --trust-server-names "$URL" # broken
wget -c "$URL" -O $LOWERAPP.deb

# Add local repository so that we can install deb files
# that were downloaded outside of a repository
dpkg-scanpackages . /dev/null | gzip -9c > Packages.gz
zcat Packages.gz
echo "deb file:$(readlink -e $PWD) ./" >> sources.list

apt-get $OPTIONS update

URLS=$(apt-get $OPTIONS -y install --print-uris $LOWERAPP | cut -d "'" -f 2 | grep -e "^http")

wget -c $URLS

cd ./$APP.AppDir/

find ../*.deb -exec dpkg -x {} . \; || true

rm usr/bin/brackets
mv opt/brackets/* usr/bin/
rm -rf ./opt/

wget -c "https://github.com/probonopd/AppImageKit/releases/download/5/AppRun" # (64-bit)
chmod a+x ./AppRun

cp ./usr/share/icons/hicolor/scalable/apps/brackets.svg .

cp ./usr/bin/brackets.desktop .
echo "Comment=An open source code editor for web designers and front-end developers. Supported by Adobe." >> brackets.desktop

move_lib

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

# Figure out $VERSION
VERSION=$(cat $APP.AppDir//usr/bin/www/config.json | grep version | cut -d '"' -f 4 | sed -e 's|-|.|g')
echo $VERSION

wget -c "https://github.com/probonopd/AppImageKit/releases/download/5/AppImageAssistant" # (64-bit)
chmod a+x ./AppImageAssistant
mkdir -p ../out
rm ../out/$APP"-"$VERSION"-x86_64.AppImage" || true
./AppImageAssistant ./$APP.AppDir/ ../out/$APP"-"$VERSION"-x86_64.AppImage"
