#!/usr/bin/env bash

BASE="ftp://math.nist.gov/pub/MatrixMarket2/Harwell-Boeing/"
                
PWD=`pwd`
TNL_MATRIX_CONVERT="tnl-matrix-convert --verify no"
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


for link in $MM_MATRICES;
do
   echo "###############################################################################"
   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.float.bin.bz2;
      then
         #echo "Converting $unzipped_matrix.float.bin.bz2 ..."
         gunzip -f $matrix
         $TNL_MATRIX_CONVERT --input-file $unzipped_matrix --output-file $unzipped_matrix.float.bin.bz2 --precision float --verbose yes --verify no
         gzip $unzipped_matrix
      else
         echo "Matrix $unzipped_matrix.float.bin.bz2 was already converted."         
      fi
      
      #######
      ## AMD ordering
      ###      
      #if test ! -e $unzipped_matrix.amd.float.bin.bz2;
      #then
      #   #echo "Converting $unzipped_matrix.amd.float.bin.bz2 ..."
      #   gunzip -f $matrix
      #   if test ! -e $unzipped_matrix.amd;
      #   then 
      #      echo "Ordering the matrix ..."
      #      ./reorder-csr -A -g 32 -i $unzipped_matrix -o $unzipped_matrix.amd
      #   fi        
      #   $TNL_MATRIX_CONVERT --input-file $unzipped_matrix.amd --output-file $unzipped_matrix.amd.float.bin.bz2 --precision float --verbose yes --verify no
      #   #rm $unzipped_matrix.amd
      #   gzip $unzipped_matrix
      #else
      #   echo "Matrix amd-$unzipped_matrix.float.bin.bz2 was already converted."         
      #fi
      
      if test ! -e $unzipped_matrix.double.bin.bz2;
      then
         #echo "Converting $matrix ..."
         gunzip -f $matrix
         $TNL_MATRIX_CONVERT --input-file $unzipped_matrix --output-file $unzipped_matrix.double.bin.bz2 --precision double --verbose yes --verify no
         gzip $unzipped_matrix
      else
         echo "Matrix $unzipped_matrix.double.bin.bz2 was already converted."         
      fi

      #######
      ## AMD ordering
      ###      
      #if test ! -e $unzipped_matrix.amd.double.bin.bz2;
      #then
      #   #echo "Converting $unzipped_matrix.amd.double.bin.bz2 ..."
      #   gunzip -f $matrix
      #   if test ! -e $unzipped_matrix.amd
      #   then 
      #      echo "Ordering the matrix ..."
      #      ./reorder-csr -A -g 32 -i $unzipped_matrix -o $unzipped_matrix.amd
      #   fi        
      #   $TNL_MATRIX_CONVERT --input-file $unzipped_matrix.amd --output-file $unzipped_matrix.amd.double.bin.bz2 --precision double --verbose yes --verify no
      #   #rm $unzipped_matrix.amd
      #   gzip $unzipped_matrix
      #else
      #   echo "Matrix $unzipped_matrix.amd.double.bin.bz2 was already converted."         
      #fi         
   fi
done

for link in $FLORIDA_MM_MATRICES;
do
   echo "###############################################################################"
   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 usualy in array format
     for file in $DIRNAME/$SUBDIRNAME/*.mtx;
     do
         if test ! -e $file.float.bin.bz2;
         then
            #echo "Converting matrix $file.float.bin.bz2 ..."
            $TNL_MATRIX_CONVERT --input-file $file --output-file $file.float.bin.bz2 --precision float --verbose yes --verify no
         else
            echo "Matrix $file.float.bin.bz2 was already converted."             
         fi
         if test ! -e $file.double.bin.bz2;
         then
            #echo "Converting matrix $file.double.bin.bz2 ..."
            $TNL_MATRIX_CONVERT --input-file $file --output-file $file.double.bin.bz2 --precision double --verbose yes --verify no
         else
            echo "Matrix $file.double.bin.bz2 was already converted."             
         fi         

         #######
         ## AMD ordering
         ###         
         #if test ! -e $file.amd.float.bin.bz2;
         #then
         #   echo "Converting matrix $file.amd.float.bin.bz2 ..."
         #   if test ! -e $file.amd;
         #   then
         #      ./reorder-csr -A -g 32 -i $file -o $file.amd
         #   fi
         #   $TNL_MATRIX_CONVERT --input-file $file.amd --output-file $file.amd.float.bin.bz2 --precision float --verbose yes --verify no
         #   #rm $file.amd
         #else
         #   echo "Matrix $file.amd.float.bin.bz2 was already converted."             
         #fi
         #if test ! -e $file.amd.double.bin.bz2;
         #then
         #   echo "Converting matrix $file.amd.double.bin.bz2 ..."
         #   if test ! -e $file.amd;
         #   then 
         #      ./reorder-csr -A -g 32 -i $file -o $file.amd
         #   fi
         #   $TNL_MATRIX_CONVERT --input-file $file.amd --output-file $file.amd.double.bin.bz2 --precision double --verbose yes --verify no
         #   #rm $file.amd
         #else
         #   echo "Matrix $file.amd.double.bin.bz2 was already converted."             
         #fi                                          
         #rm $file
      done
   fi
done
