#!/bin/bash
#
#  File        : cimg_buildpackage
#                ( Bash script )
#
#  Description : Build .zip, .tar.gz and .deb package files
#                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.sourceforge.net )
#
#  Usage       : ./cimg_buildpackage [beta] [compile]
#
#  Copyright   : David Tschumperle
#                ( http://www.greyc.ensicaen.fr/~dtschump/ )
#
#  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`
RELEASE=$RELEASE1.$RELEASE2.$RELEASE3

# Read command line options.
if [ "$1" == "beta" -o "$2" == "beta" ]; then BETA="yes"; RELEASE=${RELEASE}beta; else BETA="no"; fi
if [ "$1" == "compile" -o "$2" == "compile" ]; then COMPILE="yes"; else COMPILE="no"; fi

# Define the different paths and filenames used in this script.
BASE_DIR=`pwd`
cd ${BASE_DIR}
SRC_DIR=${BASE_DIR}/CImg
DEST_DIR=/tmp/CImg-${RELEASE}
ZIP_FILE=CImg-${RELEASE}.zip
TAR_FILE=CImg_${RELEASE}.tar
DEB_DIR=cimg-${RELEASE}
DEB_FILE=cimg-dev_${RELEASE}-1_all.deb
LOG_FILE=${BASE_DIR}/LOG_`basename $DEST_DIR`.txt
rm -rf $LOG_FILE

echo
echo " - Release number : $RELEASE"
echo " - Base directory : $BASE_DIR/"
echo " - Source directory : $SRC_DIR/"
echo " - Build directory : $DEST_DIR/"
echo " - ZIP package filename : $ZIP_FILE"
echo " - TAR.GZ package filename : $TAR_FILE.gz"
echo " - DEB package filename : $DEB_FILE"
echo " - LOG file : $LOG_FILE"
echo " - Compile examples : $COMPILE"

# Create archive structure
echo " - Create package structure."
rm -rf $DEST_DIR
mkdir $DEST_DIR
cd $SRC_DIR
cp -f CHANGES.txt CImg.h Licence_CeCILL-C_V1-en.txt Licence_CeCILL_V2-en.txt README.txt $DEST_DIR

mkdir $DEST_DIR/examples
cd $SRC_DIR/examples
cp -f *.cpp *.h *_def.raw Makefile *.m $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/html
cd $SRC_DIR/html
cp -f *.shtml *.html *.doxygen *.h favicon.* $DEST_DIR/html/
mkdir $DEST_DIR/html/img
cd $SRC_DIR/html/img
cp -f *.html *.jpg *.gif *.ppm $DEST_DIR/html/img/
mkdir $DEST_DIR/html/img/reference
cd $SRC_DIR/html/img/reference
cp -f *.jpg $DEST_DIR/html/img/reference/

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 cimg_buildpackage debian project_* $DEST_DIR/resources/

# Clean directory
echo " - Clean package directory."
cd $DEST_DIR
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 "*.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
iconv -t utf8 -f latin1 resources/debian/changelog > /tmp/foo.changelog
mv /tmp/foo.changelog resources/debian/changelog
iconv -t utf8 -f latin1 resources/debian/control > /tmp/foo.control
mv /tmp/foo.control resources/debian/control
chmod a+x $DEST_DIR/resources/cimg_buildpackage

# Generate special files 'gmic.h' and 'gmic_gimp.h'
# (gmic must be installed !)
cd $DEST_DIR/examples
make gmic_def >>$LOG_FILE 2>&1

# Generate Documentation with doxygen
echo " - Generate reference documentation using Doxygen."
cd $DEST_DIR/html
echo -e "\n** Log generated by 'doxygen' **\n\n">>$LOG_FILE
doxygen CImg.doxygen>>$LOG_FILE 2>&1

echo " - Build reference documentation in PDF format."
cd $DEST_DIR/html/latex
echo -e "\n** Log generated by 'latex' **\n\n">>$LOG_FILE
make>>$LOG_FILE 2>&1
cp -f refman.pdf ../CImg_reference.pdf
rm -rf ../latex

# 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

# Create TAR.GZ archive
echo " - Build TAR.GZ archive file '$TAR_FILE.gz'."
cd $DEST_DIR/..
rm -f $TAR_FILE $TAR_FILE.gz
echo -e "\n** Log generated by 'tar' **\n\n">>$LOG_FILE
tar cvf $TAR_FILE `basename $DEST_DIR`>>$LOG_FILE 2>&1
gzip --best $TAR_FILE

# Compile examples
if [ $COMPILE == "yes" ]; then
echo " - Compile CImg examples."
cd $DEST_DIR/examples/
mkdir -p ../bin
echo -e "\n** Log generated by 'CImg compilation' **\n\n">>$LOG_FILE
make "CC=g++ -Dcimg_imagepath=\"\\\"/usr/share/CImg/examples/img/\\\"\"" EXEPFX=../bin/cimg_ olinux
rm -f *.o
cd $DEST_DIR/resources/debian
echo "bin/* usr/bin/" >> cimg-dev.install
fi

# Create Debian package
echo " - Build Debian package file '$DEB_FILE'."
cd $DEST_DIR/..
rm -rf $DEB_DIR.tar $DEB_DIR.tar.gz
mv $DEST_DIR $DEB_DIR
tar cvf $DEB_DIR.tar $DEB_DIR>>$LOG_FILE 2>&1
gzip $DEB_DIR.tar
cp -f $DEB_DIR.tar.gz cimg_$RELEASE.orig.tar.gz

cd $DEB_DIR
cp -f CHANGES.txt changelog
cp -rf resources/debian .
export DEBNAME=$DEB_FILE
export DEBFULLNAME="David Tschumperl"
export DEBEMAIL="David.Tschumperle@greyc.ensicaen.fr"
echo -e "\n** Log generated by 'Debian packaging tools' **\n\n">>$LOG_FILE
dpkg-buildpackage -rfakeroot>>$LOG_FILE 2>&1
cd ../
mv $DEB_DIR $DEST_DIR

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

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