#!/bin/bash -e

#biomaj version
export BMAJVERSION="1.1.0"

svn export svn://scm.gforge.inria.fr/svn/biomaj/biomaj_deb
rmdir biomaj_deb/usr/share/biomaj/apache-tomcat-6.0.24

svn export svn://scm.gforge.inria.fr/svn/biomaj/BioMAJ-$BMAJVERSION
chmod 755 BioMAJ-$BMAJVERSION/bin/*
cp -R BioMAJ-$BMAJVERSION/bin/* biomaj_deb/usr/share/biomaj/bin/
mkdir -p biomaj_deb/usr/share/doc/biomaj
cp -R BioMAJ-$BMAJVERSION/doc/UsersGuide_BioMAJ_EN.pdf biomaj_deb/usr/share/doc/biomaj/
cp -R BioMAJ-$BMAJVERSION/doc/UsersGuide_BioMAJ_FR.pdf biomaj_deb/usr/share/doc/biomaj/
cp -R BioMAJ-$BMAJVERSION/doc/UsersGuide_BioMAJ_EN.odt biomaj_deb/usr/share/doc/biomaj/
cp -R BioMAJ-$BMAJVERSION/doc/UsersGuide_BioMAJ_FR.odt biomaj_deb/usr/share/doc/biomaj/

#cp BioMAJ-$BMAJVERSION/lib/commons-cli-2.0-SNAPSHOT.jar biomaj_deb/usr/share/biomaj/lib/

svn export http://svn.apache.org/repos/asf/commons/sandbox/cli2/trunk biomaj_deb/usr/share/biomaj/lib/vendor/commons-cli2
chmod 644 biomaj_deb/usr/share/biomaj/lib/vendor/commons-cli2/src/site/resources/images/1x1.gif

if [ -e biomaj_deb/usr/share/biomaj/lib/biomaj.jar ]
then
rm biomaj_deb/usr/share/biomaj/lib/biomaj.jar
fi
echo "Copy files..."
cp -R BioMAJ-$BMAJVERSION/src/* biomaj_deb/usr/share/biomaj/src/
cp -R BioMAJ-$BMAJVERSION/sql/* biomaj_deb/usr/share/biomaj/sql/
cp -R BioMAJ-$BMAJVERSION/workflows/* biomaj_deb/usr/share/biomaj/workflows/
cp -R BioMAJ-$BMAJVERSION/xslt/* biomaj_deb/usr/share/biomaj/xslt/
cp BioMAJ-$BMAJVERSION/*.* biomaj_deb/usr/share/biomaj/
cp BioMAJ-$BMAJVERSION/License* biomaj_deb/usr/share/biomaj/
cp BioMAJ-$BMAJVERSION/MANIFEST biomaj_deb/usr/share/biomaj/
cp BioMAJ-$BMAJVERSION/README biomaj_deb/usr/share/biomaj/
cp BioMAJ-$BMAJVERSION/RELEASE biomaj_deb/usr/share/biomaj/
cp BioMAJ-$BMAJVERSION/TODO biomaj_deb/usr/share/biomaj/

echo "Delete unnecessary files"

rm biomaj_deb/usr/share/biomaj/general.conf
rm -rf biomaj_deb/usr/share/biomaj/xslt/css
rm -rf biomaj_deb/usr/share/biomaj/doc

echo "Update configuration"
cp -R BioMAJ-$BMAJVERSION/conf/* biomaj_deb/etc/biomaj/
sed 's/data.dir=/data.dir=\/var\/lib\/biomaj\//' biomaj_deb/etc/biomaj/db_properties/global.properties > biomaj_deb/etc/biomaj/db_properties/global.properties.tmp
mv biomaj_deb/etc/biomaj/db_properties/global.properties.tmp biomaj_deb/etc/biomaj/db_properties/global.properties
mv biomaj_deb/etc/biomaj/process/* biomaj_deb/usr/share/biomaj/conf/process/
rmdir biomaj_deb/etc/biomaj/process

echo "Updating file rights"
#chmod 644 biomaj_deb/usr/share/biomaj/lib/*.jar
chmod 644 biomaj_deb/usr/share/biomaj/src/org/inria/biomaj/sql/*.java
chmod 644 biomaj_deb/usr/share/biomaj/src/org/inria/biomaj/internal/ant/task/net/test/*.java
chmod 644 biomaj_deb/usr/share/biomaj/src/org/inria/biomaj/internal/ant/task/net/*.java
chmod 644 biomaj_deb/usr/share/biomaj/sql/*.sql
chmod 644 biomaj_deb/usr/share/biomaj/bin/*.java

#mv  biomaj_deb/DEBIAN biomaj_deb/debian
#chmod 644 biomaj_deb/debian/control
#rm biomaj_deb/debian/conffiles
#rm biomaj_deb/debian/fichier

echo "Update debian build file"
mv biomaj_deb/usr/share/biomaj/build.xml_deb biomaj_deb/usr/share/biomaj/build.xml

echo "Removing temporary directories"
rm -rf BioMAJ-$BMAJVERSION

mv biomaj_deb biomaj_$BMAJVERSION


#cd biomaj-1.1.0
GZIP="--best --no-name" tar --exclude biomaj_$BMAJVERSION/DEBIAN -cvzf  biomaj_$BMAJVERSION.orig.tar.gz biomaj_$BMAJVERSION

rm -rf biomaj_$BMAJVERSION

#dpkg-buildpackage
#cd ..
#lintian -i -I --show-overrides  biomaj_1.1.0-1_amd64.changes
