APP=Corebird
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

generate_status

echo "deb http://archive.ubuntu.com/ubuntu/ trusty main universe
deb http://ppa.launchpad.net/ubuntuhandbook1/corebird/ubuntu trusty main
deb http://ppa.launchpad.net/gnome3-team/gnome3/ubuntu trusty main
deb http://ppa.launchpad.net/gnome3-team/gnome3-staging/ubuntu trusty main
" > 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

# get_desktop
cp ./usr/share/applications/org.baedert.corebird.desktop $LOWERAPP.desktop
sed -i -e 's|^Exec=.*|Exec=corebird|g' $LOWERAPP.desktop
get_icon
get_apprun

# Workaround for:
# GLib-GIO-ERROR **: Settings schema 'org.baedert.corebird' is not installed
# AppRun exports usr/share/glib-2.0/schemas/
(  cd usr/share/glib-2.0/schemas/ ; glib-compile-schemas . )

# Dear upstream authors, please make your app relocateable
# e.g., using binreloc, rather than hardcoding absolute paths
#
# Workaround for:
# Failed to open file '/usr/share/corebird//no_avatar.png': No such file or directory
# patch_usr
# Patching only the executable files seems not to be enough for some apps
find usr/ -type f -exec sed -i -e "s|/usr|././|g" {} \;

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_type2_appimage
