# 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=VSCode
LOWERAPP=code # ${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

URL="https://vscode-update.azurewebsites.net/latest/linux-deb-x64/stable"
wget -c  --trust-server-names "$URL"

# 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
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

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

# Dear upstream developers, in debian, binaries ought to be in usr/bin
# and without a path in the .desktop files
sed -i -e 's|/usr/share/code/||g' $LOWERAPP.desktop

# Currently the Bintray upload script doesn't deal with blanks
# in an application name correctly (FIXME)
sed -i -e 's|Visual Studio Code|VSCode|g' $LOWERAPP.desktop

cp ./usr/share/pixmaps/code.png .

get_apprun

mv ./usr/share/code usr/bin

move_lib
mv ./usr/lib/x86_64-linux-gnu/* ./usr/lib/ # AppRun sets Qt env here

delete_blacklisted
rm -rf ./etc/ ./home/ || true

VERSION=$(find ../*.deb -name $LOWERAPP"_*" | head -n 1 | cut -d "~" -f 1 | cut -d "_" -f 2 | cut -d "-" -f 1 | sed -e 's|1%3a||g' )
echo $VERSION

get_desktopintegration $LOWERAPP

# Go out of AppImage
cd ..

generate_appimage
