APP=LMMS
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://archive.ubuntu.com/ubuntu/ trusty main universe
deb http://ppa.launchpad.net/kxstudio-debian/libs/ubuntu trusty main
deb http://ppa.launchpad.net/kxstudio-debian/music/ubuntu trusty main
deb http://ppa.launchpad.net/kxstudio-debian/plugins/ubuntu trusty main
deb http://ppa.launchpad.net/kxstudio-debian/apps/ubuntu trusty main
deb http://ppa.launchpad.net/kxstudio-debian/kxstudio/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

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

sed -i -e "s|env QT_X11_NO_NATIVE_MENUBAR=1 ||g" $LOWERAPP.desktop

rm -rf ./usr/share/icons/48x48/apps || true
find ./usr/share/icons -name $LOWERAPP.png -exec cp {} . \; || true
find ./usr/share/icons -path *256* -name $LOWERAPP.png -exec cp {} . \; || true
find ./usr/share/icons -path *512* -name $LOWERAPP.png -exec cp {} . \; || true

get_apprun

# Copy in the indirect dependencies
# copy_deps # not needed here due to the way we use apt to download everything

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

mv ./usr/lib/pulseaudio/*.so usr/lib/

delete_blacklisted
rm -rf ./etc/ ./home/ || 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" {} \;

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
