
#############################################################
#      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=tmp-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

# Pre-settings 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 properly. This is why we set 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"


##################### Action steps to perform ###############
# Add or remove the actions in the actionList to perform or 
# not them. Take care that the a right order is mandatory
#############################################################

# the normal and complete list of actions. Works normally fine 
# on most of the system. Based on the provide pre-settings of 
# cmake. 
actionList=(checking init recoveringSrc cmakeAction compilation repackaging compression)

# perform on the end of the compilation packaging procedure
# typicaly usefull if you have used cmake/ccmake in interactive
# mode for a specific setting and generate the makefiles manually. 
# actionList=(compilation repackaging compression)


##################### 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}


echo "The command line is " $1

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

# search the cmd given in argument
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 "Start the compilation/packaging process..."
      echo "Directories handling and initialisation..."

      if [ ! -d  $ROOT_DIR/build ]; then
	mkdir $ROOT_DIR/build
      else
         echo $ROOT_DIR/build" already exist..."
      fi

      if [ ! -d $ROOT_DIR/VTK-Bin ]; then
         mkdir $ROOT_DIR/VTK-Bin
      else
         echo $ROOT_DIR/VTK-Bin" already exist..."
      fi

      if [ ! -d $ROOT_DIR/$NATIVE_RELEASE_DIR ]; then
         mkdir $ROOT_DIR/$NATIVE_RELEASE_DIR
      else
         echo $ROOT_DIR/$NATIVE_RELEASE_DIR" already exist..."      
      fi

      if [ ! -d $ROOT_DIR/$INCLUDE_RELEASE_DIR ]; then
         mkdir $ROOT_DIR/$INCLUDE_RELEASE_DIR
      else
         echo $ROOT_DIR/$INCLUDE_RELEASE_DIR" already exist..."
      fi

      if [ ! -d $ROOT_DIR/$JAR_RELEASE_DIR ]; then
         mkdir  $ROOT_DIR/$JAR_RELEASE_DIR
      else
         echo $ROOT_DIR/$JAR_RELEASE_DIR" already exist..."
      fi
}


# downloading phase
donwload(){
          if  [ -f $ROOT_DIR/${VTK_SRC_LOCAL_TARBALL} ]; then
              echo "VTK sources tarball already found localy. Using it without new download..."
          else
              echo "Downloading the VTK sources release..."
              wget ${SRC_RELEASE_URL} -O $ROOT_DIR/${VTK_SRC_LOCAL_TARBALL}
          fi
          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
             #echo "First pass, without pre-configuration..."
             #cmake $CMAKE_PRE_SET $ROOT_DIR/VTK
             echo "Second pass, with the pre-configuration..."
             #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... "
             cd $ROOT_DIR/build
             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
}

checking(){
    echo "Check the local system and look for all needed tools..."
    search "gcc"
    search "g++"
    search "make"
    search "cmake"
    search "java"
    search "javac"
}


recoveringSrc(){
if [ "_"$1 != "_" ]; then
   echo "Copying VTK sources from..." $1
   cp $1 $ROOT_DIR/${VTK_SRC_LOCAL_TARBALL}
   tar xzf $ROOT_DIR/${VTK_SRC_LOCAL_TARBALL}
else
   echo "Downloading VTK source from the Web..."
   build donwload
fi
}


STEP=1
# performing each step
len=${#actionList[*]}
i=0
while [ $i -lt $len ]; do
   echo ${actionList[$i]}
   build ${actionList[$i]}
   let i++
done


echo "End"
