#!/bin/bash
+##
+## Copyright (C) 2016 Simon Peter
+## Copyright (C) 2017-2018 Uwe Hermann <uwe@hermann-uwe.de>
+## This file is licensed under the terms of the MIT license.
+##
-# (c) 2016 Simon Peter
-# This file is licensed under the terms of the MIT license.
-#
-# Bundle PulseView and its dependencies as an AppImage for x86_64 Linux
-# TODO: Change it to use build products and libraries from a more mature
-# distribution such as Debian oldstable, CentOS 6 or Ubuntu Trusty or older.
+# Bundle PulseView/sigrok-cli (and deps) as an AppImage for x86_64/i386 Linux.
+# Note: This assumes the full sigrok stack has been installed into $PREFIX.
-APP=PulseView
-LOWERAPP=${APP,,}
+PREFIX=$HOME/sr
-ARCH=x86_64
+APPIMAGEKIT_OUTDIR=$HOME/AppImageKit/build/out
-mkdir -p ./$APP/$APP.AppDir/usr/bin ./$APP/$APP.AppDir/usr/lib
-cd ./$APP
+# ARCH=i386
+ARCH=x86_64
-wget -q https://github.com/probonopd/AppImages/raw/master/functions.sh -O ./functions.sh
-. ./functions.sh
+PYVER=3.4
########################################################################
-# Get runtime dependencies
+# You usually don't have to change anything below this line
########################################################################
-sudo apt-get update
+if [ "x$1" = "xsigrok-cli" ]; then
+ APP=sigrok-cli
+else
+ APP=PulseView
+fi
+LOWERAPP=${APP,,}
-# The following are installed into the host system so that we can bundle them into the AppImage as dependencies
-sudo apt-get -y --force-yes install libqt5svg5 libqt5gui5 libboost-thread1.58.0 libboost-test1.58.0 libboost-chrono1.58.0 libboost-atomic1.58.0 libftdi1-2 libzip4
+export STATIC_FILES=`pwd`/contrib
-########################################################################
-# Get build products from Jenkins
-# FIXME: Do the actual building here instead
-########################################################################
+# Add $APPIMAGEKIT_OUTDIR so we can find all the binaries there.
+export PATH=$APPIMAGEKIT_OUTDIR:$PATH
+
+A="./$APP/$APP.AppDir"
+mkdir -p $A/usr/bin $A/usr/lib $A/usr/share
+cd ./$APP
-wget http://sigrok.org/jenkins/job/pulseview/buildtype=shared,compiler=gcc,platform=native-amd64/ws/_inst/bin/*zip*/bin.zip
-unzip bin.zip
+. $STATIC_FILES/functions.sh
-FW=$(wget -q "http://sigrok.org/download/binary/sigrok-firmware-fx2lafw/?C=M;O=D" -O - | grep .tar.gz | head -n 1 | cut -d '"' -f 13 | cut -d ">" -f 2 | cut -d "<" -f 1)
-wget -c "http://sigrok.org/download/binary/sigrok-firmware-fx2lafw/$FW"
-tar xf sigrok-firmware-fx2lafw-bin-*.tar.gz
-rm sigrok-firmware-fx2lafw-bin-*.tar.gz
+########################################################################
+# Get build products from $PREFIX
+########################################################################
cd $APP.AppDir/
-mv ../bin/pulseview usr/bin/
+cp $PREFIX/bin/$LOWERAPP usr/bin/
chmod a+x usr/bin/*
-
-cd usr/lib/
-wget http://sigrok.org/jenkins/job/libsigrok/buildtype=shared,compiler=gcc,platform=native-amd64/ws/_inst/lib/libsigrok.so.3
-wget http://sigrok.org/jenkins/job/libsigrok/buildtype=shared,compiler=gcc,platform=native-amd64/ws/_inst/lib/libsigrokcxx.so.3
-wget http://sigrok.org/jenkins/job/libserialport/buildtype=shared,compiler=gcc,platform=native-amd64/lastSuccessfulBuild/artifact/_inst/lib/libserialport.so.0
-cd ../../
-
-wget "http://sigrok.org/jenkins/job/libsigrokdecode/buildtype=shared,compiler=gcc,platform=native-amd64/lastSuccessfulBuild/artifact/*zip*/archive.zip"
-unzip archive.zip
-mv archive/_inst/share/libsigrokdecode usr/share/libsigrokdecode
-mv archive/_inst/lib/libsigrokdecode.so.3 usr/lib/
-rm -rf archive*
+cp $PREFIX/lib/lib*.so* usr/lib/
+cp -r $PREFIX/share/libsigrokdecode usr/share/
+cp -r $PREFIX/share/sigrok-firmware usr/share/
+mkdir -p usr/share/applications
+cp $PREFIX/share/applications/org.sigrok.$APP.desktop usr/share/applications
+cp -r $PREFIX/share/icons usr/share/
+cp -r $PREFIX/share/metainfo usr/share/
+cp -r $PREFIX/share/mime usr/share/
+
+# Drop unneeded stuff.
+if [ "x$1" = "xsigrok-cli" ]; then
+ rm -f usr/lib/libsigc*
+ rm -f usr/lib/libglibmm*
+ rm -f usr/lib/libsigrokcxx*
+ rm -f usr/share/icons/hicolor/*/apps/pulseview.*
+ rm -f usr/share/metainfo/org.sigrok.PulseView.appdata.xml
+else
+ rm -f usr/share/icons/hicolor/scalable/apps/sigrok-cli.svg
+fi
# Reduce binary size
strip usr/bin/*
# AppRun is the main launcher that gets executed when AppImage is run
########################################################################
-get_apprun
+cp $APPIMAGEKIT_OUTDIR/AppRun .
########################################################################
# Copy desktop and icon file to AppDir for AppRun to pick them up
########################################################################
-wget http://sigrok.org/jenkins/job/pulseview/buildtype=shared,compiler=gcc,platform=native-amd64/ws/contrib/pulseview.desktop
-wget -q "http://sigrok.org/gitweb/?p=pulseview.git;a=blob_plain;f=icons/sigrok-logo-notext.png" -O sigrok-logo-notext.png
-
-########################################################################
-# Patch away absolute paths; it would be nice if they were relative
-########################################################################
-
-sed -i -e 's|/home/jenkins_slave/fsroot/workspace/libsigrokdecode/buildtype/shared/compiler/gcc/platform/native-amd64/_inst/share/|.//////////////////////////////////////////////////////////////////////////////////////////////////////////////share/|g' usr/lib/libsigrokdecode.so.3
+cp $PREFIX/share/applications/org.sigrok.$APP.desktop .
+cp $PREFIX/share/icons/hicolor/scalable/apps/$LOWERAPP.svg .
########################################################################
# Copy in the dependencies that cannot be assumed to be available
# on all target systems
########################################################################
-mkdir -p usr/share/
-mv ../sigrok-firmware-* usr/share/sigrok-firmware
-
copy_deps
+if [ "x$1" != "xsigrok-cli" ]; then
+ # Get all Qt5 plugins (won't be copied automatically).
+ QT5PLUGINS=/usr/lib/$ARCH-linux-gnu/qt5/plugins # Host (+ AppRun) path.
+ mkdir -p .$QT5PLUGINS
+ cp -r $QT5PLUGINS/accessible .$QT5PLUGINS
+ cp -r $QT5PLUGINS/generic .$QT5PLUGINS
+ cp -r $QT5PLUGINS/iconengines .$QT5PLUGINS
+ cp -r $QT5PLUGINS/imageformats .$QT5PLUGINS
+ cp -r $QT5PLUGINS/platforminputcontexts .$QT5PLUGINS
+ cp -r $QT5PLUGINS/platforms .$QT5PLUGINS
+
+ # Get some additional dependencies of the Qt5 plugins.
+ ldd .$QT5PLUGINS/platforms/libqxcb.so | grep "=>" | awk '{print $3}' | xargs -I '{}' cp -v '{}' ./usr/lib || true
+ ldd .$QT5PLUGINS/imageformats/libqsvg.so | grep "=>" | awk '{print $3}' | xargs -I '{}' cp -v '{}' ./usr/lib || true
+fi
+
+# Python 3
+cp /usr/lib/$ARCH-linux-gnu/libpython$PYVER* ./usr/lib
+mkdir -p ./usr/share/pyshared
+cp -r /usr/lib/python$PYVER/* ./usr/share/pyshared # AppRun expects this path.
+
+cp -r ./usr/share/pyshared/plat-$ARCH-linux-gnu/* ./usr/share/pyshared
+
########################################################################
# Delete stuff that should not go into the AppImage
########################################################################
-delete_blacklisted
move_lib
-mv ./usr/lib/x86_64-linux-gnu/* usr/lib/
-rm -r ./usr/lib/x86_64-linux-gnu/
+mv ./usr/lib/$ARCH-linux-gnu/* usr/lib/
+rm -r ./usr/lib/$ARCH-linux-gnu/
+
+delete_blacklisted
+
+# Remove some incorrectly/unintentionally copied files.
+rm -r ./home
########################################################################
-# Determine the version of the app; also include needed glibc version
+# Determine the version of the app
########################################################################
-VER1=$(./AppRun --version | cut -d " " -f 2)
-GLIBC_NEEDED=$(glibc_needed)
-VERSION=$VER1.glibc$GLIBC_NEEDED
+VERSION="NIGHTLY"
echo $VERSION
########################################################################
# Patch away absolute paths; it would be nice if they were relative
########################################################################
-patch_usr
+find usr/ -type f -executable -exec sed -i -e "s|/usr|././|g" {} \;
########################################################################
# AppDir complete
cd ..
-generate_appimage
+VERSION=$VERSION $APPIMAGEKIT_OUTDIR/appimagetool ./$APP.AppDir/
+mkdir -p ../out/ || true
+mv *.AppImage* ../out/