#!/bin/bash
set -e
source beegfs-sp

print_usage() {
  echo "this script restores BeeGFS quota settings based on the input files"
  echo "- quota_default_sp<POOLID>.csv"
  echo "- quota_gid_sp<POOLID>.csv"
  echo "- quota_uid_sp<POOLID>.csv"
  echo "where <POOLID> is the respective id of the storage pool for which the quota should be recovered."
  echo ""
  echo "If no applicable files are available in the current execution directory,"
  echo "then the script stops and prints this message."
}

test_file_exists() {
  if [ -f "$1" ]
  then
    FOUND_FILE=true
  fi
}

restore_quota_storage_pool() {
  STORAGE_POOL_ID=$1

  if [ -z "$STORAGE_POOL_ID" ]
  then
    STORAGE_POOL_PARA=""
    STORAGE_POOL_ECHO=""
  else
    STORAGE_POOL_PARA="--storagepoolid=$STORAGE_POOL_ID"
    STORAGE_POOL_ECHO="for storage pool $STORAGE_POOL_ID"
  fi

  FNAME="quota_default_sp$STORAGE_POOL_ID.csv"
  if $POOL6TO7
  then
    FNAME="quota_default_sp.csv"
  fi

  if [ -f "$FNAME" ]
  then
    #echo "restoring default user quota from file '$FNAME'..."

    DEFAULT_USER_SIZE=$(cut -d, -f1 "$FNAME")
    DEFAULT_USER_INODES=$(cut -d, -f2 "$FNAME")

    #Do NOT put $STORAGE_POOL_PARA in quotation marks because this variable can be empty!
    beegfs-ctl --setquota --uid --default \
    --sizelimit="$DEFAULT_USER_SIZE" \
    --inodelimit="$DEFAULT_USER_INODES" \
    $STORAGE_POOL_PARA
    echo "restored default user quota $STORAGE_POOL_ECHO from file $FNAME."

    #echo "restoring default user quota from file '$FNAME'..."

    DEFAULT_GROUP_SIZE=$(cut -d, -f3 "$FNAME")
    DEFAULT_GROUP_INODES=$(cut -d, -f4 "$FNAME")

    #Do NOT put $STORAGE_POOL_PARA in quotation marks because this variable can be empty!
    beegfs-ctl --setquota --gid --default \
    --sizelimit="$DEFAULT_GROUP_SIZE" \
    --inodelimit="$DEFAULT_GROUP_INODES" \
    $STORAGE_POOL_PARA
    echo "restored default group quota $STORAGE_POOL_ECHO from file $FNAME."
  else
    echo "--!!--WARNING--!!-- file $FNAME does not exist. skipping default group restore $STORAGE_POOL_ECHO!"
  fi

  FNAME="quota_uid_sp$STORAGE_POOL_ID.csv"
  if $POOL6TO7
  then
    FNAME="quota_uid_sp.csv"
  fi
  if [ -f "$FNAME" ]
  then
    #echo "restoring user quota from file '$FNAME'..."
    echo ""
    #Do NOT put $STORAGE_POOL_PARA in quotation marks because this variable can be empty!
    beegfs-ctl --setquota --uid --file="$FNAME" $STORAGE_POOL_PARA
    echo "restored user quota $STORAGE_POOL_ECHO from file $FNAME."
  else
    echo "--!!--WARNING--!!-- file $FNAME does not exist. skipping user quota restore $STORAGE_POOL_ECHO!"
  fi

  FNAME="quota_gid_sp$STORAGE_POOL_ID.csv"
  if $POOL6TO7
  then
    FNAME="quota_gid_sp.csv"
  fi
  if [ -f "$FNAME" ]
  then
    #echo "restoring user quota from file '$FNAME'..."
    #Do NOT put $STORAGE_POOL_PARA in quotation marks because this variable can be empty!
    beegfs-ctl --setquota --gid --file="$FNAME" $STORAGE_POOL_PARA
    echo "restored group quota $STORAGE_POOL_ECHO from file $FNAME."
  else
    echo "--!!--WARNING--!!-- file $FNAME does not exist. skipping group quota restore $STORAGE_POOL_ECHO!"
  fi

  echo ""
}

main_restore(){
  get_storage_pools

  FOUND_FILE=false
  POOL6TO7=false
  if $POOLS_EXIST
  then  
    echo "Found existing storage pool IDs:"
    for INDEX in ${!POOL_ID_ARRAY[*]}
    do
      echo "${POOL_ID_ARRAY[$INDEX]}"
      test_file_exists "quota_default_sp${POOL_ID_ARRAY[$INDEX]}.csv"
      test_file_exists "quota_gid_sp${POOL_ID_ARRAY[$INDEX]}.csv"
      test_file_exists "quota_uid_sp${POOL_ID_ARRAY[$INDEX]}.csv"
      if ! $FOUND_FILE
      then
        test_file_exists "quota_default_sp.csv"
        test_file_exists "quota_gid_sp.csv"
        test_file_exists "quota_uid_sp.csv"
	if $FOUND_FILE
	then
	  POOL6TO7=true
	fi
      fi
    done
  else
    test_file_exists "quota_default_sp.csv"
    test_file_exists "quota_gid_sp.csv"
    test_file_exists "quota_uid_sp.csv"
  fi
  echo ""

  if ! $FOUND_FILE
  then
    echo "--!!--WARNING--!!-- no applicable input file found"
    print_usage
    exit 1
  fi


  if $POOLS_EXIST
  then
    for INDEX in ${!POOL_ID_ARRAY[*]}
    do
      echo "processing data for storage pool ID ${POOL_ID_ARRAY[$INDEX]}"
      restore_quota_storage_pool "${POOL_ID_ARRAY[$INDEX]}"
    done
  else
    echo "processing data"
    restore_quota_storage_pool
  fi
}

main_restore
