#!/bin/sh
# The application archive has to be assembled: both omni.ja files present, the
# runtime rewritten by modify_omni, and the resource trees of the zotero-*
# packages reachable through the links next to the archive.
set -eu

LIB=/usr/lib/zotero

for f in omni.ja app/omni.ja app/application.ini zotero-bin; do
	[ -e "$LIB/$f" ] || { echo "missing: $LIB/$f" >&2; exit 1; }
done

for f in reader document-worker pdfjs note-editor; do
	[ -d "$LIB/app/resource/$f" ] || { echo "missing: app/resource/$f" >&2; exit 1; }
done

for f in classnames.js csl-validator-wasm jspath.js pako.js prop-types.js \
	react-autosuggest.js react-dom-server.js react-dom.js react.js \
	ace/ace.js ace/mode-javascript.js ace/worker-javascript.js \
	vs/loader.js vs/editor/editor.main.js vs/editor/editor.main.css \
	vs/language/json/jsonWorker.js vs/language/typescript/tsMode.js; do
	[ -L "$LIB/app/resource/$f" ] || { echo "not a link: app/resource/$f" >&2; exit 1; }
	[ -e "$LIB/app/resource/$f" ] || { echo "dangling: app/resource/$f" >&2; exit 1; }
done

# The TypeScript worker carries upstream's own patch, so it is a real file.
[ -f "$LIB/app/resource/vs/language/typescript/tsWorker.js" ] \
	&& [ ! -L "$LIB/app/resource/vs/language/typescript/tsWorker.js" ] \
	|| { echo "tsWorker.js should be a plain file" >&2; exit 1; }

for f in libxul.so browser defaults platform.ini dependentlibs.list; do
	[ -e "$LIB/$f" ] || { echo "broken runtime link: $f" >&2; exit 1; }
done

python3 - <<'PY'
import zipfile

gre = zipfile.ZipFile('/usr/lib/zotero/omni.ja')
app = zipfile.ZipFile('/usr/lib/zotero/app/omni.ja')

const = gre.read('modules/AppConstants.sys.mjs').decode()
assert 'BROWSER_CHROME_URL: "chrome://zotero/content/zoteroPane.xhtml"' in const, \
    'modify_omni did not rewrite BROWSER_CHROME_URL'
assert 'MOZ_APP_BASENAME: "Zotero"' in const, 'MOZ_APP_BASENAME not rewritten'

names = set(app.namelist())
for want in ('chrome/content/zotero/zoteroPane.xhtml',
             'chrome.manifest',
             'chrome/en-US/locale/branding/brand.dtd',
             'defaults/preferences/zotero.js',
             'translators.json',
             'deleted.txt',
             'localization/zh-CN/zotero.ftl',
             'localization/en-US/browser/menubar.ftl'):
    assert want in names, 'app/omni.ja is missing ' + want

for unwanted in ('localization/zh-CN/zotero.ftl',
                 'localization/en-US/browser/menubar.ftl'):
    assert unwanted not in set(gre.namelist()), \
        'omni.ja still carries ' + unwanted

for unwanted in ('defaults/preferences/firefox.js',
                 'chrome/browser/content/browser/browser.xhtml',
                 'translators/deleted.txt',
                 'styles/update',
                 'version'):
    assert unwanted not in names, 'app/omni.ja still ships ' + unwanted

bch = app.read('modules/BrowserContentHandler.sys.mjs').decode()
assert 'ZoteroDebugText' in bch, 'command line handler was not installed'

for prefix in ('resource/reader/', 'resource/note-editor/',
               'resource/document-worker/', 'resource/pdfjs/'):
    assert not any(n.startswith(prefix) for n in names), \
        'app/omni.ja still carries ' + prefix

res = [l for l in app.read('chrome.manifest').decode().splitlines()
       if l.startswith('resource zotero')]
assert res and res[0].split()[-1] == 'file:///usr/lib/zotero/app/resource/', \
    'resource://zotero/ does not point at app/resource/'
PY

sh -n /usr/bin/zotero
grep -q 'exec /usr/lib/zotero/zotero-bin' /usr/bin/zotero
grep -q '^Exec=zotero -url %U$' /usr/share/applications/zotero.desktop
