#! /bin/bash
#
# This file is part of the Collabora Office project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#

# Assemble Flatpak app files and metadata under /app/, copying from the
# installation tree generated by 'make distro-pack-install' (at
# $PREFIXDIR).
#
# Arguments:
# $1  the flatpak ID

set -e

cp -r "${PREFIXDIR?}"/lib/collaboraoffice /app/
ln -s /app/collaboraoffice/program/soffice /app/bin/collaboraoffice

mkdir -p /app/share/applications
"${SRCDIR?}"/solenv/bin/assemble-flatpak-desktop.sh "${PREFIXDIR?}"/share/applications/ \
 /app/share/applications/ "${1?}"

## icons/hicolor/*/apps/collaboraoffice-* ->
## icons/hicolor/*/apps/$1-*:
mkdir -p /app/share/icons
for i in "${PREFIXDIR?}"/share/icons/hicolor/*/apps/collaboraoffice-*
do
 mkdir -p \
  "$(dirname /app/share/icons/hicolor/"${i#"${PREFIXDIR?}"/share/icons/hicolor/}")"
 cp -a "$i" \
  "$(dirname /app/share/icons/hicolor/"${i#"${PREFIXDIR?}"/share/icons/hicolor/}")"/"$(basename "$i")"
 cp -a "$i" \
  "$(dirname /app/share/icons/hicolor/"${i#"${PREFIXDIR?}"/share/icons/hicolor/}")"/"${1?}"."${i##*/apps/collaboraoffice-}"
done

mkdir -p /app/share/runtime/locale
for i in $(ls /app/collaboraoffice/program/resource)
do
  lang="${i%[_@]*}"
  mkdir -p /app/share/runtime/locale/"${lang}"/resource
  mv /app/collaboraoffice/program/resource/"${i}" /app/share/runtime/locale/"${lang}"/resource
  ln -s ../../../share/runtime/locale/"${lang}"/resource/"${i}" /app/collaboraoffice/program/resource
done

# The .../share/registry/res/registry_*.xcd glob may match nothing (the
# .../share/registry/Langpack-*.xcd and .../share/registry/res/fcfg_langpack_*.xcd globs will always
# match at least .../share/registry/Langpack-en-US.xcd and
# .../share/registry/res/fcfg_langpack-en-US.xcd, respectively):
shopt -s nullglob
for i in /app/collaboraoffice/share/registry/Langpack-*.xcd /app/collaboraoffice/share/registry/res/{fcfg_langpack,registry}_*.xcd
do
  basename="$(basename "${i}" .xcd)"
  lang="${basename#Langpack-}"
  lang="${lang#fcfg_langpack_}"
  lang="${lang#registry_}"

  # ship the base app with at least one Langpack/fcfg_langpack
  if [ "${lang}" = "en-US" ]
  then
    continue
  fi

  lang="${lang%-*}"
  mkdir -p /app/share/runtime/locale/"${lang}"/registry
  mv "${i}" /app/share/runtime/locale/"${lang}"/registry
  ln -rs /app/share/runtime/locale/"${lang}"/registry/"${basename}".xcd "${i}"
done

mkdir -p /app/share/appdata
"${SRCDIR?}"/solenv/bin/assemble-flatpak-appdata.sh /app/share/appdata/ 1 "${1?}"

## see <https://github.com/flatpak/flatpak/blob/master/app/
## flatpak-builtins-build-finish.c> for further places where build-finish would
## look for data:
## cp ... /app/share/dbus-1/services/
## cp ... /app/share/gnome-shell/search-providers/
