#!/bin/bash


### TODO: Provide an option to remove logical volumes...


### Include ymclibnettools
. /usr/lib/lib-fliwi/ymc-networktools.bash

if [ $(ymc_is_chroot) -ne 0 ]; then
  echo "WARNING: $0 can not be run inside a chroot..."
  exit 0
fi

### handle host-based logical-volumes (by always ignoring 'root' and 'swap')
for config_name in $(fliwi-get-my-logical-volumes --host-based-lookup --show-config-name | grep -v -E -e '^(root|swap)$')
do
  echo "INFO: Processing volume '$config_name'..."
  volume_name=$(fliwi-get-my-logical-volumes --host-based-lookup --limit=$config_name --show-volume-name)
  if [ $? -ne 0 ] || \
     [ "$volume_name" == "" ]; then
    echo "ERROR: Can not get the volume-name for volume '$config_name' - skipping"
    continue
  fi

  volume_group_name=$(fliwi-get-my-logical-volumes --host-based-lookup --limit=$config_name --show-volume-group)
  if [ $? -ne 0 ] || \
     [ "$volume_group_name" == "" ]; then
    echo "ERROR: Can not get the volume-group for volume '$config_name' - skipping"
    continue
  fi

  device_path=$(fliwi-get-my-logical-volumes --host-based-lookup --limit=$config_name --show-device-path)
  if [ $? -ne 0 ] || \
     [ "$device_path" == "" ]; then
    echo "ERROR: Can not get the device-path for volume '$config_name' - skipping"
    continue
  fi

  if [ -b "$device_path" ]; then
    echo "INFO: Volume '$config_name' already exists - skipping..."
    continue
  fi

  if [ -e "$device_path" ]; then
    echo "ERROR: Something exists at '$device_path' but it is not a block-device - skipping..."
    continue
  fi

  volume_size=$(fliwi-get-my-logical-volumes --host-based-lookup --limit=$config_name --show-size)
  if [ $? -ne 0 ] || \
     [ "$volume_size" == "" ]; then
    echo "ERROR: Can not get size for volume '$config_name' - skipping"
    continue
  fi

  volume_filesystem=$(fliwi-get-my-logical-volumes --host-based-lookup --limit=$config_name --show-filesystem)
  if [ $? -ne 0 ]; then
    echo "ERROR: Can not get filesystem for volume '$config_name' - skipping..."
    continue
  fi

  if [ -n "$volume_filesystem" ] && \
     [ -z "$(which mkfs.$volume_filesystem 2>/dev/null)" ]; then
    echo "ERROR: No tool to create a filesystem of type '$volume_filesystem' for volume '$config_name' found - skipping..."
    continue
  fi

  lvcreate -n $volume_name -L $volume_size $volume_group_name
  if [ $? -ne 0 ]; then
    echo "ERROR: Failed to create the logical volume '$config_name' - stopping further processing..."
    exit 1
  fi

  ### Generate the filesystem if needed... //start
  if [ "$volume_filesystem" == "" ]; then
    echo "INFO: No filesystem defined for volume '$config_name' - not creating one..."
    continue
  fi

  echo "INFO: Generating a filesystem of type '$volume_filesystem' on '$device_path'"
  mkfs.$volume_filesystem $device_path
  if [ $? -ne 0 ]; then
    echo "ERROR: Failed to create a filesystem of type '$volume_filesystem' on volume '$config_name' - stopping further processing..."
    exit 1
  fi
  ### Generate the filesystem if needed... //end
done


### handle service-based logical-volumes
for my_service in $(fliwi-get-my-services)
do
  for config_name in $(fliwi-get-logical-volumes --service-based-lookup --show-config-name $my_service)
  do
    echo "INFO: Processing volume '$config_name' for service '$my_service'..."

    volume_name=$(fliwi-get-logical-volumes --service-based-lookup --limit=$config_name $my_service --show-volume-name)
    if [ $? -ne 0 ] || \
       [ "$volume_name" == "" ]; then
      echo "ERROR: Can not get the volume-name for volume '$config_name' - skipping"
      continue
    fi

    volume_group_name=$(fliwi-get-logical-volumes --service-based-lookup --limit=$config_name $my_service --show-volume-group)
    if [ $? -ne 0 ] || \
       [ "$volume_group_name" == "" ]; then
      echo "ERROR: Can not get the volume-group for volume '$config_name' of service '$my_service' - skipping"
      continue
    fi

    device_path=$(fliwi-get-logical-volumes --service-based-lookup --limit=$config_name $my_service --show-device-path)
    if [ $? -ne 0 ] || \
       [ "$volume_group_name" == "" ]; then
      echo "ERROR: Can not get the device-path for volume '$config_name' of service '$my_service' - skipping"
      continue
    fi

    if [ -b "$device_path" ]; then
      echo "INFO: Volume '$config_name' already exists - skipping..."
      continue
    fi

    if [ -e "$device_path" ]; then
      echo "ERROR: Something exists at '$device_path' but it is not a block-device - skipping..."
      continue
    fi

    volume_size=$(fliwi-get-logical-volumes --service-based-lookup --limit=$config_name $my_service --show-size)
    if [ $? -ne 0 ] || \
       [ "$volume_size" == "" ]; then
      echo "ERROR: Can not get size for volume '$config_name' of service '$my_service' - skipping"
      continue
    fi

    volume_filesystem=$(fliwi-get-logical-volumes --service-based-lookup --limit=$config_name $my_service --show-filesystem)
    if [ $? -ne 0 ]; then
      echo "ERROR: Can not get filesystem for volume '$config_name' of service '$my_service' - not creating one..."
      continue
    fi

    if [ -n "$volume_filesystem" ] && \
       [ -z "$(which mkfs.$volume_filesystem 2>/dev/null)" ]; then
      echo "ERROR: No tool to create a filesystem of type '$volume_filesystem' for volume '$config_name' found - skipping..."
      continue
    fi

    lvcreate -n $volume_name -L $volume_size $volume_group_name
    if [ $? -ne 0 ]; then
      echo "ERROR: Failed to create the logical volume '$config_name' of service '$my_service' - stopping further processing..."
      exit 1
    fi

    ### Generate the filesystem if needed... //start
    if [ "$volume_filesystem" == "" ]; then
      echo "INFO: No filesystem defined for volume '$config_name' of service '$my_service' - not creating one..."
      continue
    fi

    echo "INFO: Generating a filesystem of type '$volume_filesystem' on '$device_path'"
    mkfs.$volume_filesystem $device_path
    if [ $? -ne 0 ]; then
      echo "ERROR: Failed to create a filesystem of type '$volume_filesystem' on volume '$config_name' of service '$my_service' - stopping further processing..."
      exit 1
    fi
    ### Generate the filesystem if needed... //end
  done
done


### handle logical volumes for vServers on fliwi-vhosts (service needs to start with fliwi-vhost)
for my_fliwi_vhosts_service in $(fliwi-get-my-services | grep -E -e '^fliwi-vhost')
do
  for config_name in $(fliwi-get-logical-volumes --vhost-based-lookup --show-config-name $my_fliwi_vhosts_service)
  do
    echo "INFO: Processing volume '$config_name' for vHost-service '$my_fliwi_vhosts_service'..."

    volume_name=$(fliwi-get-logical-volumes --vhost-based-lookup --limit=$config_name $my_fliwi_vhosts_service --show-volume-name)
    if [ $? -ne 0 ] || \
       [ "$volume_name" == "" ]; then
      echo "ERROR: Can not get the volume-name for volume '$config_name' - skipping"
      continue
    fi

    volume_group_name=$(fliwi-get-logical-volumes --vhost-based-lookup --limit=$config_name $my_fliwi_vhosts_service --show-volume-group)
    if [ $? -ne 0 ] || \
       [ "$volume_group_name" == "" ]; then
      echo "ERROR: Can not get the volume-group for volume '$config_name' of service '$my_fliwi_vhosts_service' - skipping"
      continue
    fi

    device_path=$(fliwi-get-logical-volumes --vhost-based-lookup --limit=$config_name $my_fliwi_vhosts_service --show-device-path)
    if [ $? -ne 0 ] || \
       [ "$volume_group_name" == "" ]; then
      echo "ERROR: Can not get the device-path for volume '$config_name' of service '$my_fliwi_vhosts_service' - skipping"
      continue
    fi

    if [ -b "$device_path" ]; then
      echo "INFO: Volume '$config_name' already exists - skipping..."
      continue
    fi

    if [ -e "$device_path" ]; then
      echo "ERROR: Something exists at '$device_path' but it is not a block-device - skipping..."
      continue
    fi

    volume_size=$(fliwi-get-logical-volumes --vhost-based-lookup --limit=$config_name $my_fliwi_vhosts_service --show-size)
    if [ $? -ne 0 ] || \
       [ "$volume_size" == "" ]; then
      echo "ERROR: Can not get size for volume '$config_name' of service '$my_fliwi_vhosts_service' - skipping"
      continue
    fi

    lvcreate -n $volume_name -L $volume_size $volume_group_name
    if [ $? -ne 0 ]; then
      echo "ERROR: Failed to create the logical volume '$config_name' of service '$my_fliwi_vhosts_service' - stopping further processing..."
      exit 1
    fi
  done
done


exit 0
