if [ -e $1 ]
then
	if [ "$1" == "" ]
	then
		echo "you should provide following argument: output folder name"
   		exit
	fi
	
	if [ -r  $1/setup-MicMac-macosx-qt${qt_version}.dmg ]
	then
		echo "removing old dmg file"
		rm $1/setup-MicMac-macosx-qt${qt_version}.dmg
	fi
	
	echo "creation d'un package MicMac dans $1"
	#source ~/.profile
	
	echo "-- creation des sous-dossiers du bundle"
	mkdir $1/MicMac.app
	mkdir $1/MicMac.app/Contents
	mkdir $1/MicMac.app/Contents/Resources
	mkdir $1/MicMac.app/Contents/MacOS
	mkdir $1/MicMac.app/Contents/MacOS/bin
	mkdir $1/MicMac.app/Contents/MacOS/data
	mkdir $1/MicMac.app/Contents/MacOS/include
	mkdir $1/MicMac.app/Contents/MacOS/include/XML_GEN
	mkdir $1/MicMac.app/Contents/MacOS/include/XML_MicMac
	mkdir $1/MicMac.app/Contents/MacOS/include/qt
	mkdir $1/MicMac.app/Contents/MacOS/include/qt/translations
	mkdir $1/MicMac.app/Contents/Frameworks
	mkdir $1/MicMac.app/Contents/Libraries
	mkdir $1/MicMac.app/Contents/Libraries/PlugIns

	echo "-- copie de l'icone de l'app"
	cp ../data/ico/micmac.icns $1/MicMac.app/Contents/Resources

	echo "-- creation du dictionnaire du bundle"
	#echo $PkgInfoContents >$1/MicMac.app/Contents/PkgInfo
	
	plistfile=$1/MicMac.app/Contents/Info.plist
	echo '<?xml version="1.0" encoding="UTF-8"?>' >$plistfile
	echo '<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">' >>$plistfile
	echo '<plist version="1.0">' >>$plistfile
	echo '<dict>' >>$plistfile
	echo '  <key>CFBundleDevelopmentRegion</key>' >>$plistfile
	echo '  <string>France</string>' >>$plistfile
	echo '  <key>CFBundleExecutable</key>' >>$plistfile
	echo '  <string>mm3d</string>' >>$plistfile
	echo '  <key>CFBundleIconFile</key>' >>$plistfile
	echo '  <string>micmac.icns</string>' >>$plistfile
	echo '  <key>CFBundleIdentifier</key>' >>$plistfile
	echo '  <string>org.MicMac.MicMac</string>' >>$plistfile
	echo '  <key>CFBundleInfoDictionaryVersion</key>' >>$plistfile
	echo '  <string>6.0</string>' >>$plistfile
	echo '  <key>CFBundleName</key>' >>$plistfile
	echo '  <string>MicMac-1.0.0</string>' >>$plistfile
	echo '  <key>CFBundleVersion</key>' >>$plistfile
	echo '  <string>1.1</string>' >>$plistfile
	echo '  <key>CFBundleGetInfoString</key>' >>$plistfile
	echo '  <string>version 1.0.0, Copyright ©2014 IGN - Service des Développements. Tous droits réservés. </string>' >>$plistfile
	echo '  <key>CFBundlePackageType</key>' >>$plistfile
	echo '  <string>APPL</string>' >>$plistfile
 	echo '  <key>LSEnvironment</key>' >>$plistfile
   	echo '   <dict>' >>$plistfile
        echo '  	<key>IGN_DATA</key>' >>$plistfile
        echo '  	<string>Data/</string>' >>$plistfile
       	echo '   	<key>PROJ_LIB</key>' >>$plistfile
       	echo '  	<string>Data/geodesy/nad</string>' >>$plistfile
   	echo '   </dict>' >>$plistfile
	echo '   </dict>' >>$plistfile
   	echo '   </plist>' >>$plistfile

	echo "-- copie des executables de l'application"
	cp ../bin/* 						$1/MicMac.app/Contents/MacOS/bin
	cp ../bin/mm3d 						$1/MicMac.app/Contents/MacOS/
	
	#echo "-- copie des librairies dynamiques (plugins Qt, Pq, odbc)"

	#### partie a rendre generique… ####
	#cp ${Pq_DIR}/lib/libpq.5.dylib $1/MicMac.app/Contents/Libraries
	#cp /usr/lib/libiodbc.2.dylib $1/MicMac.app/Contents/Libraries
	####				####

	echo "-- copie des frameworks QT"
	cp -Rn ${QT_QTCONCURRENT_LIBRARY}  	$1/MicMac.app/Contents/Frameworks
	cp -Rn ${QT_QTCORE_LIBRARY}  		$1/MicMac.app/Contents/Frameworks
	cp -Rn ${QT_QTGUI_LIBRARY} 		$1/MicMac.app/Contents/Frameworks
	cp -Rn ${QT_QTOPENGL_LIBRARY}  		$1/MicMac.app/Contents/Frameworks
	cp -Rn ${QT_QTWIDGETS_LIBRARY}  	$1/MicMac.app/Contents/Frameworks
	cp -Rn ${QT_QTXML_LIBRARY}  		$1/MicMac.app/Contents/Frameworks
	
	if [ "${qt_version}" == "5" ]
	then
		QT_DIR=${QT5_ROOT_DIR}
		mkdir $1/MicMac.app/Contents/Libraries/PlugIns/platforms
		#cp -Rn ${QT_COCOA_PLUGIN}  		$1/MicMac.app/Contents/Libraries/PlugIns/platforms
		cp -Rn ${CMAKE_PREFIX_PATH}/plugins/${QT_COCOA_PLUGIN}  		$1/MicMac.app/Contents/Libraries/PlugIns/platforms
	elif [ "${qt_version}" == "4" ]
	then
		QT_DIR=${QT_LIBRARY_DIR}
	fi
	
	echo "-- suppression des versions debug des frameworks Qt"
	rm 	$1/MicMac.app/Contents/Frameworks/QtConcurrent.framework/Versions/${qt_version}/QtConcurrent_debug
	rm 	$1/MicMac.app/Contents/Frameworks/QtCore.framework/Versions/${qt_version}/QtCore_debug
	rm 	$1/MicMac.app/Contents/Frameworks/QtGui.framework/Versions/${qt_version}/QtGui_debug
	rm 	$1/MicMac.app/Contents/Frameworks/QtOpenGL.framework/Versions/${qt_version}/QtOpenGL_debug
	rm 	$1/MicMac.app/Contents/Frameworks/QtWidgets.framework/Versions/${qt_version}/QtWidgets_debug
	rm 	$1/MicMac.app/Contents/Frameworks/QtXml.framework/Versions/${qt_version}/QtXml_debug
	

	ALIAS_FRAMEWORKS="../../Frameworks"
	
	echo "-- passage des librairies QT en chemin relatif (MicMac)"
	#cf: http://qt-project.org/doc/qt-4.8/deployment-mac.html

	install_name_tool -id @executable_path/$ALIAS_FRAMEWORKS/QtConcurrent.framework/Versions/${qt_version}/QtConcurrent      	$1/MicMac.app/Contents/Frameworks/QtConcurrent.framework/Versions/${qt_version}/QtConcurrent
	install_name_tool -id @executable_path/$ALIAS_FRAMEWORKS/QtCore.framework/Versions/${qt_version}/QtCore      			$1/MicMac.app/Contents/Frameworks/QtCore.framework/Versions/${qt_version}/QtCore
	install_name_tool -id @executable_path/$ALIAS_FRAMEWORKS/QtGui.framework/Versions/${qt_version}/QtGui      			$1/MicMac.app/Contents/Frameworks/QtGui.framework/Versions/${qt_version}/QtGui
	install_name_tool -id @executable_path/$ALIAS_FRAMEWORKS/QtOpenGL.framework/Versions/${qt_version}/QtOpenGL   			$1/MicMac.app/Contents/Frameworks/QtOpenGL.framework/Versions/${qt_version}/QtOpenGL
	install_name_tool -id @executable_path/$ALIAS_FRAMEWORKS/QtWidgets.framework/Versions/${qt_version}/QtWidgets   		$1/MicMac.app/Contents/Frameworks/QtWidgets.framework/Versions/${qt_version}/QtWidgets
	install_name_tool -id @executable_path/$ALIAS_FRAMEWORKS/QtXml.framework/Versions/${qt_version}/QtXml   			$1/MicMac.app/Contents/Frameworks/QtXml.framework/Versions/${qt_version}/QtXml


	

	install_name_tool -change $QT_DIR/QtCore.framework/Versions/${qt_version}/QtCore   @executable_path/$ALIAS_FRAMEWORKS/QtCore.framework/Versions/${qt_version}/QtCore 	$1/MicMac.app/Contents/Frameworks/QtGui.framework/Versions/${qt_version}/QtGui

	install_name_tool -change $QT_DIR/QtCore.framework/Versions/${qt_version}/QtCore   @executable_path/$ALIAS_FRAMEWORKS/QtCore.framework/Versions/${qt_version}/QtCore 	$1/MicMac.app/Contents/Frameworks/QtXml.framework/Versions/${qt_version}/QtXml

	install_name_tool -change $QT_DIR/QtGui.framework/Versions/${qt_version}/QtGui   @executable_path/$ALIAS_FRAMEWORKS/QtGui.framework/Versions/${qt_version}/QtGui 	$1/MicMac.app/Contents/Frameworks/QtWidgets.framework/Versions/${qt_version}/QtWidgets
	install_name_tool -change $QT_DIR/QtCore.framework/Versions/${qt_version}/QtCore   @executable_path/$ALIAS_FRAMEWORKS/QtCore.framework/Versions/${qt_version}/QtCore 	$1/MicMac.app/Contents/Frameworks/QtWidgets.framework/Versions/${qt_version}/QtWidgets

	install_name_tool -change $QT_DIR/QtCore.framework/Versions/${qt_version}/QtCore   @executable_path/$ALIAS_FRAMEWORKS/QtCore.framework/Versions/${qt_version}/QtCore 	$1/MicMac.app/Contents/Frameworks/QtConcurrent.framework/Versions/${qt_version}/QtConcurrent

	install_name_tool -change $QT_DIR/QtCore.framework/Versions/${qt_version}/QtCore   	@executable_path/$ALIAS_FRAMEWORKS/QtCore.framework/Versions/${qt_version}/QtCore 		$1/MicMac.app/Contents/Frameworks/QtOpenGL.framework/Versions/${qt_version}/QtOpenGL
	install_name_tool -change $QT_DIR/QtWidgets.framework/Versions/${qt_version}/QtWidgets  @executable_path/$ALIAS_FRAMEWORKS/QtWidgets.framework/Versions/${qt_version}/QtWidgets 	$1/MicMac.app/Contents/Frameworks/QtOpenGL.framework/Versions/${qt_version}/QtOpenGL
	install_name_tool -change $QT_DIR/QtGui.framework/Versions/${qt_version}/QtGui		@executable_path/$ALIAS_FRAMEWORKS/QtGui.framework/Versions/${qt_version}/QtGui			$1/MicMac.app/Contents/Frameworks/QtOpenGL.framework/Versions/${qt_version}/QtOpenGL

	install_name_tool -change $QT_DIR/QtCore.framework/Versions/${qt_version}/QtCore   			@executable_path/$ALIAS_FRAMEWORKS/QtCore.framework/Versions/${qt_version}/QtCore			$1/MicMac.app/Contents/MacOS/bin/mm3d
	install_name_tool -change $QT_DIR/QtGui.framework/Versions/${qt_version}/QtGui    			@executable_path/$ALIAS_FRAMEWORKS/QtGui.framework/Versions/${qt_version}/QtGui			$1/MicMac.app/Contents/MacOS/bin/mm3d
	install_name_tool -change $QT_DIR/QtConcurrent.framework/Versions/${qt_version}/QtConcurrent  	 	@executable_path/$ALIAS_FRAMEWORKS/QtConcurrent.framework/Versions/${qt_version}/QtConcurrent	$1/MicMac.app/Contents/MacOS/bin/mm3d
	install_name_tool -change $QT_DIR/QtOpenGL.framework/Versions/${qt_version}/QtOpenGL  			@executable_path/$ALIAS_FRAMEWORKS/QtOpenGL.framework/Versions/${qt_version}/QtOpenGL		$1/MicMac.app/Contents/MacOS/bin/mm3d
	install_name_tool -change $QT_DIR/QtWidgets.framework/Versions/${qt_version}/QtWidgets   		@executable_path/$ALIAS_FRAMEWORKS/QtWidgets.framework/Versions/${qt_version}/QtWidgets		$1/MicMac.app/Contents/MacOS/bin/mm3d
	install_name_tool -change $QT_DIR/QtXml.framework/Versions/${qt_version}/QtXml   			@executable_path/$ALIAS_FRAMEWORKS/QtXml.framework/Versions/${qt_version}/QtXml			$1/MicMac.app/Contents/MacOS/bin/mm3d

	install_name_tool -change $QT_DIR/QtCore.framework/Versions/${qt_version}/QtCore   			@executable_path/$ALIAS_FRAMEWORKS/QtCore.framework/Versions/${qt_version}/QtCore		$1/MicMac.app/Contents/MacOS/bin/SaisieQT
	install_name_tool -change $QT_DIR/QtGui.framework/Versions/${qt_version}/QtGui    			@executable_path/$ALIAS_FRAMEWORKS/QtGui.framework/Versions/${qt_version}/QtGui			$1/MicMac.app/Contents/MacOS/bin/SaisieQT
	install_name_tool -change $QT_DIR/QtConcurrent.framework/Versions/${qt_version}/QtConcurrent  	 	@executable_path/$ALIAS_FRAMEWORKS/QtConcurrent.framework/Versions/${qt_version}/QtConcurrent	$1/MicMac.app/Contents/MacOS/bin/SaisieQT
	install_name_tool -change $QT_DIR/QtOpenGL.framework/Versions/${qt_version}/QtOpenGL  			@executable_path/$ALIAS_FRAMEWORKS/QtOpenGL.framework/Versions/${qt_version}/QtOpenGL		$1/MicMac.app/Contents/MacOS/bin/SaisieQT
	install_name_tool -change $QT_DIR/QtWidgets.framework/Versions/${qt_version}/QtWidgets   		@executable_path/$ALIAS_FRAMEWORKS/QtWidgets.framework/Versions/${qt_version}/QtWidgets		$1/MicMac.app/Contents/MacOS/bin/SaisieQT
	install_name_tool -change $QT_DIR/QtXml.framework/Versions/${qt_version}/QtXml   			@executable_path/$ALIAS_FRAMEWORKS/QtXml.framework/Versions/${qt_version}/QtXml			$1/MicMac.app/Contents/MacOS/bin/SaisieQT

	#install_name_tool -id @executable_path/../Libraries/plugins/sqldrivers/libqsqlpsql.dylib  	$1/MicMac.app/Contents/Libraries/plugins/sqldrivers/libqsqlpsql.dylib
	#install_name_tool -id @executable_path/../Libraries/plugins/sqldrivers/libqsqlite.dylib    	$1/MicMac.app/Contents/Libraries/plugins/sqldrivers/libqsqlite.dylib
	#install_name_tool -id @executable_path/../Libraries/plugins/sqldrivers/libqsqlodbc.dylib   	$1/MicMac.app/Contents/Libraries/plugins/sqldrivers/libqsqlodbc.dylib
	#install_name_tool -id @executable_path/../Libraries/libpq.5.dylib   				$1/MicMac.app/Contents/Libraries/libpq.5.dylib
	#install_name_tool -id @executable_path/../Libraries/libiodbc.2.dylib 				$1/MicMac.app/Contents/Libraries/libiodbc.2.dylib



	echo "-- copie des ressources"
	cp -R ../data/* $1/MicMac.app/Contents/MacOS/data/
	
	### copie des templates
	cp ../include/XML_GEN/* $1/MicMac.app/Contents/MacOS/include/XML_GEN/
	cp -R ../include/XML_MicMac/* $1/MicMac.app/Contents/MacOS/include/XML_MicMac/
	
	### copie des ressources qt
	cp ../include/qt/translations/* $1/MicMac.app/Contents/MacOS/include/qt/translations/
	cp ../include/qt/style.qss $1/MicMac.app/Contents/MacOS/include/qt/

	echo "-- creation du lien symbolique vers /Applications"
	ln -s /Applications $1/MicMac.app/.

	echo "-- creation de l'installer MicMac dans $1"
	mkdir $1/MicMac
	mv $1/MicMac.app $1/MicMac
	ln -s /Applications $1/MicMac/.
	hdiutil create -srcfolder $1/MicMac -volname "MicMac" -fs HFS+ -fsargs "-c c=64,a=16,e=16" -format UDZO  $1/MicMac_installer.dmg
	rm -rf $1/MicMac
	rm -rf $1/MicMac

	echo "fin de creation de MicMac dans $1"
	
	mv $1/MicMac_installer.dmg $1/setup-MicMac-qt${qt_version}.dmg
	echo "-- DMG cree: $1/setup-MicMac-qt${qt_version}.dmg"
else
	echo "dossier $1 introuvable"
fi