# TODO: Fine-tune. For example, the theme is entirely missing so far.

# In this example, we use a trusty deb

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

# Needs a specific version (FIXME)
sed -i -e 's|libc|xxxlibc|g' status
cat > status <<\EOF
Package: libc
Status: install ok installed
Architecture: all
Version: 2.19-0ubuntu6

Package: debconf
Status: install ok installed
Architecture: all
Version: 9:999.999.999

Package: libstdc++6
Status: install ok installed
Architecture: all
Version: 9:999.999.999

Package: dconf-gsettings-backend
Status: install ok installed
Architecture: all
Version: 9:999.999.999

EOF

echo "deb http://archive.ubuntu.com/ubuntu/ trusty main universe multiverse
deb http://download.opensuse.org/repositories/home:/strycore/xUbuntu_14.04/ ./
" > 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
get_icon
get_apprun

mv ./usr/lib/x86_64-linux-gnu/pulseaudio/* ./usr/lib/x86_64-linux-gnu/ || true

# Workaround for:
# /usr/lib/x86_64-linux-gnu/libdrm_amdgpu.so.1: error: symbol lookup error:
# undefined symbol: drmGetNodeTypeFromFd (fatal)
# libGL error: unable to load driver: swrast_dri.so
# libGL error: failed to load driver: swrast
rm -rf ./usr/lib/x86_64-linux-gnu/libdrm.* || true

# Without this it segfaults (TODO: to be investigated)
# Most likely we are deleting too much here
rm -rf lib/ lib32/ || true

# 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" {} \;

# Workaround for:
# GdkPixbuf-WARNING **: Cannot open pixbuf loader module file
# '././/lib/x86_64-linux-gnu/gdk-pixbuf-2.0/2.10.0/loaders.cache':
# No such file or directory
sudo apt-get -y install libgdk-pixbuf2.0-dev
( cd usr/ ; gdk-pixbuf-query-loaders > ././/lib/x86_64-linux-gnu/gdk-pixbuf-2.0/2.10.0/loaders.cache )

# delete_blacklisted

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
