
#############################################################
#      Artenum's VTK compilation and packaging script       #
#                           for                             #
#                         Keridwen                          #
#                (see http:/www.artenum.com)                #
#                      Version 1.1.0                        #
#                        2011/01/30                         #
#   (c) artenum sarl, France, 2011                          #
#############################################################


####################### SETTINGS ############################
SRC_RELEASE_URL=http://www.vtk.org/files/release/5.6/vtk-5.6.1.tar.gz
VTK_SRC_LOCAL_TARBALL=vtk-5.6.1.tar.gz
NATIVE_RELEASE_DIR=vtk-native-5.6.1-linux-x86_32-glibc2_12-jvm1_6
INCLUDE_RELEASE_DIR=vtk-include-5.6.1-linux-x86_32-glibc2_12-jvm1_6
JAR_RELEASE_DIR=vtk-jar-5.6.1-linux-x86_32-glibc2_12-jvm1_6

# if you wan to by-pass the compilation phase set it at 1, otherwise let it at 0
BY_PASS_COMPILATION=0

# Presettings to compile VTK for Keridwen, i.e with the Java wrapping, the shared build and 
# parallel on. 
# Nota Bene: With some versions of cmake the -C flag does not work properll. This is why 
# we sett all value as an independent argument. 
CMAKE_PRE_SET="-DBUILD_SHARED_LIBS:BOOLEAN=ON -DVTK_WRAP_JAVA:BOOLEAN=ON -DCMAKE_INSTALL_PREFIX:PATH=$ROOT_DIR/VTK-Bin/ -DVTK_USE_PARALLEL:BOOLEAN=ON"

##################### END OF SETTING SECTION ################

#############################################################
# DO NOT EDIT THIS SECTION                                  #
#############################################################
export PATH=$PATH:/user/bin/
CMD_LINE=`dirname $0`
START_POINT=$PWD
#echo "START_POINT="$START_POINT

# Build-up the absolute path for the SPIS command
if [ ${CMD_LINE:0:1} == "/" ]; then
   echo "absolu";
   HERE=$CMD_LINE
else
   echo "relatif";
   HERE=$START_POINT"/"$CMD_LINE
fi
ROOT_DIR=${HERE}

###############

search() {
        echo ""
        echo -n "Step ${STEP}/${TOTAL_NB_SERVICES}: search of ${1}... "
        which $1
        if [ $? -ne 0 ]; then
                echo "Unable to found $1"
                exit 1
        fi
        echo "${1} found"
        echo ""
        let STEP++
}


build() {
	echo ""
	echo -n "Step ${STEP}/${TOTAL_NB_SERVICES}: Building of ${1}... "
	echo ""
        $1
	if [ $? -ne 0 ]; then
		echo "Unable to do $1"
		exit 1
	fi
        echo "DONE"
	let STEP++
}



###############
# directories handling and initialisation
# please check your settings here. 
init(){
      echo "Directories handling and initialisation..."
      mkdir $ROOT_DIR/build
      mkdir $ROOT_DIR/VTK-Bin
      mkdir $ROOT_DIR/$NATIVE_RELEASE_DIR
      mkdir $ROOT_DIR/$INCLUDE_RELEASE_DIR
      mkdir  $ROOT_DIR/$JAR_RELEASE_DIR
}


# downloading phase
donwload(){
          echo "Downloading the VTK sources release..."
          wget ${SRC_RELEASE_URL} ${VTK_SRC_LOCAL_TARBALL}
          echo "Unpacking the VTK sources tarball..."
          tar xzf $ROOT_DIR/${VTK_SRC_LOCAL_TARBALL}
}


# cmake phase
cmakeAction(){
             echo "Cmake configuration phase... "
             cd $ROOT_DIR/build
             #cmake -DBUILD_SHARED_LIBS:BOOLEAN=ON -DVTK_WRAP_JAVA:BOOLEAN=ON -DCMAKE_INSTALL_PREFIX:PATH=$ROOT_DIR/VTK-Bin/ -DVTK_USE_PARALLEL:BOOLEAN=ON $ROOT_DIR/VTK
             cmake $CMAKE_PRE_SET $ROOT_DIR/VTK
}


# compilation
compilation(){
             echo "Compilation phase... "
             make
             make install
}

# repackaging phase
repackaging(){
             echo "Packaging phase for Keridwen... "
             cp -r $ROOT_DIR/VTK-Bin/lib/vtk-5.6/* $ROOT_DIR/$NATIVE_RELEASE_DIR
             cp -r $ROOT_DIR/VTK-Bin/lib/vtk-5.6/java/vtk.jar $ROOT_DIR/$JAR_RELEASE_DIR/vtk-5.6.1.jar
             cp -r $ROOT_DIR/VTK-Bin/include/vtk-5.6/* $ROOT_DIR/$INCLUDE_RELEASE_DIR

             # adding the Artenum's header and platform information to 
             # the binary package
             cat $ROOT_DIR/packagingHeader.txt > $ROOT_DIR/$NATIVE_RELEASE_DIR/README.TXT
             $ROOT_DIR/getTargetedPlatformInfo.sh 
             cat platform.info >> $ROOT_DIR/$NATIVE_RELEASE_DIR/README.TXT
             cp $ROOT_DIR/$NATIVE_RELEASE_DIR/README.TXT $ROOT_DIR/$JAR_RELEASE_DIR
             cp $ROOT_DIR/$NATIVE_RELEASE_DIR/README.TXT $ROOT_DIR/$INCLUDE_RELEASE_DIR
}

# compression phase
compression(){
             echo "Release tarballs compression phase... "
             tar czf $ROOT_DIR/$NATIVE_RELEASE_DIR.tgz $ROOT_DIR/$NATIVE_RELEASE_DIR
             MD5_KEY_VAR=`md5sum $ROOT_DIR/$NATIVE_RELEASE_DIR.tgz`
             echo $ROOT_DIR/$NATIVE_RELEASE_DIR.tgz" generated with the following MD5 checksum: "$MD5_KEY_VAR

             tar czf $ROOT_DIR/$JAR_RELEASE_DIR.tgz $ROOT_DIR/$JAR_RELEASE_DIR
             MD5_KEY_VAR=`md5sum $ROOT_DIR/$JAR_RELEASE_DIR.tgz`
             echo $ROOT_DIR/$JAR_RELEASE_DIR.tgz" generated with the following MD5 checksum: "$MD5_KEY_VAR

             tar czf $ROOT_DIR/$INCLUDE_RELEASE_DIR.tgz $ROOT_DIR/$INCLUDE_RELEASE_DIR
             MD5_KEY_VAR=`md5sum $ROOT_DIR/$INCLUDE_RELEASE_DIR.tgz`
             echo $ROOT_DIR/$INCLUDE_RELEASE_DIR.tgz" generated with the following MD5 checksum: "$MD5_KEY_VAR
}

STEP=1
echo "Check the local system and look for all needed tools..."
search "gcc"
search "g++"
search "make"
search "cmake"
search "java"
search "javac"

echo "Start the compilation/packaging process..."
build init
build donwload
build cmakeAction
if [ $BY_PASS_COMPILATION -ne 1 ]; then
           build compilation
fi
build repackaging
build compression

echo "End"
