#!/bin/bash
## this script generates executables for jiwigo
## to use it, you have to download the "travail" directory
## here : http://www.le-guevel.com/jiwigo/downloads/travail.tar.gz
## you just have to change travail_path to indicate the path to "travail"
## and "project_path" to the path of the project.
## you have to run the mvn jar command before launching this script
## the mvn command is written in the install file of the project : install.txt
## all the generated files will be moved to the "generation" directory.
travail_path='/home/mael/Documents/Jiwigo/travail'
project_path='/home/mael/workspace/jiwigo'
target_path=$project_path/target

###############################################################################################
#####################		Construction du deb 		 ##############################
###############################################################################################

#calcul de la taille et de la version, pour compléter le fichier control de debian
taille=`du -cs jiwigo`
taille=`echo $taille | cut -d ' ' -f 1`
version=`find $target_path -type f -name \*with-dependencies.jar | xargs -I file echo file | cut -d '-' -f 2`
cp $travail_path/control $travail_path/temp
echo Version: $version >> $travail_path/temp
echo Installed-Size: $taille >> $travail_path/temp
rm $travail_path/jiwigo/DEBIAN/control
mv temp $travail_path/jiwigo/DEBIAN/control

#copie des sources
rm -rf $travail_path/jiwigo/usr/share/jiwigo/src/src-jiwigo/*
cp -r $project_path/src/* $travail_path/jiwigo/usr/share/jiwigo/src/src-jiwigo/

#copie du jar
rm $travail_path/jiwigo/usr/share/jiwigo/jiwigo.jar
find $target_path -type f -name \*with-dependencies.jar -exec cp {} $travail_path/jiwigo/usr/share/jiwigo/jiwigo.jar \;

#suppression des fichiers temporaires
find "$travail_path" | egrep "~$" | perl -n -e 'system("rm $_");'

#changement du répertoire
cd "$travail_path"

#construction du deb
dpkg-deb --build jiwigo

rm "$travail_path/depot/jiwigo.deb"
rm "$travail_path/depot/Packages.gz"
#déplacement du deb
mv -f "$travail_path/jiwigo.deb" "$travail_path/depot/jiwigo.deb"

#construction de Packages.gz
cd "$travail_path/depot"
dpkg-scanpackages . /dev/null | gzip - > Packages.gz


###############################################################################################
#####################		Construction du exe 		 ##############################
###############################################################################################

$travail_path/launch4j/launch4j $travail_path/launch4j.xml

###############################################################################################
#####################		du dossier de generation	 ##############################
###############################################################################################

rm $travail_path/generation/*
cp $travail_path/jiwigo.exe $travail_path/generation/
cp $travail_path/depot/jiwigo.deb $travail_path/generation/
cp $travail_path/jiwigo/usr/share/jiwigo/jiwigo.jar $travail_path/generation/
tar -zcf $travail_path/generation/jiwigo-src.tar.gz $project_path/src/*