#!/bin/sh

# **** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
#
# The contents of this file are subject to the Mozilla Public
# License Version 1.1 (the "MPL"); you may not use this file
# except in compliance with the MPL. You may obtain a copy of
# the MPL at http://www.mozilla.org/MPL/
#
# Software distributed under the MPL is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the MPL for the specific language governing
# rights and limitations under the MPL.
#
# The Original Code is Enigmail.
#
# The Initial Developer of the Original Code is Patrick Brunschwig.
# Portions created by Patrick Brunschwig <patrick@enigmail.net> are
# Copyright (C) 2004 Patrick Brunschwig. All Rights Reserved.
#
# Contributor(s):
#
# Alternatively, the contents of this file may be used under the terms of
# either the GNU General Public License Version 2 or later (the "GPL"), or
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
# in which case the provisions of the GPL or the LGPL are applicable instead
# of those above. If you wish to allow use of your version of this file only
# under the terms of either the GPL or the LGPL, and not to allow others to
# use your version of this file under the terms of the MPL, indicate your
# decision by deleting the provisions above and replace them with the notice
# and other provisions required by the GPL or the LGPL. If you do not delete
# the provisions above, a recipient may use your version of this file under
# the terms of any one of the MPL, the GPL or the LGPL.
# ***** END LICENSE BLOCK *****

#
# This script generates the Enigmail XPI
#

echo "genxpi: Generating $1 in $6"

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

xpiFile=$1
xpiVersion="$2"
osArch=$3
xpcomAbi=$4
mozApp=$5
srcDir=$7
xpiModule=$8
dllSuffix=$9
libPrefix=${10}
appTuned="${11}"
cwd=`pwd`
cd "$6"
targetDir=`pwd`
cd "$cwd"

if [ "$xpcomAbi" = "" ] ; then
  xpcomAbi="unknown"
fi
platform=${osArch}_${xpcomAbi}
# Pepare install.rdf
sed -e 's/^\(.*\)@TENFOURBIRD_TUNED@\(.*\)$/\1'${appTuned}'\2/' -e 's/<!-- targetPlatform placeholder -->/<em:targetPlatform>'${platform}'<\/em:targetPlatform>/' < ${srcDir}/package/install.rdf > ${targetDir}/install.rdf


enigmimeDll=${libPrefix}enigmime${dllSuffix}
if [ ${platform} = OS2_x86-gcc3 ]; then
  # workaround for file name length limit on OS/2
  enigDllFile=platform/${platform}/components/${enigmimeDll}
else
  enigDllFile=platform/${platform}/components/${libPrefix}enigmime-${xpcomAbi}${dllSuffix}
fi

subprocessDll=${libPrefix}subprocess${dllSuffix}
spDllFile=platform/${platform}/lib/${libPrefix}subprocess-${xpcomAbi}${dllSuffix}

# Prepare chrome.manifest
touch ${targetDir}/chrome.manifest
cp  ${targetDir}/chrome.manifest ${targetDir}/chrome.manifest.save
cat  ${srcDir}/package/chrome.manifest | \
sed  's/##ENIGMIMEDLL-PLACEHOLDER##/binary-component platform\/'${platform}'\/components\/'`basename ${enigDllFile}`' ABI='${platform}'/' \
> ${targetDir}/chrome.manifest

# Prepare languages other than en-US

if [ -s ${srcDir}/lang/current-languages.txt ]; then
  localeJar=chrome/${xpiModule}.jar

  echo '' >> ${targetDir}/chrome.manifest
  echo '# Additional languages' >> ${targetDir}/chrome.manifest

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

else
  localeJar=""
fi


cd ${srcDir}/package
cd "$targetDir"


mkdir -p platform/${platform}/components
mkdir -p platform/${platform}/lib
mkdir -p wrappers

cp ${srcDir}/util/gpg-agent-wrapper.sh wrappers

cp $cwd/ipc/src/${subprocessDll} ${spDllFile}
cp components/${enigmimeDll} ${enigDllFile}

if [ -d chrome/enigmail ]; then
  echo "Fixing translations"
  cd chrome/enigmail
  for lang in `cat ${srcDir}/lang/current-languages.txt`; do
      ${srcDir}/util/fixlang.pl locale/en-US locale/$lang > /dev/null
  done
  echo "Creating ${xpiModule}.jar file"
  zip -rD ../${xpiModule}.jar * > /dev/null
  cd ../..
fi

echo "Creating ${xpiFile} file"

zip ${xpiFile} \
    components/${xpiModule}.xpt \
    components/${xpiModule}.js \
    components/enigprefs-service.js \
    components/enigMsgCompFields.js \
    components/mimeDecrypt.js \
    components/mimeEncrypt.js \
    defaults/preferences/enigmail.js \
    modules/enigmailCommon.jsm \
    modules/pipeConsole.jsm \
    modules/keyManagement.jsm \
    modules/mimeVerify.jsm \
    modules/installGnuPG.jsm \
    modules/gpgAgentHandler.jsm \
    modules/commonFuncs.jsm \
    modules/subprocess.jsm \
    modules/subprocess_worker_win.js \
    modules/subprocess_worker_unix.js \
    wrappers/gpg-agent-wrapper.sh \
    chrome/${xpiModule}.jar \
    ${localeJar} \
    components/enigmime.xpt \
    ${enigDllFile} \
    ${spDllFile} \
    chrome.manifest \
    install.rdf

rm install.rdf chrome.manifest
mv chrome.manifest.save chrome.manifest
