#!/bin/sh

# 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 https://mozilla.org/MPL/2.0/.

#
# This script generates the Enigmail XPI
#

echo "genxpi: Generating $1 in $3"

if [ $# -lt 5 ]; then
  echo "Wrong number of parameters"
  exit 1
fi

xpiFile=$1
xpiVersion="$2"
distDir="$3"
srcDir=$4
xpiModule=$5
enableLang=$6

cd ${srcDir}
cwd=`pwd`
cd "$distDir"
targetDir=`pwd`
cd "$cwd"

cp ${srcDir}/package/install.rdf ${targetDir}/install.rdf
cp ${srcDir}/package/bootstrap.js ${targetDir}/bootstrap.js

# Prepare chrome.manifest
cat  ${srcDir}/package/chrome.manifest \
> ${targetDir}/chrome.manifest

# Prepare languages other than en-US

if [ "$enableLang" = "yes" ]; then
  if [ -s ${srcDir}/lang/current-languages.txt ]; then
    echo '' >> ${targetDir}/chrome.manifest
    echo '# Additional languages' >> ${targetDir}/chrome.manifest

    for lang in `cat ${srcDir}/lang/current-languages.txt`; do
      echo 'locale        enigmail    '$lang'       chrome/locale/'$lang'/' >> ${targetDir}/chrome.manifest
    done
  fi
fi


cd "$targetDir"

find modules -name "*.js*" | LC_ALL=C sort > modules/all-modules.txt

echo "Creating ${xpiFile} file"

zip -9 --must-match\
    ../${xpiFile} \
    modules/preferences/defaultPrefs.js \
    modules/addrbook.jsm \
    modules/amPrefsService.jsm \
    modules/app.jsm \
    modules/armor.jsm \
    modules/attachment.jsm \
    modules/autocrypt.jsm \
    modules/buildDate.jsm \
    modules/card.jsm \
    modules/clipboard.jsm \
    modules/commandLine.jsm \
    modules/configure.jsm \
    modules/constants.jsm \
    modules/data.jsm \
    modules/decryption.jsm \
    modules/persistentCrypto.jsm \
    modules/dialog.jsm \
    modules/encryption.jsm \
    modules/core.jsm \
    modules/configBackup.jsm \
    modules/errorHandling.jsm \
    modules/funcs.jsm \
    modules/gpgAgent.jsm \
    modules/protocolHandler.jsm \
    modules/events.jsm \
    modules/execution.jsm \
    modules/files.jsm \
    modules/filters.jsm \
    modules/filtersWrapper.jsm \
    modules/fixExchangeMsg.jsm \
    modules/glodaMime.jsm \
    modules/glodaUtils.jsm \
    modules/gpg.jsm \
    modules/hash.jsm \
    modules/httpProxy.jsm \
    modules/installGnuPG.jsm \
    modules/installPep.jsm \
    modules/key.jsm \
    modules/keyEditor.jsm \
    modules/keyRing.jsm \
    modules/keyUsability.jsm \
    modules/keyRefreshService.jsm \
    modules/keyserver.jsm \
    modules/keyserverUris.jsm \
    modules/lazy.jsm \
    modules/locale.jsm \
    modules/localizeHtml.jsm \
    modules/log.jsm \
    modules/mime.jsm \
    modules/mimeDecrypt.jsm \
    modules/mimeEncrypt.jsm \
    modules/mimeVerify.jsm \
    modules/msgCompFields.jsm \
    modules/msgRead.jsm \
    modules/os.jsm \
    modules/openpgp.jsm \
    modules/overlays.jsm \
    modules/passwordCheck.jsm \
    modules/passwords.jsm \
    modules/pEp.jsm \
    modules/pEpAdapter.jsm \
    modules/pEpDecrypt.jsm \
    modules/pEpFilter.jsm \
    modules/pEpListener.jsm \
    modules/pEpKeySync.jsm \
    modules/pgpmimeHandler.jsm \
    modules/pipeConsole.jsm \
    modules/prefs.jsm \
    modules/rng.jsm \
    modules/rules.jsm \
    modules/send.jsm \
    modules/singletons.jsm \
    modules/socks5Proxy.jsm \
    modules/stdlib/compose.jsm \
    modules/stdlib/misc.jsm \
    modules/stdlib/msgHdrUtils.jsm \
    modules/stdlib/openpgp-lib.js \
    modules/stdlib/openpgp.worker.min.js \
    modules/stdlib.jsm \
    modules/streams.jsm \
    modules/subprocess.jsm \
    modules/enigmailprocess_shared_unix.js \
    modules/enigmailprocess_worker_common.js \
    modules/enigmailprocess_common.jsm \
    modules/enigmailprocess_shared_win.js \
    modules/enigmailprocess_worker_unix.js \
    modules/enigmailprocess_main.jsm \
    modules/enigmailprocess_unix.jsm \
    modules/enigmailprocess_worker_win.js \
    modules/enigmailprocess_shared.js \
    modules/enigmailprocess_win.jsm \
    modules/system.jsm \
    modules/time.jsm \
    modules/timer.jsm \
    modules/tor.jsm \
    modules/trust.jsm \
    modules/uris.jsm \
    modules/verify.jsm \
    modules/versioning.jsm \
    modules/webKey.jsm \
    modules/wkdLookup.jsm \
    modules/windows.jsm \
    modules/wksMimeHandler.jsm \
    modules/zbase32.jsm \
    modules/all-modules.txt \
    chrome/content/*.* \
    chrome/skin/aero/*.* \
    chrome/skin/modern/*.* \
    chrome/skin/tb-mac/*.* \
    chrome/skin/classic-seamonkey/*.* \
    chrome/skin/tb-linux/*.* \
    chrome/skin/tb-win-xp/*.* \
    chrome/locale/*/*.* \
    chrome/locale/*/help/*.* \
    bootstrap.js \
    chrome.manifest \
    install.rdf
