#!/bin/bash

# 14/04/2023
# Shell script used (under Linux) to create the 'TexMaths-XXX.oxt' package
# The created package can be used on Linux, MacOS X and Windows
# The package version 'XXX' is obtained from the directory name 'TexMaths-XXX' and is used
# to update the description.xml file
# If the package name 'TexMaths-XXX.oxt' already exists, it is first copied to 'TexMaths-XXX.oxt.bak'


# Update po files
update_po(){
	
echo
echo Updating po files...
cd po
./xgettextbas ../TexMaths/*.xba > TexMaths.pot 2> /dev/null
for File in `ls *.po 2>/dev/null`
do
  	echo $File
	msgmerge -N $File TexMaths.pot > $File.tmp
	mv $File.tmp $File
done
cd ..
}


# Create package
make_package(){


cat description.xml \
	| sed s/"<version value=\".*\""/"<version value=\"${version}\""/ \
	>  tmp.xml

mv tmp.xml description.xml

href="http://downloads.sourceforge.net/texmaths/TexMaths-${version}.oxt"

echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<description xmlns=\"http://openoffice.org/extensions/update/2006\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">
  <identifier value=\"org.roland65.texmaths\"/>
  <display-name>
    <name>TexMaths</name>
  </display-name>
  <version value=\"${version}\"/>
  <update-download>
   <src xlink:href=\"${href}\"/>
  </update-download>
</description>" > ../TexMaths.update.xml

if [ -f ../TexMaths-${version}.oxt ]; then
    mv ../TexMaths-${version}.oxt ../TexMaths-${version}.oxt.bak
fi


zip -r ../TexMaths-${version}.oxt \
	 AddonRegistry.{xcs,xcu} \
	 Addons.xcu \
	 ChangeLog \
	 makepkg \
	 description.xml \
	 README \
	 META-INF/manifest.xml \
	 Office/UI/*.xcu \
	 icons/*.{bmp,png,svg} \
	 icons/symbols-black-pre75/*.svg \
	 icons/symbols-white-pre75/*.svg \
	 icons/symbols-black/*.svg \
	 icons/symbols-white/*.svg \
	 fonts/*.ttf \
	 scripts/*.bat \
	 TexMaths/*.{xba,xdl,xlb} \
	 license.txt \
	 help/* \
	 po/*
}


# Main part

# Update po files
update_po

# Get package version from directory name
currentdir=`basename "$PWD"`
version=${currentdir#*-}

echo
echo Making package release version $version ...
echo

# Make package
make_package

# Display message
echo
echo Package TexMaths-$version.oxt created!
echo

exit 0

