#!/bin/sh
# Script to create a PyQT deb package using fpm
#
# This script will create a folder structure that will be used as input
# for the fpm command and copy into it all the distributable files generated by
# pyinstaller.
# We will create a folder with the same structure Linux systems expects:
#  - /opt for our executable and associated files (a.k.a our dist/ folder)
#  - /usr/share/applications (for the .desktop file)
#  - /usr/share/icons/hicolor/scalable/apps for our svg icons (only svg in scalable folder)
#
# More info:
#  - https://www.pythonguis.com/tutorials/packaging-pyqt5-applications-linux-pyinstaller/
#  - https://fpm.readthedocs.io/en/latest/packages/dir.html#dir-local-files
#
# Create folders
[ -e tmp ] && rm -r tmp
mkdir -p tmp/opt
mkdir -p tmp/usr/share/applications
mkdir -p tmp/usr/share/icons/hicolor/scalable/apps

# Build the project
[ -e build ] && rm -r build
[ -e dist ] && rm -r dist
python build.py

# Copy files
cp -r dist/opendataeditor tmp/opt/opendataeditor
cp ./packaging/linux/icon.svg tmp/usr/share/icons/hicolor/scalable/apps/org.okfn.opendataeditor.svg
cp ./packaging/linux/opendataeditor.desktop tmp/usr/share/applications

# Change permissions
# Packages retain the permissions of installed files from when they were packaged,
# but will be installed by root. In order for ordinary users to be able to run the
# application, we need to change the permissions.
find tmp/opt/opendataeditor -type f -exec chmod 644 -- {} +
find tmp/opt/opendataeditor -type d -exec chmod 755 -- {} +
find tmp/usr/share -type f -exec chmod 644 -- {} +
chmod +x tmp/opt/opendataeditor/opendataeditor

# Create the deb package
VERSION=$(python -c "import ode; print(ode.__version__)")
FILENAME=opendataeditor-linux-$VERSION.deb
[ -e dist/$FILENAME ] && rm dist/$FILENAME
fpm -C tmp -s dir -t deb -n "opendataeditor" -v $VERSION  -p dist/$FILENAME
