#!/bin/bash
#Step 0: Login in as root 
if [ "$EUID" -ne 0 ]
  then echo "Please run this script as root"
  exit
fi
#This will update the storage policy for the files
/srv/hops/mysql-cluster/ndb/scripts/mysql-client.sh hops -e "update hdfs_inodes set storage_policy = 0x03 where is_dir = 1 and storage_policy = 0x0E" 2>/dev/null
/srv/hops/mysql-cluster/ndb/scripts/mysql-client.sh hops -e "update hdfs_inodes set storage_policy = 0x03 where is_dir = 0 and file_stored_in_db = 0 and storage_policy = 0x07 and under_construction = 0" 2>/dev/null
retries=-1
count=0
while [[ retries -lt 3 ]]; do
  echo "Moving blocks to the cloud"
  su - hdfs -c "/srv/hops/hadoop/bin/hdfs mover -p /"
  retries=$((retries+1))
  count=$(/srv/hops/mysql/bin/mysql --defaults-file=/srv/hops/mysql-cluster/my.cnf -u root --skip-password -S /srv/hops/mysql-cluster/mysql.sock  hops -e "select count(*) from hdfs_replicas" -s -N 2>/dev/null)
  if [[ $count -eq "0" ]]; then
    echo "All blocks moved to the cloud"
     break;
  fi
done
if [[ $count -ne "0" ]]; then
  echo "Unable to move all files to cloud. Running FSCK"
  su - hdfs -c "/srv/hops/hadoop/bin/hdfs fsck  /"
fi
