#!/usr/bin/env bash

VERBOSE="yes"

PWD=`pwd`
TNL_MATRIX_DRAW="tnl-grid-view"
IWD="$PWD"

#source ../tnl-env-variables
#export TNL_SPARSE_MATRIX_CHECK_CFG_DESC_FILE="$TNL_SOURCE_DIR/tests/tnl-sparse-matrix-check.cfg.desc"

source matrix-market
source florida-matrix-market

draw_matrix()
{
   # $1 - matrix name

   if test ! -e $1.pdf;
   then
      echo "Drawing $1.pdf"
      $TNL_MATRIX_DRAW --input-files $1.double.bin.bz2 --output-files $1.eps --output-format eps --verbose $VERBOSE
      epstopdf $1.eps            
      rm $1.eps
   else
      echo "$1.pdf exists -- SKIPPING."
   fi
   
   for sort in "" sort.;
   do
      for group_size in 16 32 64;
      do
         bool_sort="no"
         if test x$sort = xsort.;
         then
            bool_sort="yes"
         fi
         if test ! -e $1.${sort}rgcsr-$group_size.pdf;
         then
            echo "Drawing $1.${sort}rgcsr-$group_size.pdf"
            $TNL_MATRIX_DRAW --input-files $1.double.bin.bz2 \
                             --output-files $1.${sort}rgcsr-$group_size.eps \
                             --output-format eps \
                             --matrix-format rg-csr \
                             --matrix-group-size $group_size \
                             --sort-matrix $bool_sort \
                             --verbose $VERBOSE
            epstopdf $1.${sort}rgcsr-$group_size.eps            
            rm $1.${sort}rgcsr-$group_size.eps
         else
            echo "$1.${sort}rgcsr-$group_size.pdf exists -- SKIPPING."
         fi
      done


      for chunk_size in 1 2 4 8 16 32;
      do
         for cuda_block_size in 16 32 64 128 256;
         do        
            if test ! -e $1.${sort}argcsr-$chunk_size-$cuda_block_size.pdf;
            then
               echo "Drawing $1.${sort}argcsr-$chunk_size-$cuda_block_size.pdf"
               $TNL_MATRIX_DRAW --input-files $1.double.bin.bz2 \
                                --output-files $1.${sort}argcsr-$chunk_size-$cuda_block_size.eps \
                                --output-format eps \
                                --matrix-format arg-csr \
                                --desired-matrix-chunk-size $chunk_size \
                                --cuda-block-size $cuda_block_size \
                                --sort-matrix $bool_sort \
                                --verbose $VERBOSE
               epstopdf $1.${sort}argcsr-$chunk_size-$cuda_block_size.eps            
               rm $1.${sort}argcsr-$chunk_size-$cuda_block_size.eps
            else
               echo "$1.${sort}argcsr-$chunk_size-$cuda_block_size.pdf exists -- SKIPPING."
            fi
         done
      done
   done   
   
}

for link in $MM_MATRICES;
do
   matrix=matrices`echo $link | sed 's/ftp:\/\/math.nist.gov\/pub//'`
   unzipped_matrix=`echo $matrix | sed 's/.gz//'`
   if test ! -e $matrix;
   then
      echo "Matrix $matrix is missing !!! Run the script 'get-matrices' first."      
   else
      if test -e $unzipped_matrix.double.bin.bz2;         
      then
         draw_matrix $unzipped_matrix        
      else         
         echo "Matrix $unzipped_matrix.double.bin.bz2 is missing - run the script convert-matrices first."         
      fi
      
      if test -e $unzipped_matrix.amd.double.bin.bz2;         
      then
         draw_matrix $unzipped_matrix.amd        
      else         
         echo "Matrix $unzipped_matrix.amd.double.bin.bz2 is missing - run the script convert-matrices first."         
      fi
      
      
   fi
done

for link in $FLORIDA_MM_MATRICES;
do
   matrix=matrices`echo $link | sed 's/http:\/\/www.cise.ufl.edu\/research\/sparse//'`
   if test ! -e $matrix;
   then      
      echo "Matrix $matrix is missing !!! Run the script 'get-matrices' first."
   else
      DIRNAME=`dirname $matrix`
      FILENAME=`basename $matrix`
      cd $DIRNAME
      echo "Unpacking $FILENAME"
      tar zxvf $FILENAME
      cd $IWD
      SUBDIRNAME=`echo $FILENAME | sed 's/.tar.gz//'`
      rm -f $DIRNAME/$SUBDIRNAME/*_b.mtx # these are usually in array format
      for file in $DIRNAME/$SUBDIRNAME/*.mtx;
      do
         if test -e $file.double.bin.bz2;
         then
            draw_matrix $file
         else
            echo "Matrix $file.double.bin.bz2 is missing - run the script convert-matrices first."             
         fi
         
         if test -e $file.amd.double.bin.bz2;
         then
            draw_matrix $file.amd
         else
            echo "Matrix $file.amd.double.bin.bz2 is missing - run the script convert-matrices first."             
         fi         
      done
   fi
done
