# NOTE: This example is provided here for historical reasons only.
# Ultimaker provides official AppImage downloads now.
#
# This is an example on how to bundle a complex Python application as an AppImage
# using virtualenv and AppImageKit using Ubuntu
# NOTE: Please test the resulting AppImage on your target systems and copy in any additional
# libraries and/or dependencies that might be missing on your target system(s).

########################################################################
# Get virtualenv on the target system
########################################################################

sudo apt-get update
sudo apt-get -y install python-virtualenv

########################################################################
# Create the AppDir
########################################################################

APP=Cura
LOWERAPP=${APP,,} 

mkdir -p $APP/$APP.AppDir/

cd $APP/

########################################################################
# Source some helper functions
########################################################################

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

########################################################################
# Download debs
########################################################################

DEB=$(wget -c "http://software.ultimaker.com/current/?C=M;O=D" -O - | grep debian_amd64.deb | head -n 1 | cut -d '"' -f 8)
URL="http://software.ultimaker.com/current/$DEB"
wget -c "$URL"

sudo apt-get -y  remove python-serial
sudo apt-get clean
sudo apt-get -y install --download-only python-setuptools python-wxgtk2.8 python-serial python-numpy python-opengl freeglut3

cd $APP.AppDir/

########################################################################
# Create a virtualenv inside the AppDir
########################################################################

virtualenv usr

# Install python dependencies into the virtualenv
# ./usr/bin/pip install ...

# The following ones cannot be installed using pip
find /var/cache/apt/archives/ -name *deb -exec dpkg -x {} . \;
rm -f ./usr/lib/python2.7/lib-dynload # pip created a symlink to /usr there; stupid
cp -r /usr/lib/python2.7/lib-dynload ./usr/lib/python2.7/
cp /usr/lib/python2.7/hashlib.py usr/lib/python2.7/
cp /lib/x86_64-linux-gnu/libcrypto.so.1.0.0 ./usr/lib/

mv ./usr/lib/python2.7/dist-packages/wx*/wx/ ./usr/lib/python2.7/dist-packages/

########################################################################
# "Install" a simple sample app in the AppDir
########################################################################

find ../ -name *.deb -exec dpkg -x {} . \;
find /var/cache/apt/archives/ -name *.deb -exec dpkg -x {} . \;
sed -i -e 's|/usr|././|g' usr/bin/cura

########################################################################
# Finalize the AppDir
########################################################################

get_apprun

find . -name *.desktop -exec cp {} . \;
sed -i -e 's|/usr/bin/python /usr/share/cura/cura.py|cura|g' $LOWERAPP.desktop
sed -i -e 's|/usr/share/cura/resources/images/c.png|cura|g' $LOWERAPP.desktop
sed -i -e 's|Path=|X-Disabled-Path=|g' $LOWERAPP.desktop
sed -i -e 's|Comment=Cura|Comment=Ultimaker Cura|g' $LOWERAPP.desktop

cp usr/share/cura/resources/images/c.png $LOWERAPP.png

# Make the AppImage ask to "install" itself into the menu
get_desktopintegration $LOWERAPP

########################################################################
# Bundle dependencies
########################################################################

copy_deps

delete_blacklisted

# Workaround for:
# /usr/lib/x86_64-linux-gnu/libgdk-x11-2.0.so.0: error: symbol lookup error: undefined symbol: XRRGetMonitors (fatal)
rm 'usr/lib/x86_64-linux-gnu/libXrandr.so.2'

move_lib

VERSION=$(ls ../cura_*.deb | cut -d _ -f 2 | cut -d "-" -f 1)

########################################################################
# Package the AppDir as an AppImage
########################################################################

# At this point, you should be able to run the application by executing
# ./AppRun

cd ..

generate_appimage
