#!/bin/sh

if [ "x$MSYSTEM" != "x" ] ; then
  EXEEXT=".exe"
  LIBEXT="dll"
else
  EXEEXT=""
  LIBEXT="so"
fi

echo Creating output directory tree

mkdir -p output/share/codeblocks/lexers
mkdir -p output/share/codeblocks/images/settings
mkdir -p output/share/codeblocks/images/codecompletion
mkdir -p output/share/codeblocks/images/16x16
mkdir -p output/share/codeblocks/plugins
mkdir -p output/share/codeblocks/templates
mkdir -p output/share/codeblocks/templates/wizard
mkdir -p output/share/codeblocks/scripts
mkdir -p devel/share/codeblocks/lexers
mkdir -p devel/share/codeblocks/images/settings
mkdir -p devel/share/codeblocks/images/codecompletion
mkdir -p devel/share/codeblocks/images/16x16
mkdir -p devel/share/codeblocks/plugins
mkdir -p devel/share/codeblocks/templates
mkdir -p devel/share/codeblocks/templates/wizard
mkdir -p devel/share/codeblocks/scripts

ZIPCMD="zip"
RESDIR="devel/share/codeblocks"
echo Compressing core UI resources
${ZIPCMD} -jqu9 ${RESDIR}/resources.zip src/resources/*.xrc > /dev/null
${ZIPCMD} -jqu9 ${RESDIR}/manager_resources.zip sdk/resources/*.xrc sdk/resources/images/*.png > /dev/null
${ZIPCMD} -jqu9 ${RESDIR}/start_here.zip src/resources/start_here/* > /dev/null
echo Compressing plugins UI resources
${ZIPCMD} -jqu9 ${RESDIR}/astyle.zip plugins/astyle/resources/manifest.xml plugins/astyle/resources/*.xrc > /dev/null
${ZIPCMD} -jqu9 ${RESDIR}/autosave.zip plugins/autosave/manifest.xml plugins/autosave/*.xrc > /dev/null
${ZIPCMD} -jqu9 ${RESDIR}/classwizard.zip plugins/classwizard/resources/manifest.xml plugins/classwizard/resources/*.xrc > /dev/null
${ZIPCMD} -jqu9 ${RESDIR}/codecompletion.zip plugins/codecompletion/resources/manifest.xml plugins/codecompletion/resources/*.xrc > /dev/null
${ZIPCMD} -jqu9 ${RESDIR}/compiler.zip plugins/compilergcc/resources/manifest.xml plugins/compilergcc/resources/*.xrc > /dev/null
${ZIPCMD} -jqu9 ${RESDIR}/debugger.zip plugins/debuggergdb/resources/manifest.xml plugins/debuggergdb/resources/*.xrc > /dev/null
${ZIPCMD} -jqu9 ${RESDIR}/defaultmimehandler.zip plugins/defaultmimehandler/resources/manifest.xml plugins/defaultmimehandler/resources/*.xrc > /dev/null
${ZIPCMD} -jqu9 ${RESDIR}/openfileslist.zip plugins/openfileslist/manifest.xml > /dev/null
${ZIPCMD} -jqu9 ${RESDIR}/projectsimporter.zip plugins/projectsimporter/resources/*.xrc plugins/projectsimporter/resources/manifest.xml > /dev/null
${ZIPCMD} -jqu9 ${RESDIR}/scriptedwizard.zip plugins/scriptedwizard/resources/manifest.xml > /dev/null
${ZIPCMD} -jqu9 ${RESDIR}/todo.zip plugins/todo/resources/manifest.xml plugins/todo/resources/*.xrc > /dev/null
echo Packing core UI bitmaps
cd src/resources
${ZIPCMD} -0 -qu ../../${RESDIR}/resources.zip images/*.png images/16x16/*.png > /dev/null
#cd ../../sdk/resources
#${ZIPCMD} -0 ../../${RESDIR}/manager_resources.zip images/*.png > /dev/null
echo Packing plugins UI bitmaps
cd ../../plugins/compilergcc/resources
${ZIPCMD} -0 -qu ../../../${RESDIR}/compiler.zip images/*.png images/16x16/*.png > /dev/null
cd ../../../plugins/debuggergdb/resources
${ZIPCMD} -0 -qu ../../../${RESDIR}/debugger.zip images/*.png images/16x16/*.png > /dev/null
cd ../../..

echo Copying files
# Create an exclude pattern file
echo .svn > excludes.txt
echo Makefile >> excludes.txt
echo Makefile.am >> excludes.txt
echo Makefile.in >> excludes.txt
cp -fu ${RESDIR}/*.zip output/share/codeblocks > /dev/null
cp -fu sdk/resources/lexers/lexer_* ${RESDIR}/lexers > /dev/null
cp -fu sdk/resources/lexers/lexer_* output/share/codeblocks/lexers > /dev/null
cp -fu src/resources/images/*.png ${RESDIR}/images > /dev/null
cp -fu src/resources/images/settings/*.png ${RESDIR}/images/settings > /dev/null
cp -fu src/resources/images/*.png output/share/codeblocks/images > /dev/null
cp -fu src/resources/images/settings/*.png output/share/codeblocks/images/settings > /dev/null
cp -fu src/resources/images/16x16/*.png ${RESDIR}/images/16x16 > /dev/null
cp -fu src/resources/images/16x16/*.png output/share/codeblocks/images/16x16 > /dev/null
cp -fu plugins/codecompletion/resources/images/*.png ${RESDIR}/images/codecompletion > /dev/null
cp -f plugins/codecompletion/resources/images/*.png output/share/codeblocks/images/codecompletion > /dev/null
rsync -au --exclude-from=excludes.txt plugins/scriptedwizard/resources/ ${RESDIR}/templates/wizard > /dev/null
rsync -au --exclude-from=excludes.txt plugins/scriptedwizard/resources/ output/share/codeblocks/templates/wizard > /dev/null
rsync -au --exclude-from=excludes.txt templates/common/ output/share/codeblocks/templates > /dev/null
rsync -au --exclude-from=excludes.txt templates/unix/ output/share/codeblocks/templates > /dev/null
rsync -au --exclude-from=excludes.txt templates/common/ ${RESDIR}/templates > /dev/null
rsync -au --exclude-from=excludes.txt templates/unix/ ${RESDIR}/templates > /dev/null
cp -fu scripts/*.script ${RESDIR}/scripts > /dev/null
cp -fu scripts/*.script output/share/codeblocks/scripts > /dev/null
cp -fu scripts/*.gdb ${RESDIR}/scripts > /dev/null
cp -fu scripts/*.gdb output/share/codeblocks/scripts > /dev/null
cp -fu tips.txt devel/share/codeblocks > /dev/null
cp -fu tips.txt output/share/codeblocks > /dev/null
cp -fu tools/ConsoleRunner/cb_console_runner${EXEEXT} output > /dev/null
cp -fu tools/ConsoleRunner/cb_console_runner${EXEEXT} devel > /dev/null
cp -fu devel/codeblocks${EXEEXT} output > /dev/null
# cp -fu devel/cb_share_config${EXEEXT} output > /dev/null
cp -fu devel/*.${LIBEXT} output > /dev/null
cp -fu ${RESDIR}/plugins/*.${LIBEXT} output/share/codeblocks/plugins > /dev/null
# Now remove the exclude pattern file
rm excludes.txt

echo Stripping debug info from output tree
strip output/codeblocks${EXEEXT}
# strip output/cb_share_config${EXEEXT}
strip output/cb_console_runner${EXEEXT}
# strip output/codesnippets${EXEEXT}
strip output/*.${LIBEXT}
strip output/share/codeblocks/plugins/*.${LIBEXT}

if [ "x$MSYSTEM" = "x" ] ; then
  echo Creating launch-scripts
  echo "#!/bin/sh" > output/run.sh
  echo -n "APP_DIR=" >> output/run.sh
  echo `pwd`/output >> output/run.sh
  echo "export LD_LIBRARY_PATH=\$APP_DIR:\$LD_LIBRARY_PATH" >> output/run.sh
  echo "\$APP_DIR/codeblocks \$@" >> output/run.sh
  chmod +x output/run.sh
  echo "#!/bin/sh" > devel/run.sh
  echo -n "APP_DIR=" >> devel/run.sh
  echo `pwd`/devel >> devel/run.sh
  echo "export LD_LIBRARY_PATH=\$APP_DIR:\$LD_LIBRARY_PATH" >> devel/run.sh
  echo "\$APP_DIR/codeblocks \$@" >> devel/run.sh
  chmod +x devel/run.sh
fi

ZIPCMD=
RESDIR=
