#!/bin/bash
# storage resize: {{now}}
{%- if debug and debug == "yes" %} set -x {% endif %}

_on_resize() {
  [ -z "$1" ] && echo "No mount_path found !" && return 1
  local mount_path=$1
  local df_data
  local df_source
  local df_dev
  local df_part
  local df_type
  local df_target
  df_data=$(df "$mount_path" --output=source,fstype,target | egrep -v ^File)
  df_source=$(echo "$df_data" | cut -f1 -d" ")
  df_dev=$(echo "$df_source" | sed 's/p/ /g' | cut -f1 -d" ")
  df_part=$(echo "$df_source" | sed 's/p/ /g' | cut -f2 -d" ")
  df_type=$(echo "$df_data" | cut -f2 -d" ")
  df_target=$(echo "$df_data" | cut -f3 -d" ")

  [ -z "$df_dev" ] || [ ! -e "$df_dev" ] && echo "No device path fount $df_dev" && exit 1
  sudo growpart $df_dev $df_part

  # if ext fstype
  [ -n "$(echo "$df_type" | grep "ext")" ] && sudo resize2fs $df_source
}

_on_resize $MOUNT_PATH
