#!/bin/bash
#
#  File        : cimg_buildpackage
#                ( Bash script )
#
#  Description : Build .zip package file of the CImg Library, from the current CImg/
#                directory. Must be run from ../CImg
#                This file is a part of the CImg Library project.
#                ( http://cimg.eu/ )
#
#  Usage       : ./cimg_buildpackage [final]
#
#  Copyright   : David Tschumperlé
#                ( http://tschumperle.users.greyc.fr/ )
#
#  License     : CeCILL v2.0
#                ( http://www.cecill.info/licences/Licence_CeCILL_V2-en.html )
#
#  This software is governed by the CeCILL  license under French law and
#  abiding by the rules of distribution of free software.  You can  use,
#  modify and/ or redistribute the software under the terms of the CeCILL
#  license as circulated by CEA, CNRS and INRIA at the following URL
#  "http://www.cecill.info".
#
#  As a counterpart to the access to the source code and  rights to copy,
#  modify and redistribute granted by the license, users are provided only
#  with a limited warranty  and the software's author,  the holder of the
#  economic rights,  and the successive licensors  have only  limited
#  liability.
#
#  In this respect, the user's attention is drawn to the risks associated
#  with loading,  using,  modifying and/or developing or reproducing the
#  software by the user in light of its specific status of free software,
#  that may mean  that it is complicated to manipulate,  and  that  also
#  therefore means  that it is reserved for developers  and  experienced
#  professionals having in-depth computer knowledge. Users are therefore
#  encouraged to load and test the software's suitability as regards their
#  requirements in conditions enabling the security of their systems and/or
#  data to be ensured and,  more generally, to use and operate it in the
#  same conditions as regards security.
#
#  The fact that you are presently reading this means that you have had
#  knowledge of the CeCILL license and that you accept its terms.
#

# Define release number.
RELEASE0=`grep "#define cimg_version" CImg/CImg.h | tail -c 4`
RELEASE1=`echo $RELEASE0 | head -c 1`
RELEASE2=`echo $RELEASE0 | head -c 2 | tail -c 1`
RELEASE3=`echo $RELEASE0 | head -c 3 | tail -c 1`
VERSION=${RELEASE1}${RELEASE2}${RELEASE3}
SVERSION=${RELEASE1}.${RELEASE2}.${RELEASE3}

# Read command line options.
if [ "$1" == "final" ]; then SUFFIX=""; SSUFFIX=""; else SUFFIX=_pre`date +%m%d%y`; SSUFFIX=_pre; fi

# Define the different paths and filenames used in this script.
BASE_DIR=`pwd`
SRC_DIR=${BASE_DIR}/CImg
DEST_DIR=/tmp/CImg-${SVERSION}${SUFFIX}
ZIP_FILE=CImg_${SVERSION}${SSUFFIX}.zip
LOG_FILE=${BASE_DIR}/LOG_`basename $ZIP_FILE .zip`.txt
rm -rf $LOG_FILE

echo
echo " - Release number : $SVERSION$SUFFIX"
echo " - Base directory : $BASE_DIR/"
echo " - Source directory : $SRC_DIR/"
echo " - Build directory : $DEST_DIR/"
echo " - ZIP package filename : $ZIP_FILE"
echo " - LOG file : $LOG_FILE"

# Merge 'develop' and 'master' branches.
echo " - Merge 'develop' and 'master' branches."
cd $SRC_DIR
git checkout master
git merge develop -m "."
git push
git checkout develop
cd ..

# Create clean archive structure.
echo " - Create package structure."
rm -rf $DEST_DIR
mkdir $DEST_DIR
cd $SRC_DIR
cp -f CImg.h Licence_CeCILL-C_V1-en.txt Licence_CeCILL_V2-en.txt $DEST_DIR
sed s\/_cimg_version\/$SVERSION$SUFFIX\/ README.txt > $DEST_DIR/README.txt

mkdir $DEST_DIR/examples
cd $SRC_DIR/examples
cp -f *.cpp *.m CMakeLists.txt Makefile $DEST_DIR/examples/

mkdir $DEST_DIR/examples/img
cd $SRC_DIR/examples/img
cp -f *.pgm *.ppm *.bmp *.h $DEST_DIR/examples/img/

mkdir $DEST_DIR/plugins
cd $SRC_DIR/plugins
cp -f *.h $DEST_DIR/plugins/

mkdir $DEST_DIR/resources
cd $SRC_DIR/resources
cp -rf *.bat *.py $DEST_DIR/resources/

cd $DEST_DIR
for i in CImg.h examples/*.cpp; do
  sed -e 's/ *$//' $i >/tmp/cimg_buildpackage$$ && mv /tmp/cimg_buildpackage$$ $i
done
for i in `find . -name "\#*"`; do rm -rf $i; done
for i in `find . -name "*~"`; do rm -rf $i; done
for i in `find . -name "core*"`; do rm -rf $i; done
for i in `find . -name "CVS"`; do rm -rf $i; done
for i in `find . -name ".git"`; do rm -rf $i; done
for i in `find . -name "*.plg"`; do rm -rf $i; done
for i in `find . -name "*.ncb"`; do rm -rf $i; done
for i in `find . -name "*.layout"`; do rm -rf $i; done
for i in `find . -name "*.win"`; do rm -rf $i; done
for i in `find . -name "Debug"`; do rm -rf $i; done
for i in `find . -name "Release"`; do rm -rf $i; done
for i in `find . -name "*.h"`; do col -x <$i >tmp; mv tmp $i; done
for i in `find . -name "*.cpp"`; do col -x <$i >tmp; mv tmp $i; done
for i in `find . ! -type d`; do chmod a-x $i; done
for i in `find . -name "*.sh"`; do chmod a+x $i; done
for i in `find . -name "rules"`; do chmod a+x $i; done

# Generate Documentation with doxygen
echo " - Generate reference documentation using Doxygen."
cd $SRC_DIR/html

if [ "$1" == "final" ]; then
    gmic _update_header_html header.html,${VERSION},0
    gmic _update_header_html header_doxygen.html,${VERSION},0
else
    gmic _update_header_html header.html,${VERSION},1
    gmic _update_header_html header_doxygen.html,${VERSION},1
fi

echo -e "\n** Log generated by 'doxygen' **\n\n">>$LOG_FILE
( cat CImg.doxygen ; echo "PROJECT_NUMBER=$SVERSION$SUFFIX" ) | doxygen - >>$LOG_FILE 2>&1

echo " - Build reference documentation in PDF format."
cd $SRC_DIR/html/latex
gmic it structcimg__library_1_1CImg.tex replace_str \"operator%\",\"operator\\\\%\" ot structcimg__library_1_1CImg.tex
echo -e "\n** Log generated by 'latex' **\n\n">>$LOG_FILE
make>>$LOG_FILE 2>&1
cp -f refman.pdf ../CImg_reference.pdf
cp -f refman.pdf $DEST_DIR/resources/CImg_reference.pdf
rm -rf ../latex

# Commit changes on GIT repository
echo " - Commit on GIT repository."
cd $SRC_DIR
if [ "$1" == "final" ]; then
    git tag -d v.$SVERSION
    git tag v.$SVERSION
    git commit -m "Final release "${SVERSION} >>$LOG_FILE 2>&1
else
    git commit -m "Auto-commit for release "${SVERSION}${SUFFIX} >>$LOG_FILE 2>&1
fi
git push --tags

# Create ZIP archive
echo " - Build ZIP archive file '$ZIP_FILE'."
cd $DEST_DIR/..
rm -f $ZIP_FILE
echo -e "\n** Log generated by 'zip' **\n\n">>$LOG_FILE
zip -r -9 $ZIP_FILE `basename $DEST_DIR`>>$LOG_FILE 2>&1

# Clean temporary files and directories
echo " - Clean temporary files and directories."
cd $DEST_DIR/..
mv $ZIP_FILE $BASE_DIR

# Copy files to CImg server.
cd $BASE_DIR
if [ "$1" == "final" ]; then
    lftp ftp://$GMIC_LOGIN:$GMIC_PASSWD@$GMIC_FTP -e "put -O /www/CImg/files/ ${ZIP_FILE}; quit";
fi
lftp ftp://$GMIC_LOGIN:$GMIC_PASSWD@$GMIC_FTP -e "put -O /www/CImg/files/ ${ZIP_FILE} -o CImg_latest.zip; quit"

cd $SRC_DIR/html/
lftp ftp://$GMIC_LOGIN:$GMIC_PASSWD@$GMIC_FTP -e "mirror -RL . /www/CImg/ ; quit"

# End of build script
echo -e " - All done, you should look at the LOG file '$LOG_FILE'.\n"