#!/bin/bash
#cd "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd /home/speleoalex/public_html/flatnux

######################### rimozione sitemap ####################################
rm ./*sitemap*.xml
rm ./index-*.html
rm ./trovaprezzi.txt
######################### rimozione altri files  ###############################
rm ./.directory
rm ./.htaccess
rm ./fncommerce
rm ./controlcenter/sections/fnEcommerce
rm ./sections/home/section.*
rm ./sections/fncommerce
rm ./modules/fncommerce

find . -iname \*~ | xargs rm -f
find . -iname \.directory | xargs rm -f
find . -iname \*.menu | xargs rm -f
find . -iname \*.menu.xml | xargs rm -f
rm ./sections/90_doc
rm ./sections/78_CMR
######################### creazione firstinstall   #############################
rm -rf ./misc/*
touch ./misc/firstinstall
chmod 777 ./misc/firstinstall


###########################    fix dei permessi   ##############################
for i in $(find . -type f); do
	if echo "$i" | egrep -qe "\.(php|sh)$"; then
		chmod a+x $i
	else
		chmod a-x $i
	fi;
done
######################### scrittura versione ###################################
pwd=$(basename $PWD) 
oldpwd=$PWD
echo $pwd-$(date +"%Y-%m.%d") > VERSION
echo $pwd-$(date +"%Y-%m.%d") > ../FLATNUXDEVEL
echo $pwd-$(date +"%Y-%m.%d") > ../FLATNUXSTABLE
########################    creazione zip   ####################################
cd ..
if [ -d "$pwd" ]; then
	name=$pwd-$(date +"%Y-%m.%d").tar.gz
	nameminimal=$pwd-minimal-$(date +"%Y-%m.%d").tar.gz
	namezip=$pwd-$(date +"%Y-%m.%d").zip
	namezipminimal=$pwd-minimal-$(date +"%Y-%m.%d").zip
	mkdir ./fnexlude
	declare n=0
	while [ -e $namezip ]; do
		let n++
		name=$pwd-$(date +"%Y-%m.%d").$n.tar.gz
		namezip=$pwd-$(date +"%Y-%m.%d").$n.zip
		nameminimal=$pwd-minimal-$(date +"%Y-%m.%d").$n.tar.gz
		namezipminimal=$pwd-minimal-$(date +"%Y-%m.%d").$n.zip
        echo $pwd-$(date +"%Y-%m.%d").$n > FLATNUXDEVEL
        echo $pwd-$(date +"%Y-%m.%d").$n > FLATNUXSTABLE
        echo $pwd-$(date +"%Y-%m.%d").$n > flatnux/VERSION
	done
    
        #sposto esclusi
	mv ./flatnux/.svn ./fnexlude/
        #creo zip devel
	zip -r $namezip"-devel.zip" $pwd
	mkdir ./fnbuild

        #sposto devel
	mv ./flatnux/nbproject ./fnbuild/
	mv ./flatnux/.project ./fnbuild/
	mv ./flatnux/.settings ./fnbuild/
	mv ./flatnux/docs ./fnbuild/
        #moduli extra
	mv ./flatnux/modules/ApplicationRest ./fnbuild/
	mv ./flatnux/modules/DocX ./fnbuild/
	mv ./flatnux/modules/youtubechannel ./fnbuild/



	echo "PULIZIA COMPLETATA";
        #creo zip release
	zip -r $namezip $pwd -x "flatnux/.*"  flatnux/nbproject flatnux/.director\* flatnux/.project\* flatnux/.buildpath\* flatnux/\*.sh flatnux/.settings flatnux/.svn/\*

	echo "----UPDATE SVN----";
	cd /home/speleoalex/public_html/SVN-flatnux/
	svn commit 
#	svn copy ./trunk https://flatnux.svn.sourceforge.net/svnroot/flatnux/testing/$pwd-$(date +"%Y-%m.%d").$n -m $(date +"%Y-%m.%d")
	svn copy ./trunk https://flatnux.svn.sourceforge.net/svnroot/flatnux/tags/flatnux_1.0 -m flatnux_1.0

	cd /home/speleoalex/public_html/flatnux


        #ripristino esclusi
	mv ./fnexlude/.svn ./flatnux/
        #ripristino devel
	mv ./fnbuild/nbproject ./flatnux/
	mv ./fnbuild/.project ./flatnux/
	mv ./fnbuild/.settings ./flatnux/
	mv ./fnbuild/docs ./flatnux/
        #moduli extra
	mv ./fnbuild/ApplicationRest ./flatnux/modules/ 
	mv ./fnbuild/DocX ./flatnux/modules/ 
	mv ./fnbuild/youtubechannel ./flatnux/modules/ 


fi
#########################ripristino i permessi##################################
chown -R speleoalex ./flatnux/*
###############################     fine    ####################################
echo creato: $namezip

