#!/bin/bash
#
#  File        : cimg_buildpackage
#                ( Bash script )
#
#  Description : Build .zip, 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 [final] [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" == "final" -o "$2" == "final" ]; then SUFFIX=""; else SUFFIX=_testing; fi

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

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

# 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 CHANGES.txt $DEST_DIR
sed s\/_cimg_version\/$RELEASE$SUFFIX\/ README.txt > $DEST_DIR/README.txt

mkdir $DEST_DIR/examples
cd $SRC_DIR/examples
cp -f *.cpp *.h *_def.gmic *.m $DEST_DIR/examples/
sed s\/_cimg_version\/$RELEASE$SUFFIX\/ Makefile > $DEST_DIR/examples/Makefile
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 *.h favicon.* $DEST_DIR/html/
sed s\/_cimg_version\/$RELEASE\/ CImg.doxygen > $DEST_DIR/html/CImg.doxygen
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_win_visualcpp $DEST_DIR/resources/
cd $DEST_DIR/resources/debian
sed s\/_cimg_version\/$RELEASE\/ changelog > /tmp/changelog
mv /tmp/changelog changelog

cd $DEST_DIR
for i in CImg.h examples/*.cpp examples/*.gmic; 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 "*.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 file 'gmic_def.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

# Commit changes on CVS repository
echo " - Commit on CVS repository."
cd $SRC_DIR
cvs commit -m "">>$LOG_FILE 2>&1

# 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 Debian package for all architectures.
# echo " - Build Debian package file '$DEB_FILE'."
# cd $DEST_DIR/..
# rm -rf $DEB_DIR $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_FILE0
# 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
# if [ "$SUFFIX" != "" ]; then mv $DEB_FILE0 $DEB_FILE; fi

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

# Copy files to sourceforge server.
cd $BASE_DIR
FORGE=ronounours,cimg@frs.sourceforge.net:/home/frs/project/c/ci/cimg/
scp ${ZIP_FILE} ${DEB_FILE} $FORGE

cd $DEST_DIR/html/
scp -r * ronounours,cimg@frs.sourceforge.net:htdocs/

# Create pre-compiled Debian packages for 32 and 64 bits.
# cd $DEST_DIR/..
# scp $DEB_DIR.tar.gz lynx.greyc.ensicaen.fr:/tmp/
# scp $DEB_DIR.tar.gz gemini.greyc.ensicaen.fr:/tmp/

# ssh -f lynx "\
# sleep 10 && \
# cd /tmp && \
# rm -rf $DEST_DIR && \
# tar zxvf $DEB_DIR.tar.gz && \
# rm $DEB_DIR.tar.gz && \
# cd $DEB_DIR && \
# mkdir -p bin && \
# cd examples && \
# make CC=\"g++ -Dcimg_imagepath=\\\\\\\"/usr/share/CImg/examples/img/\\\\\\\"\" EXEPFX=../bin/cimg_ olinux && \
# rm -f *.o && \
# cd ../ && \
# cp -f CHANGES.txt changelog && \
# cp -rf resources/debian . && \
# cd debian/ && \
# echo \"bin/* usr/bin/\" >> cimg-dev.install && \
# cd ../../ && \
# tar cvf $DEB_DIR.tar $DEB_DIR && \
# gzip $DEB_DIR.tar && \
# cp -f $DEB_DIR.tar.gz cimg_$RELEASE.orig.tar.gz && \
# cd $DEB_DIR && \
# export DEBNAME=$DEB_FILE0 && \
# export DEBFULLNAME=\"David Tschumperl\" && \
# export DEBEMAIL=\"David.Tschumperle@greyc.ensicaen.fr\" && \
# dpkg-buildpackage -rfakeroot ; \
# cd ../ && \
# mv $DEB_FILE0 cimg-dev_${RELEASE}${SUFFIX}_i386.deb && \
# scp cimg-dev_${RELEASE}${SUFFIX}_i386.deb $FORGE && \
# cp -f cimg-dev_${RELEASE}${SUFFIX}_i386.deb \$HOME/work/src && \
# echo LYNX_DONE" &

# ssh -f gemini "\
# sleep 10 && \
# cd /tmp && \
# rm -rf $DEST_DIR && \
# tar zxvf $DEB_DIR.tar.gz && \
# rm $DEB_DIR.tar.gz && \
# cd $DEB_DIR && \
# mkdir -p bin && \
# cd examples && \
# make CC=\"g++ -Dcimg_imagepath=\\\\\\\"/usr/share/CImg/examples/img/\\\\\\\"\" EXEPFX=../bin/cimg_ olinux && \
# rm -f *.o && \
# cd ../ && \
# cp -f CHANGES.txt changelog && \
# cp -rf resources/debian . && \
# cd debian/ && \
# echo \"bin/* usr/bin/\" >> cimg-dev.install && \
# cd ../../ && \
# tar cvf $DEB_DIR.tar $DEB_DIR && \
# gzip $DEB_DIR.tar && \
# cp -f $DEB_DIR.tar.gz cimg_$RELEASE.orig.tar.gz && \
# cd $DEB_DIR && \
# export DEBNAME=$DEB_FILE0 && \
# export DEBFULLNAME=\"David Tschumperl\" && \
# export DEBEMAIL=\"David.Tschumperle@greyc.ensicaen.fr\" && \
# dpkg-buildpackage -rfakeroot ; \
# cd ../ && \
# mv $DEB_FILE0 cimg-dev_${RELEASE}${SUFFIX}_amd64.deb && \
# scp cimg-dev_${RELEASE}${SUFFIX}_amd64.deb $FORGE && \
# cp -f cimg-dev_${RELEASE}${SUFFIX}_amd64.deb \$HOME/work/src && \
# echo GEMINI_DONE" &

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