#!/bin/bash
## this script generates executables for jiwigo
## to use it, you have to download the "work" directory
## here : http://www.le-guevel.com/jiwigo/downloads/work.tar.gz
## you just have to change work_path to indicate the path to "work"
## 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 writter in the install file of the project : install/install.txt
## all the generated files will be moved to the "generation" directory.
work_path='/home/mael/Documents/Jiwigo/travail'
project_path='/home/mael/workspace/jiwigo'
target_path=$project_path/target

###############################################################################################
#####################		building deb 		 	 ##############################
###############################################################################################

#Calculate size and version to complete debian's control file
size=`du -cs jiwigo`
size=`echo $size | cut -d ' ' -f 1`
version=`find $target_path -type f -name \*with-dependencies.jar | xargs -I file echo file | cut -d '-' -f 2`
cp $work_path/control $work_path/temp
echo Version: $version >> $work_path/temp
echo Installed-Size: $size >> $work_path/temp
rm $work_path/jiwigo/DEBIAN/control
mv temp $work_path/jiwigo/DEBIAN/control

#copies sources
rm -rf $work_path/jiwigo/usr/share/jiwigo/src/src-jiwigo/*
cp -r $project_path/src/* $work_path/jiwigo/usr/share/jiwigo/src/src-jiwigo/

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

#delete temporary files
find "$work_path" | egrep "~$" | perl -n -e 'system("rm $_");'

#changes current directory
cd "$work_path"

#makes deb
dpkg-deb --build jiwigo

rm "$work_path/depot/jiwigo.deb"
rm "$work_path/depot/Packages.gz"
#moves deb
mv -f "$work_path/jiwigo.deb" "$work_path/depot/jiwigo.deb"

#makes Packages.gz
cd "$work_path/depot"
dpkg-scanpackages . /dev/null | gzip - > Packages.gz


###############################################################################################
#####################		Building    exe 		 ##############################
###############################################################################################

$work_path/launch4j/launch4j $work_path/launch4j.xml

###############################################################################################
#####################		completes generation folder	 ##############################
###############################################################################################

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